Code covered by the BSD License  

Highlights from
external merge sort

external merge sort

by

 

simulates external merge sort.

merge(bufs,fid)
function [out]= merge(bufs,fid)
%this function is specially made for pass 0.
%out is the number of output files.
%bufs is the number of buffers for pass 0.
%fid is the file descriptor of the file to be externally sorted.

bufs=bufs;
fid=fid;

tmp=[];
br=0;
k=1;
%this while loop breaks only at the end of the file.
while 1
            tmp=[];
            p=1; 
            %this while loop reads from file pages equal to buffer.
            while p<=bufs 
            
            %j=line(fid)
            
            %if ~ischar(j), br=1;break, end
            j=[];
                
                
                
            tline = fgetl(fid);
            %tr=p;
            %if ~ischar(tline)
             %   tmp = [];
            %end
            if ~ischar(tline), br=1;break, end
           
            
            
            [h,t]=strtok(tline);
            h=str2num(h);
                       
            
            j=cat(2,j,h);
            
            i=1;
            %this while loop is meant for reading a line.
            while 1
                if isempty(t), break, end  
                [h,t]=strtok(t);
                temp=str2num(h);
                
                
                j=cat(2,j,temp);
                
                
                %fprintf(fid2,'%s \n',tline);
                         
                
                
                i=i+1;
                  
                %fclose(mid);
            end

            
            %j=sort(j);   
            tmp=[tmp;j];
            %tmp=sort(tmp);
            %[m,n] = size(tmp);
            %tmp=sort(tmp,2);
            p=p+1;
            end
                tmp=algo(tmp); 
                if isempty(tmp),break,end           
                %display('control is there') 
                d=[0,k];
                z=int2str(d);
                %c='guri.txt';
                c=strcat(z,'.txt');
                %mid=fopen('c.txt','wt');
                dlmwrite(c, tmp, 'delimiter', ' ', 'precision', 6)
                display(tmp)    
                
                if br==1;k=k+1,display('hiii'),break,end
                
                k=k+1;
           
            
            
end
%this output variable signifies the number of runs created in this pass.
out=k-1
end

Contact us