MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies0%
Study Materials
Data Structures & AlgorithmsProgress 0%
1.1Introduction to DSA, Big-O, Big-Omega & Big-Theta Notations1.2Time Complexity Calculation Rules & Common Runtime Patterns1.3Space Complexity, Auxiliary Memory & Stack vs Heap Allocation
6.1Binary Search: Search Space Elimination, Lower Bound & Upper Bound6.2Binary Search on Answer Space: Allocation & Feasibility Checks6.3Elementary Sorting Algorithms: Bubble, Selection & Insertion Sort (O(n²))6.4Merge Sort: Divide-and-Conquer Paradigm & Stability Analysis6.5Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies
HomeTutorialsData Structures & AlgorithmsQuick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies
Binary Tree Traversals: DFS (Pre, In, Post) & BFS Level-Order

Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Quick Sort is an in-place divide-and-conquer sorting algorithm. In practical benchmarks, Quick Sort is typically 2x to 3x faster than Merge Sort and Heap Sort due to excellent CPU hardware cache locality and minimal memory movement.


1. Real-World Analogy: Sorting Students by Height Around a Reference

Imagine a gym teacher organizing 30 students by height:

  • The teacher picks one student (say, Alex) to stand in the center as the Pivot.
  • The teacher instructs: "Anyone shorter than Alex, move to his left. Anyone taller than Alex, move to his right."
  • Notice: Alex is now standing at his exact final sorted position forever!
  • The teacher repeats the exact same instruction recursively for the group on the left and the group on the right.

2. The Two Famous Partitioning Schemes

1. Lomuto Partition Scheme (Simpler to Understand)

  • The Pivot is chosen as the last element arr[high].
  • A slow pointer i maintains the boundary of elements smaller than the pivot.
  • A fast pointer j scans from low to high - 1.
  • When arr[j] <= pivot, increment i and swap arr[i] with arr[j].

2. Hoare Partition Scheme (Original & More Efficient)

  • Two pointers start at the outer ends (low - 1 and high + 1) and advance inward toward each other.
  • Makes approximately three times fewer swaps on average than Lomuto!

3. The Pivot Selection Trap ($O(n^2)$ Worst-Case)

If you always choose the first or last element as the pivot:

  • If the input array is already sorted or reverse sorted, the pivot produces a degenerate partition of size $0$ and $n - 1$.
  • The recursion tree degenerates from a balanced tree ($\log n$) into a linked list ($n$), resulting in worst-case $O(n^2)$ quadratic time!

The Remedy: Randomized Pivot or Median-of-Three

Pick three candidates (first, middle, last), find their mathematical median, and swap it to the pivot position. This guarantees balanced $O(n \log n)$ partitions in production!


4. Multi-Language Implementations: Quick Sort (Lomuto Partition)

Python 3:

1import random
2 
3def quick_sort(arr: list[int], low: int = 0, high: int = None) -> None:
4 if high is None:
5 high = len(arr) - 1
6
7 if low < high:
8 # Partition the array and get pivot index
9 pivot_index = randomized_partition(arr, low, high)
10
11 # Recursively sort elements before and after partition
12 quick_sort(arr, low, pivot_index - 1)
13 quick_sort(arr, pivot_index + 1, high)
14 
15def randomized_partition(arr: list[int], low: int, high: int) -> int:
16 rand_idx = random.randint(low, high)
17 arr[rand_idx], arr[high] = arr[high], arr[rand_idx] # Swap random to end
18 return lomuto_partition(arr, low, high)
19 
20def lomuto_partition(arr: list[int], low: int, high: int) -> int:
21 pivot = arr[high]
22 i = low - 1 # Boundary of elements <= pivot
23
24 for j in range(low, high):
25 if arr[j] <= pivot:
26 i += 1
27 arr[i], arr[j] = arr[j], arr[i]
28
29 # Place pivot in its correct final position
30 arr[i + 1], arr[high] = arr[high], arr[i + 1]
31 return i + 1

Modern JavaScript / TypeScript:

1export function quickSort(arr: number[], low: number = 0, high: number = arr.length - 1): void {
2 if (low < high) {
3 const pivotIdx = partition(arr, low, high);
4 quickSort(arr, low, pivotIdx - 1);
5 quickSort(arr, pivotIdx + 1, high);
6 }
7}
8 
9function partition(arr: number[], low: number, high: number): number {
10 const pivot = arr[high];
11 let i = low - 1;
12 
13 for (let j = low; j < high; j++) {
14 if (arr[j] <= pivot) {
15 i++;
16 [arr[i], arr[j]] = [arr[j], arr[i]];
17 }
18 }
19 
20 [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
21 return i + 1;
22}

5. Trace Table: Partitioning [10, 80, 30, 90, 40, 50, 70] (Pivot = 70)

jarr[j]arr[j] <= 70?iSwap ActionArray State
Init---1None[10, 80, 30, 90, 40, 50, 70]
010Yes0Swap arr[0] with arr[0][10, 80, 30, 90, 40, 50, 70]
180No0None[10, 80, 30, 90, 40, 50, 70]
230Yes1Swap arr[1] (80) with arr[2] (30)[10, 30, 80, 90, 40, 50, 70]
390No1None[10, 30, 80, 90, 40, 50, 70]
440Yes2Swap arr[2] (80) with arr[4] (40)[10, 30, 40, 90, 80, 50, 70]
550Yes3Swap arr[3] (90) with arr[5] (50)[10, 30, 40, 50, 80, 90, 70]
Final---Swap arr[4] (80) with pivot 70[10, 30, 40, 50, 70, 90, 80]

Pivot 70 is placed at index 4! All items to its left are $< 70$, all to its right are $> 70$.


Interactive Quiz• Knowledge Check

Multiple Choice Questions

Question 1
What is the average-case time complexity of Quick Sort?
Question 2
Under what input condition does naive Quick Sort (picking the first or last element as pivot) degrade to worst-case $O(n^2)$ time?
Question 3
How does "Randomized Quick Sort" mitigate the worst-case $O(n^2)$ scenario?
Question 4
Is standard in-place Quick Sort a "Stable" sorting algorithm?
Question 5
Why is Quick Sort faster in practical machine benchmarks than Merge Sort, even though both share $O(n \log n)$ average complexity?

Next Lesson

Binary Tree Traversals: DFS (Pre, In, Post) & BFS Level-Order

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Merge Sort: Divide-and-Conquer Paradigm & Stability AnalysisBinary Tree Traversals: DFS (Pre, In, Post) & BFS Level-Order

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Data Structures & Algorithms with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext