Ok i know this has all sorts of issues, im getting tons of errors, any suggestion on where to start?
#include <stdio.h>
#include <stdlib.h>
typdef char byte;
void memcopy( byte *to, byte *from, int count ) {
while ( count-- > 0 ) *to++ = *from++ ;
}
int compare( byte *a , byte *b ) {
if( *a == *b ) return( 1 );
else return( 0 );
}
void merge_sort( byte data[], int n, int elementsize, int (*p_cmp_f)( ) );
int main( int argc, char *argv[] ) {
int A[500000] ;
int n , i ;
n = atoi( argv[1] ) ;
for ( i = 0 ; i <= n ; i++ ) A[i] = rand() % 10000 ;
for ( i = 0 ; i <= 19 ; i++ ) printf(" %d ", A[i] ) ;
printf("\n") ;
merge_sort( (byte *) A, n , sizeof(int) , compare ) ;
printf("\n") ;
for ( i = 0 ; i <= 19 ; i++ ) printf(" %d ", A[i] ) ;
printf("\n") ;
return 0 ;
}
void merge_sort( byte data[], int n, int elementsize, int (*p_cmp_f)( ) ) {
byte *firsthalf;
byte *endoffirsthalf;
byte *secondhalf;
byte *endofsecondhalf;
byte *resultbuffer, *p_result;
int halfsize;
if( n <= 1 )
return;
halfsize = n/2;
firsthalf = data;
secondhalf = data + halfisze * elementsize;
mergesort( firsthalf, halfisze, elementsize, p_cmp_f );
mergesort( secondhalf, n - halfsize, elementsize, p_cmp_f );
endoffirsthalf = secondhalf;
endofsecondhalf = data + n * elementsize;
resultbuffer = ( byte * ) malloc( n * elementsize );
p_result = resultbuffer;
while( firsthalf < endoffirsthalf && secondhalf < endofsecondhalf ){
if( ( *p_cmp_f )( firsthalf, secondhalf ) < 0 ){
memcpy( p_result, firsthalf, elementsize );
firsthalf += elementsize;
} else {
memcpy( p_result, secondhalf, elementsize );
secondhalf += elementsize;
}
p_result += elementsize;
}
while( firsthalf < endoffirsthalf ){
memcpy( p_result, firsthalf, elementsize );
firsthalf += elementsize;
p_result += elementsize;
}
while( secondhalf < endofsecondhalf ){
memcpy( p_result, secondhalf, elementsize );
secondhalf += elementsize;
p_result += elementsize;
}
memcpy( data, resultbuffer, n * elementsize );
free( resultbuffer );
}
}