c++ - quicksort bug not found -
after working whole day on this, cannot find wrong code, cannot find error is. can please me figure out did wrong?
void a_quick(int array[], int i, int j){ //array quick sort if ((j - i) < 2){ return; } int top = j; int bot = i; int p_location = + (j + i) / 2; int pivot = array[p_location]; while (i < j){ while (array[i] < pivot){ i++; } while (array[j] > pivot){ j--; } if (i <= j){ swap(array[i], array[j]); i++; j--; } } a_quick(array, bot, p_location); a_quick(array, p_location + 1, top); } running code on 64k int arrays crash computer, , need run on 128k.
im getting:
test array: 703 322 673 141 253 547 662 37 723 529 316 190 288 40 264 446 890 370 6 393 quick sort w array: 6 37 40 141 253 190 316 288 264 322 393 370 446 529 723 662 890 547 673 703
this code works fine me
void a_quick(int array[], int i, int j){ //array quick sort int top = j; int bot = i; int p_location = (j + i) / 2; int pivot = array[p_location]; { while (array[i] < pivot)i++; while (array[j] > pivot)j--; if (i <= j){ swap(array[i], array[j]); i++; j--; } } while (i<=j); if (bot<j) a_quick(array, bot, j);// problem here if (i<top) a_quick(array, i, top);// or here }
Comments
Post a Comment