void mergesort_iter(int left, int right, int *list) { /* Base case, the list can be no simpler */ if(right - left <= 1) return; /* set up bounds to slice array into */ int left_start = left; int left_end = (left + right) / 2; int right_start = left_end; int right_end = right; /* sort left half */ mergesort_iter(left_start, left_end, list); /* sort right half */ mergesort_iter(right_start, right_end, list); /* merge sorted havles back together */ merge(list, left_start, left_end, right_start, right_end); } /* end merge sort recursive iterator */
void test_mergesort() { int A[] = {1, 4, 6, 2, 3, 5}; int B[] = {5, 3, 1, 2, 4, 6}; int C[] = {0, 0, 0, 0, 0, 0}; mergesort(A, C, 0, 6); mergesort_iter(B, C, 5); print_list(A, 6); print_list(B, 5); }
void mergesort(int *list, int length) { mergesort_iter(0, length, list); } /* end merge sort */