• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

QuickSort – Understanding the QuickSort Algorithm and Implementation

August 21, 2020 by varunshrivastava 1 Comment

QuickSort algorithm is a brilliant idea of Tony Hoare. This algorithm is so effective that if implemented well, it can be 2x or 3x faster than its competitors merge sort and heap sort.

I personally like quick sort algorithm because of its simplicity and speed. But I’m surprised to see that so many people get confused with quick sort algorithm.

But worry not, because I think I know the reason now 🙂

The reason people are scared of the QuickSort algorithm is that it involves recursion. And I totally agree, recursion is a bit difficult to grasp at first, especially if you are just starting off with the algorithms.

In this article, I’ve made an effort to explain the QuickSort algorithm in the easiest way I could.

Actually the way I’m going to explain to you is the way I’ve understood it. And trust me, once you understand the concept, coding becomes easy and once you are comfortable with the code then you can play around with it in your style.

If you are interested in programming then do visit bemyaficionado.com/programming to find more such articles.

QuickSort Single Iteration

Let’s take it real slow and see what happens in the very first iteration.

I’ve tried my best to generate the below HTML using print statements in the quicksort program. Just give it some time and carefully visit every step and try to understand what’s happening. This is the only way to learn (look under the hood).

And the first iteration is the crux of it all. If you understand the first iteration, you understand the quicksort. Okay, here it is…


Input Array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

10 9 8 7 6 5 4 3 2 1

First Iteration…

Start Index = 0, End Index = 10

i=0; j=0;
10 9 8 7 6 5 4 3 2 1

Let’s select a random index from the array and name it as the pivot.

Let’s say we select index = 1 as pivot

Pivot Index = 1

Value at Pivot Index = 9

i=0; j=0; pivot
10 9 8 7 6 5 4 3 2 1

Let’s put the selected pivot at the end of the array.

So the array now becomes this, Array = [10, 1, 8, 7, 6, 5, 4, 3, 2, 9]

pivot
10 1 8 7 6 5 4 3 2 9

So far so good… we haven’t begun with the iteration yet.

Now, let’s take two variables (say, i & j) and assign the starting index to them such that the value of i=0 and j=0

i=0; j=0; pivot
10 1 8 7 6 5 4 3 2 9

Now, let’s start the iteration.

Iterate until j=0 reaches the last index (i.e 9)

In the while loop, we will compare the value of arr[j] with the selected pivot value (9).

Let’s see how the value of j and i changes over the iteration.

i=0, j=0

Increment j by 1

new value of i is 0 and j is 1

i=0, j=1

i=0; j=1; pivot
10 1 8 7 6 5 4 3 2 9

value at j=1 is (1) is smaller than the pivot (9)

so we swap the values at index 0 with 1 and increment i by one

Increment j by 1

new value of i is 1 and j is 2

i=1; j=2; pivot
1 10 8 7 6 5 4 3 2 9

i=1, j=2

value at j=2 (8) is smaller than the pivot(9)

so we swap the values at index 1 with 2 and increment i by one

Increment j by 1

new value of i is 2 and j is 3

i=2; j=3; pivot
1 8 10 7 6 5 4 3 2 9

i=2, j=3

value at j=3 (7) is smaller than the pivot(9)

so we swap the values at index 2 with 3 and increment i by one

Increment j by 1

new value of i is 3 and j is 4

i=3, j=4

i=3; j=4; pivot
1 8 7 10 6 5 4 3 2 9

value at j=4 (6) is smaller than the pivot(9) so we swap the values at index 3 with 4 and increment i by one

Increment j by 1

new value of i is 4 and j is 5

i=4; j=5; pivot
1 8 7 6 10 5 4 3 2 9

i=4, j=5

value at j=5 (5) is smaller than the pivot(9)

so we swap the values at index 4 with 5 and increment i by one
Increment j by 1

new value of i is 5 and j is 6

i=5; j=6; pivot
1 8 7 6 5 10 4 3 2 9

i=5, j=6

value at j=6 (4) is smaller than the pivot(9) so we swap the values at index 5 with 6and increment i by one

Increment j by 1

new value of i is 6 and j is 7

i=6; j=7; pivot
1 8 7 6 5 4 10 3 2 9

i=6, j=7

value at j=7 (3) is smaller than the pivot(9) so we swap the values at index 6 with 7 and increment i by one

Increment j by 1

new value of i is 7 and j is 8

i=7; j=8; pivot
1 8 7 6 5 4 3 10 2 9

i=7, j=8

value at j=8 (2) is smaller than the pivot(9) so we swap the values at index 7 with 8 and increment i by one

Increment j by 1

the new value of i is 8 and j is 9

i=7; j=8; pivot
1 8 7 6 5 4 3 2 10 9

Finally j has reached the pivot, which means i is the correct index for the pivot.

So let’s swap them.

Now the pivot has reached its correct position that is at index 8 when the array is sorted in the ascending order.

i=7; j=8; pivot
1 8 7 6 5 4 3 2 9 10

In the first iteration, the pivot number 9 has reached its correct position in the array (if sorted ascending). Similarly, every iteration we pick one pivot and make sure it reaches its correct position.

Another way to look at this is that after the end of the iteration all the numbers that are smaller than the pivot are moved to its left and all the numbers greater than the pivot has moved to its right.

If we perform the same steps over and over again with different pivots, at the end we will have the sorted array. So, the best-case scenario would be O(nlogn). But the worst case could still be O(n2).

The worst case is very unlikely. For the worst case, you would have to be really unlucky to pick the bad pivot every time. And by bad I mean either you pick the pivot from the start or end.

Complete QuickSort Algorithm

Now that we have seen the first iteration, we just have to do it for all the numbers in the array.

The best way to do it for all the numbers is to recursively sort the left side of the array and the right side of the array.

So, in the above example, break the array into two logical parts e.g. index 0-7 and 9. Since the right array contains only one element which means it is already sorted. So, you will sort the left array and so on recursively.

Here’s the entire code for the same.

Conclusion

Now that you have understood how it works, analyze the above code, put print statements here and there and fit it in your brain. This algorithm is not just helpful in sorting the array but also very useful for solving nth order statistics.

Maybe in the next article, I will give you a glimpse of nth order statistics problem.

Let me know if you are facing any problem understanding the code. Comment below… that’s the fastest way to get an answer.

Related

Filed Under: Programming Tagged With: algorithms, elementary sorting algorithms, implementation, quick sort, quick sort implementation

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version