Posts

Showing posts with the label merge sort

C: Implement merge sort.

https://github.com/pereiradaniel/c_programs/blob/master/merge_sort.c // Implement merge sort algorithm. #include <stdio.h> #include "length.h" void merge_sort(int a[], int length); void merge_sort_recursion(int a[], int l, int r); void merge_sorted_arrays(int a[], int l, int m, int r); int main(int argc, char* argv[]) { int array[] = {9,4,8,1,7,0,3,2,5,6}; // test array int length = LENGTH(array); // Sort array using merge_sort: merge_sort(array, length); // Print array: for(int i=0; i < length; ++i) printf("%d", array[i]); printf("\n"); return 0; } // Perform a merge sort of the array using the given length. void merge_sort(int a[], int length) { // Call the merge_sort_recursion function to sort array: // - Initially we will want to use the whole array. // - Use left index of 0 and right index of -1. merge_sort_recursion(a, 0, length - 1); } // Recursive portion of the algorithm:...