C Program
#include<stdio.h> void q(int a[], int l, int h) { if(l >= h) return; int p = a[h], i = l - 1, t; for(int j = l; j < h; j++) if(a[j] < p) { t = a[++i]; a[i] = a[j]; a[j] = t; } t = a[i+1]; a[i+1] = a[h]; a[h] = t; q(a, l, i); q(a, i+2, h); } int main() { int a[100], n; scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &a[i]); q(a, 0, n-1); for(int i = 0; i < n; i++) printf("%d ", a[i]); }
C Output
Input: 6 4 2 8 3 1 7 Output: 1 2 3 4 7 8
C++ Program
#include<iostream> using namespace std; void q(int a[], int l, int h) { if(l >= h) return; int p = a[h], i = l - 1; for(int j = l; j < h; j++) if(a[j] < p) swap(a[++i], a[j]); swap(a[i+1], a[h]); q(a, l, i); q(a, i+2, h); } int main() { int a[100], n; cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; q(a, 0, n-1); for(int i = 0; i < n; i++) cout << a[i] << " "; }
C++ Output
Input: 5 9 4 6 1 3 Output: 1 3 4 6 9
JAVA Program
import java.util.*; class Q { static void q(int[] a, int l, int h) { if(l >= h) return; int p = a[h], i = l - 1, t; for(int j = l; j < h; j++) if(a[j] < p) { t = a[++i]; a[i] = a[j]; a[j] = t; } t = a[i+1]; a[i+1] = a[h]; a[h] = t; q(a, l, i); q(a, i+2, h); } public static void main(String[] a) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), arr[] = new int[n]; for(int i = 0; i < n; i++) arr[i] = sc.nextInt(); q(arr, 0, n-1); for(int i = 0; i < n; i++) System.out.print(arr[i] + " "); } }
JAVA Output
Input: 4 5 3 1 4 Output: 1 3 4 5
Python Program
def q(a): if len(a) < 2: return a p = a[-1] l = [x for x in a[:-1] if x < p] r = [x for x in a[:-1] if x >= p] return q(l) + [p] + q(r) a = list(map(int, input().split())) print(*q(a))
Python Output
Input: 7 2 5 1 9 Output: 1 2 5 7 9
In-Depth Learning – Complete Concept in Paragraphs
What Is Quick Sort?
Quick Sort is a fast and efficient sorting algorithm that follows the Divide and Conquer approach. It works by choosing a pivot element and then rearranging the array so that:
-
All elements smaller than the pivot go to the left,
-
All greater go to the right,
-
The pivot sits in its correct position.
Then it recursively applies the same logic to the left and right halves.
How the Code Works
-
A pivot (typically the last element) is chosen.
-
Elements are compared to the pivot. Smaller ones are moved left, larger ones right.
-
After one full pass, the pivot is in its correct sorted spot.
-
The process is repeated recursively on the sub-arrays.
This makes quick sort extremely efficient in practice, especially on large datasets.
Example
Let’s say:
The recursion keeps slicing the problem into smaller arrays until all are sorted.
Real-Life Analogy
Imagine sorting a group of people by height:
-
You pick one person (pivot).
-
Move everyone shorter to one side, taller to the other.
-
That pivot person now has the correct position.
-
Repeat the process with the remaining groups — this is exactly how Quick Sort behaves.
It’s like organizing piles around a central reference.
Where and When Is It Used?
Quick Sort is used:
-
In standard library implementations (like C's
qsort()
) -
When you need in-place sorting with high performance
-
In large datasets where performance matters
It's frequently asked in:
-
Coding interviews
-
Competitive programming
-
System-level and embedded optimizations
It's one of the most optimized and practical sorting techniques.
Time Complexity
Case | Time |
---|---|
Best | O(n log n) |
Average | O(n log n) |
Worst | O(n²) (when the pivot is the smallest or largest repeatedly) |
Despite the worst-case, quick sort is typically faster in real-life due to fewer comparisons and swaps.
Python-Specific Advantage
Python makes recursion and list partitioning cleaner with:
This functional style makes quick sort beautiful and readable for learning.
SEO-Optimized Natural Paragraph for Ranking
Want to learn how Quick Sort works with simple examples? This guide gives you the shortest and most efficient Quick Sort code in C, C++, Java, and Python. Quick Sort is one of the fastest sorting algorithms used in real-world applications. It divides the array using a pivot and recursively sorts the left and right halves. With real-life examples, optimized code, and beginner-friendly logic, you can master Quick Sort for interviews, exams, or competitive coding. Perfect for those learning how to sort large datasets quickly and understand how Divide and Conquer strategies work in programming.
Social Plugin