Quick Sort
Quick Sort Algorithm in C++ (Complete Implementation)
C++ Quick Sort Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int partition(int arr[], int low, int high) {
int pivot = arr[high];
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;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
// Recursively sort elements before and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}Original array: 64 34 25 12 22 11 90 Sorted array: 11 12 22 25 34 64 90
Understanding Quick Sort
This program teaches you how to implement the Quick Sort algorithm in C++. Quick Sort is a highly efficient divide-and-conquer sorting algorithm that works by selecting a pivot element and partitioning the array around it. It's one of the fastest sorting algorithms in practice and is widely used in real-world applications.
---
1. What This Program Does
The program sorts an array of integers using the Quick Sort algorithm. For example:
Quick Sort works by selecting a pivot, partitioning the array so elements smaller than pivot are on the left and larger on the right, then recursively sorting the partitions.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Quick Sort
Algorithm Concept
:
1.
Choose Pivot
: Select an element as pivot (often last element)
2.
Partition
: Rearrange array so pivot is in correct position
3.
Recurse
: Recursively sort left and right partitions
Visual Example
:
[64, 34, 25, 12, 22, 11, 90]
(all < 90) (pivot)
Recurse on [64, 34, 25, 12, 22, 11]
(pivot) (all > 11)
Continue recursively...
---
4. Function: partition()
int partition(int arr[], int low, int high) {
int pivot = arr[high];
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;How it works
:
Partition Process
(pivot = 90):
[64, 34, 25, 12, 22, 11, 90]
...
---
5. Function: quickSort()
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
How it works
:
---
6. Understanding Partition
Partition Goal
:
Partition Index (pi)
:
---
7. Time and Space Complexity
Time Complexity
:
Space Complexity
: O(log n)
Stability
: Not Stable
---
8. When to Use Quick Sort
Best For
:
Not Recommended For
:
---
9. Important Considerations
Pivot Selection
:
Partition Balance
:
Recursive Calls
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning divide-and-conquer algorithms, understanding partitioning, and preparing for advanced algorithm design in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Quick Sort
This C++ program is part of the "Sorting & Searching Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.