Complete the code to swap two integers using pointers.
void swap(int* a, int* b) {
int temp = *a;
*a = [1];
*b = temp;
}We swap the values by assigning the value pointed by b to the location pointed by a.
Complete the code to choose the last element as pivot in partition function.
int partition(int arr[], int low, int high) {
int pivot = arr[[1]];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}The pivot is chosen as the last element of the array segment, which is at index 'high'.
Fix the error in the recursive quickSort call to sort the left partition.
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, [1]);
quickSort(arr, pi + 1, high);
}
}The left partition ends at index pi - 1, so we sort from low to pi - 1.
Fill both blanks to complete the partition function's for loop and condition.
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = [1]; j [2] high; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}The loop starts from 'low' and runs while 'j' is less than 'high'.
Fill all three blanks to complete the quickSort function calls and base condition.
void quickSort(int arr[], int low, int high) {
if (low [1] high) {
int pi = partition(arr, low, high);
quickSort(arr, low, [2]);
quickSort(arr, [3], high);
}
}The base condition is 'low < high'. The left partition is from low to pi - 1, and the right partition is from pi + 1 to high.
