Skip to content

Instantly share code, notes, and snippets.

@kotaroito
Created February 5, 2014 00:45
Show Gist options
  • Save kotaroito/8815408 to your computer and use it in GitHub Desktop.
Save kotaroito/8815408 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define N 10
int sort[N];
int buffer[N];
void mergeSort(int n, int x[]){
int i, j, k, m;
if ( n <= 1 ) {
return;
}
m = n / 2;
mergeSort(m, x);
mergeSort(n - m, x + m);
for ( i = 0; i < m ; i++ ) {
buffer[i] = x[i];
}
i = k = 0;
j = m;
while ( i < m && j < n ) {
if ( buffer[i] <= x[j] )
x[k++] = buffer[i++];
else
x[k++] = x[j++];
}
while ( i < m ) {
x[k++] = buffer[i++];
}
}
int main(void)
{
int i;
srand((unsigned int) time(NULL));
for ( i = 0; i < N; i++ ) {
sort[i] = rand();
printf("%010d ", sort[i]);
}
printf("\n\nstart..\n\n");
mergeSort(N, sort);
for ( i = 0; i < N; i++ ) {
printf("%010d ", sort[i]);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment