0
0
DSA Cprogramming~10 mins

Quick Sort as Divide and Conquer in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to swap two integers using pointers.

DSA C
void swap(int *a, int *b) {
    int temp = *a;
    *a = [1];
    *b = temp;
}
Drag options to blanks, or click blank then click option'
A*b
B*a
Ctemp
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning *a = temp instead of *a = *b
Using a or b instead of *a or *b
2fill in blank
medium

Complete the code to choose the last element as pivot in partition function.

DSA C
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;
}
Drag options to blanks, or click blank then click option'
Ahigh
Blow
C0
Dlow + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing pivot as arr[low] instead of arr[high]
Using an index outside the array bounds
3fill in blank
hard

Fix the error in the recursive quickSort call to sort the left partition.

DSA C
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);
    }
}
Drag options to blanks, or click blank then click option'
Api + 1
Blow
Chigh
Dpi - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using pi + 1 for left partition causing overlap
Using high instead of pi - 1
4fill in blank
hard

Fill both blanks to complete the partition function's for loop condition and swap call.

DSA C
int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j [1] high; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(&arr[i], &arr[[2]]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return i + 1;
}
Drag options to blanks, or click blank then click option'
A<
B<=
Cj
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in loop causing pivot to be included
Swapping with arr[i] instead of arr[j]
5fill in blank
hard

Fill all three blanks to complete the quickSort function with correct recursive calls and base condition.

DSA C
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);
    }
}
Drag options to blanks, or click blank then click option'
A<
Bpi - 1
Cpi + 1
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= in base condition causing infinite recursion
Swapping recursive call bounds