50
loading...
This website collects cookies to deliver better user experience
billion
, which contains 1 billion 32 bit signed int binary data. We need to cut it up into a number of pieces. Here I divide it into 100 parts, with each part containing 10 million numbers. Each one of them need to be sorted. Here we use qsort to sort part of the file.static __int32 piece[PIECESIZE];
int comp( const void *a, const void *b )
{
return *(__int32*)a - *(__int32*)b;
}
FILE *billion = fopen( "billion", "rb" );
int i = 0;
while( i < TOTAL / PIECESIZE ){
fseek( billion, PIECESIZE * i, SEEK_SET );
fread( piece, sizeof( *piece ), PIECESIZE, billion );
qsort( piece, PIECESIZE, sizeof( *piece ), comp );
char fileName[300];
snprintf( fileName, sizeof(fileName), "pieces/piece%d", i );
FILE *outFile = fopen( fileName, "wb" );
fwrite( piece, sizeof( *piece ), PIECESIZE, outFile );
fclose( outFile );
++i;
}
FILE *outFile = fopen( "out", "wb" );
FILE *fileList[FILEAMOUNT];
int i = 0;
for( i = 0; i < FILEAMOUNT; ++i ){
char filePath[200];
snprintf( fileName, sizeof(fileName), "pieces/piece%d", i );
fileList[i] = fopen( filePath, "rb" );
}
int numbers[ FILEAMOUNT ];
for( i = 0; i < FILEAMOUNT; ++i ){
fread( numbers + i, sizeof( __int32 ), 1, fileList[i] );
}
int n = 0;
while( 1 ){
int minIndex = MinIndex( numbers );
if( minIndex == -1 ) break;
fwrite( numbers + minIndex, sizeof( __int32 ), 1, outFile );
++n;
fread( numbers + minIndex, sizeof( __int32 ), 1, fileList[minIndex] );
if( feof( fileList[minIndex] ) ){
numbers[minIndex] = -1;
}
}
for( i = 0; i < FILEAMOUNT; ++i ){
fclose( fileList[i] );
}
fclose( outFile );
MinIndex
function gets the subscript of the smallest value in the array, and it will skip when it encounters -1, I used number -1
as end of file. If the MinIndex
function returns -1, it means that all files have been read.int MinIndex( int *arr )
{
int i, index = -1;
for( i = 0; i < FILEAMOUNT; ++i ){
if( arr[i] == -1 ) continue;
if( index == -1 || arr[index] > arr[i] ) index = i;
}
return index;
}