📖 Scenario: You are working on a program that needs to sort a list of numbers quickly. Sorting helps organize data so you can find things faster, like arranging books by their titles on a shelf.
🎯 Goal: Build a program that uses the Quick Sort algorithm to sort an array of integers in ascending order.
📋 What You'll Learn
Create an array called arr with the exact integers: 29, 10, 14, 37, 13
Create an integer variable n that stores the size of the array
Write a function partition that helps divide the array around a pivot
Write a recursive function quickSort that sorts the array using the partition function
Print the sorted array elements separated by spaces
💡 Why This Matters
🌍 Real World
Quick Sort is used in many software applications to organize data quickly, such as sorting search results or arranging files.
💼 Career
Understanding Quick Sort helps in technical interviews and improves problem-solving skills for software development roles.
Progress0 / 4 steps
1
Create the array and its size
Create an integer array called arr with these exact values: 29, 10, 14, 37, 13. Then create an integer variable n and set it to the size of arr.
DSA C++
Hint
Use sizeof(arr) / sizeof(arr[0]) to find the number of elements in the array.
2
Write the partition function
Write a function called partition that takes an integer array arr, two integers low and high. Use the last element as pivot, rearrange elements so smaller ones come before pivot, and return the pivot's final index.
DSA C++
Hint
The pivot is the last element arr[high]. Use a loop from low to high - 1 to compare and swap elements.
3
Write the quickSort function
Write a recursive function called quickSort that takes an integer array arr, two integers low and high. If low is less than high, call partition to get pivot index, then recursively sort the parts before and after the pivot.
DSA C++
Hint
Use the pivot index from partition to split the array and call quickSort on each part.
4
Print the sorted array
Call quickSort with arr, 0, and n - 1 to sort the array. Then print the sorted array elements separated by spaces on one line.
DSA C++
Hint
Call quickSort(arr, 0, n - 1) to sort. Use a loop to print each element followed by a space.
Practice
(1/5)
1. What is the main idea behind the Quick Sort algorithm?
easy
A. Divide the array into smaller parts using a pivot and sort them recursively
B. Compare every element with every other element and swap if needed
C. Insert elements one by one into a sorted list
D. Merge two sorted arrays into one sorted array
Solution
Step 1: Understand Quick Sort's approach
Quick Sort picks a pivot element and divides the array into parts smaller and larger than the pivot.
Step 2: Recursive sorting of parts
It then sorts these parts recursively, combining them to get the sorted array.
Final Answer:
Divide the array into smaller parts using a pivot and sort them recursively -> Option A
Quick Check:
Quick Sort = Divide and conquer using pivot [OK]
Hint: Remember: Quick Sort uses a pivot to split and sort recursively [OK]
Common Mistakes:
Confusing Quick Sort with Bubble Sort
Thinking Quick Sort merges arrays like Merge Sort
Assuming Quick Sort inserts elements one by one
2. Which of the following is the correct way to declare the partition function header in C++ for Quick Sort?
easy
A. void partition(int arr[], int low, int high);
B. int partition(int arr[], int high, int low);
C. int partition(int arr, int low, int high);
D. int partition(int arr[], int low, int high);
Solution
Step 1: Check parameter types and order
The partition function takes an array and two indices: low and high, both integers.
Step 2: Check return type
It returns an integer index where the pivot finally lies.
Final Answer:
int partition(int arr[], int low, int high); -> Option D
Quick Check:
Correct function signature = int partition(int arr[], int low, int high); [OK]
Hint: Partition returns pivot index; parameters are array and two ints [OK]
Common Mistakes:
Using void return type instead of int
Passing array as int instead of int[]
Swapping low and high parameters
3. What will be the output array after applying Quick Sort on arr = {4, 2, 7, 1} using the last element as pivot?
medium
A. [1, 2, 4, 7]
B. [1, 4, 2, 7]
C. [2, 1, 4, 7]
D. [7, 4, 2, 1]
Solution
Step 1: Partition with pivot 1
Pivot is 1 (last element). After partition, array becomes [1, 2, 7, 4] with pivot at index 0.
Step 2: Recursively sort subarrays
Left subarray is empty; right subarray [2,7,4] is sorted recursively to [2,4,7]. Final array is [1,2,4,7].
Final Answer:
[1, 2, 4, 7] -> Option A
Quick Check:
Sorted array = [1, 2, 4, 7] [OK]
Hint: Pivot last element; sort left and right recursively [OK]
Common Mistakes:
Not placing pivot correctly after partition
Sorting only once without recursion
Confusing pivot with first element
4. Identify the error in this Quick Sort partition code snippet:
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
swap(arr[i], arr[j]);
i++;
}
}
swap(arr[i], arr[high]);
return i;
}
medium
A. Swap should be done only if arr[j] <= pivot
B. Loop should run from j = low to j <= high
C. Initialization of i should be low - 1
D. Pivot should be arr[low] instead of arr[high]
Solution
Step 1: Check initialization of i
In partition, i should start at low - 1 to track smaller elements correctly.
Step 2: Analyze loop and swaps
Loop runs j from low to high-1 correctly; swap with arr[i+1] after loop is correct.
Final Answer:
Initialization of i should be low - 1 -> Option C
Quick Check:
i starts at low - 1 in partition [OK]
Hint: i starts before low to track smaller elements [OK]
Common Mistakes:
Starting i at low causes wrong partitioning
Loop running to high causes out-of-bound swap
Changing pivot position incorrectly
5. Given an array with many duplicate elements, which modification to Quick Sort helps improve performance?
hard
A. Always choose the first element as pivot
B. Use a three-way partitioning to separate less, equal, and greater elements
C. Skip sorting subarrays with size less than 5
D. Use Bubble Sort instead for duplicates
Solution
Step 1: Understand problem with duplicates
Standard Quick Sort performs poorly with many duplicates due to repeated swaps.
Step 2: Apply three-way partitioning
Three-way partitioning divides array into less than, equal to, and greater than pivot, reducing unnecessary work.
Final Answer:
Use a three-way partitioning to separate less, equal, and greater elements -> Option B