Created
September 20, 2016 12:34
-
-
Save mosfet1kg/ea90eef26b0fe2886381bc50b33f2a55 to your computer and use it in GitHub Desktop.
merge sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define NUM_SCORE 15 | |
void merge(int arr[], int start, int q, int end){ | |
int temp_arr[end-start+1]; | |
int i=start, j= q+1; | |
for(int k=0; k<(end-start+1); k++){ | |
if( arr[i]>=arr[j]){ | |
temp_arr[k] = arr[i]; | |
i++; | |
if( i>q ){ | |
for(int p=j; p<=end; p++){ | |
temp_arr[++k] = arr[p]; | |
} | |
break; | |
} | |
}else{ | |
temp_arr[k] = arr[j]; | |
j++; | |
if( j>end ){ | |
for(int p=i; p<=q; p++){ | |
temp_arr[++k] = arr[p]; | |
} | |
break; | |
} | |
} | |
} // end for | |
for(int i=start, j =0; i<=end ; i++, j++ ){ | |
arr[i] = temp_arr[j]; | |
} | |
} | |
void split(int arr[], int start, int end){ | |
if( start < end ){ | |
int temp_end = ( start + end )/2; | |
// printf("%d %d %d\n", start, temp_end, end); | |
split(arr, start, temp_end); | |
split(arr, temp_end+1, end); | |
merge(arr, start, temp_end, end); | |
} | |
} | |
int main(void){ | |
FILE* fp = fopen("input2.txt", "rt"); | |
int score[NUM_SCORE]; | |
for(int i =0; i<NUM_SCORE ; i++){ | |
fscanf(fp, "%d", &score[i]); | |
// printf("%d\n", score[i]); | |
} | |
split(score, 0, NUM_SCORE-1); | |
for(int i =0; i<NUM_SCORE ; i++){ | |
// fscanf(fp, "%d", &score[i]); | |
printf("%d\n", score[i]); | |
} | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment