0
0
DSA Javascriptprogramming~10 mins

Quick Sort Algorithm in DSA Javascript - Interactive Practice

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

Complete the code to choose the pivot element as the last element of the array.

DSA Javascript
function quickSort(arr) {
  if (arr.length <= 1) return arr;
  const pivot = arr[1];
  // rest of the code
}
Drag options to blanks, or click blank then click option'
A[0]
B[1]
C[arr.length - 1]
D[Math.floor(arr.length / 2)]
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the first element as pivot instead of the last.
Using arr.length instead of arr.length - 1.
2fill in blank
medium

Complete the code to partition the array by comparing elements with the pivot.

DSA Javascript
function partition(arr, low, high) {
  const pivot = arr[high];
  let i = low - 1;
  for (let j = low; j < high; j++) {
    if (arr[j] [1] pivot) {
      i++;
      [arr[i], arr[j]] = [arr[j], arr[i]];
    }
  }
  [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
  return i + 1;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than operator which reverses the partition logic.
Using >= or <= which includes equality and may cause incorrect swaps.
3fill in blank
hard

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

DSA Javascript
function quickSort(arr, low = 0, high = arr.length - 1) {
  if (low < high) {
    const pi = partition(arr, low, high);
    quickSort(arr, low, [1]);
    quickSort(arr, pi + 1, high);
  }
  return arr;
}
Drag options to blanks, or click blank then click option'
Api - 1
Bpi
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Using pi instead of pi - 1 causes infinite recursion.
Using high or low incorrectly changes the partition boundaries.
4fill in blank
hard

Fill both blanks to complete the recursive calls for sorting left and right partitions.

DSA Javascript
function quickSort(arr, low = 0, high = arr.length - 1) {
  if (low < high) {
    const pi = partition(arr, low, high);
    quickSort(arr, [1], pi - 1);
    quickSort(arr, pi + 1, [2]);
  }
  return arr;
}
Drag options to blanks, or click blank then click option'
Alow
Bpi
Chigh
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using pi instead of low or high causes wrong partition ranges.
Using 0 instead of low or high causes incorrect sorting range.
5fill in blank
hard

Fill all three blanks to complete the quickSort function with partitioning and recursive calls.

DSA Javascript
function quickSort(arr, [1] = 0, [2] = arr.length - 1) {
  if ([1] < [2]) {
    const pi = partition(arr, [1], [2]);
    quickSort(arr, [1], pi - 1);
    quickSort(arr, pi + 1, [2]);
  }
  return arr;
}
Drag options to blanks, or click blank then click option'
Alow
Bhigh
Cpivot
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using pivot or index as parameter names causes confusion.
Mixing parameter names in recursive calls causes errors.