c - Can anyone explain why my Merge Sort is not working? -
can please explain why not working? i'm having trouble getting numbers in array sorted , placed empty b array. i've searched high , low there seems eludes me, fellow cod3rz!
#include <stdio.h> #include <string.h> void merge(int array[], int low, int mid, int high); void midpoint(int array[], int low, int high); int main(int argc, const char * argv[]){ int b[100]; int array[] = { 13, 23, 37, 45, 55, 68, 79, 93}; int arraylength = sizeof(array)/sizeof(array[0]); (int =0 ; < arraylength; i++){ ( int j= 1 + ; j < arraylength; j++){ midpoint(&array[i], ,j );{ } printf("%d\n", *b); } } return 0; } void merge(int array[], int low, int mid, int high) { int b[100]; int b[100]; int = low; int b[100]; int j = mid + 1; int b[100]; int k = 0; while (i <= mid && j <= high) { if (array[i] <= array[j]) b[k++] = array[i++]; else b[k++] = array[j++]; } while (i <= mid) b[k++] = array[i++]; while (j <= high) b[k++] = array[j++]; k--; while (k > 0) { array[low + k] = b[k]; k --; } } void midpoint(int array[], int low, int high) { int mid; if (low < high) { mid = (high + low)/2; midpoint(array, low, mid); midpoint(array, mid + 1, high); merge(array, low, mid, high); } }
first of all, you're not passing b
array recursive calls, there no way it's going populated. furthermore, should able sorting in-place in array
, of helper array assists merge operations. so, there no need b
@ top level.
your merge()
function bit reversed. should copy entire array
array temp b
array, , merge b
array
.
that way, when algorithm finished, array
sorted @ top level, , have sorted array.
also, remove nested loops. since mergesort recursive divide , conquer algorithm, should start 1 call midpoint()
, , recursion take care of rest if implemented correctly.
so this:
(int =0 ; < arraylength; i++){ ( int j= 1 + ; j < arraylength; j++){ midpoint(&array[i], ,j );{ } printf("%d\n", *b); }
should this:
midpoint(array, 0 ,arraylength-1 );
also, have bracket issue, take out opening bracket line, , clean closing brackets necessary:
midpoint(&array[i], ,j );{
edit: there lot more problems in there aside outlined above. i'm hesitant spoon-feed answer since school assignment, since made effort , general code structure correct, , it's not can't search , find similar solutions online, i'll go ahead , post this.
here working solution implements mergesort algorithm based on code:
#include <stdio.h> #include <string.h> void merge(int *array, int low, int mid, int high); void midpoint(int *array, int low, int high); int main(int argc, const char * argv[]){ int array[] = { 100, 13, 44, 23, 22, 37, 3, 45, 2, 55, 68, 1, 79, 93, 4}; int arraylength = sizeof(array)/sizeof(array[0]); //start recursive calls midpoint(array, 0 ,arraylength-1 ); int i; (i = 0; < arraylength; i++ ){ printf("%d\n", array[i]); } return 0; } void merge(int *array, int low, int mid, int high) { int b[100]; //copy current elements temp array int v; (v = low; v <= high; v++){ b[v] = array[v]; } int = low; int j = mid + 1; int k = low; //merge left side , right side array in sorted order while (i <= mid && j <= high) { if (b[i] <= b[j]) array[k++] = b[i++]; else array[k++] = b[j++]; } //the left side might have left-over elements while (i <= mid) array[k++] = b[i++]; } void midpoint(int *array, int low, int high) { int mid; if (low < high) { mid = (high + low)/2; midpoint(array, low, mid); midpoint(array, mid + 1, high); merge(array, low, mid, high); } }
output:
1 2 3 4 13 22 23 37 44 45 55 68 79 93 100