java - Doing something at the very end of recursive quickSort -
public static void quicksort(int[] a, int begin, int end, string s) { int = begin, j = end; int middle = begin + (end - begin) / 2; int pivot = a[middle]; while (i <= j) { while (a[i] < pivot) { i++; quicksortcomparisons++; } while (a[j] > pivot) { j--; quicksortcomparisons++; } if (i <= j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; quicksortexchanges++; i++; j--; } } // recursively sort 2 sub parts if (begin < j) quicksort(a, begin, j, s); if (end > i) quicksort(a, i, end, s); quicksortwork = quicksortcomparisons + (3 * quicksortexchanges); // want here when array //is sorted , recursive calls finished. }
i'm trying implement quicksort function. sorts correctly, issue i'm having need print things when array sorted. since called recursively, if print @ end, gets printed every single time function called. there type of dummy base case can create, perhaps like:
if(!sorted){ //do quicksort operations} else{ //print values wish print}
you should wrap initial call in non-recursive function this:
public static void quicksort(int[] a, int begin, int end, string s) { // call recursive method quicksort0(a, begin, end, s); // when here, array sorted system.out.println("sorted array: " + arrays.tostring(a)); } private static void quicksort0(int[] a, int begin, int end, string s) { // original, recursive quicksort method }
Comments
Post a Comment