let list = {20, 17, 25, 28, 8, 10, 21, 34, 7, 19, 32, 43, 11, 9, 12, 23, 13, 35, 18, 16, 15} If we were to call partition(list, 0, 20), what value will be returned? public static int partition(int[] list, int first, int last) { int pivot = list[first]; int low = first + 1; int high = last; while(high > low) { while(low <= high && list[low] <= pivot) low++; while(low <= high && list[high] > pivot) high--; if(high > low) { int temp = list[high]; list[high] = list[low]; list[low] = temp; } } while(high > first && list[high] >= pivot) high--; if(pivot > list[high]) { list[first] = list[high]; list[high] = pivot; return high; }else return first; }
let list = {20, 17, 25, 28, 8, 10, 21, 34, 7, 19, 32, 43, 11, 9, 12, 23, 13, 35, 18, 16, 15}
If we were to call partition(list, 0, 20), what value will be returned?
public static int partition(int[] list, int first, int last) {
int pivot = list[first];
int low = first + 1;
int high = last;
while(high > low) {
while(low <= high && list[low] <= pivot)
low++;
while(low <= high && list[high] > pivot)
high--;
if(high > low) {
int temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}
while(high > first && list[high] >= pivot)
high--;
if(pivot > list[high]) {
list[first] = list[high];
list[high] = pivot;
return high;
}else
return first;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps