Created
March 20, 2019 16:24
-
-
Save joxer/3dd1a5330b38c70701be8f7c4f3ada6c to your computer and use it in GitHub Desktop.
This file contains 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
-UUU:----F1 merge_sort.c Bot L63 [(C/l Abbrev)] -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
File Edit Options Buffers Tools C Help | |
#include <stdio.h> | |
#include <stdlib.h> | |
int sort(int array[], int size, int start, int m, int end){ | |
int idx1 = start; | |
int idx2 = m+1; | |
while(idx1 <= m && idx2 <= end){ | |
if(array[idx1] < array[idx2]){ | |
idx2+=1; | |
} | |
else if(array[idx1] > array[idx2]){ | |
int tmp = array[idx1]; | |
array[idx1] = array[idx2]; | |
array[idx2] = tmp; | |
idx1+=1; | |
} | |
else{ | |
idx2+=1; | |
int tmp = array[idx1]; | |
array[idx1] = array[idx2]; | |
array[idx2] = tmp; | |
} | |
} | |
return 0; | |
} | |
int mergeSort(int* array, int size, int start, int end){ | |
printf("s%d d%d\n", start,end); | |
if( (start-end) == 0) | |
return -1; | |
int m = (start+end)/2; | |
mergeSort(array, size, start, m); | |
mergeSort(array,size, m+1, end); | |
for(int i = start; i <= end;i++){ | |
printf("%d ",array[i]); | |
} | |
printf("\n"); | |
sort(array, size, start,m,end); | |
for(int i = start; i <= end;i++){ | |
printf("%d ",array[i]); | |
} | |
printf("\n"); | |
return -1; | |
} | |
int main(){ | |
int* array = (int*)malloc(sizeof(int)*6); | |
array[0] = 2; | |
array[1] = 3; | |
array[2] = 1; | |
array[3] = 8; | |
array[4] = 9; | |
array[5] = 4; | |
for(int i = 0; i < 7;i++){ | |
printf("%d ",array[i]); | |
} | |
mergeSort(array, 6, 0, 6); | |
printf("\n"); | |
array[0] = 2; | |
array[1] = 3; | |
array[2] = 1; | |
array[3] = 8; | |
array[4] = 9; | |
array[5] = 4; | |
for(int i = 0; i < 7;i++){ | |
printf("%d ",array[i]); | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment