Now that you have formed an understanding of how quicksort works, let’s try to sort the same six cards we have been using until now using Quicksort.
So this time we have another random list which we need to sort in ascending order but using Quicksort. So for Quicksort we'll take a random number and call it the pivot. In our example, let's take six as the pivot. Now we simply compare six with each number in the list. And if that number is smaller than six, we place it to the left of six. We compare five with the six. Since five is smaller, we place it to the left of six. Next we compare eight. Since eight is bigger than six, we place it to the right. Three is again smaller than six, so we place it to the left. Similarly, four is smaller, so we place it to the left. And finally seven is bigger than six. It gets placed to the right. Now we know that six is on the correct position in the entire list. Now we'll focus on all the numbers just to the left of six and ignore these for now, we'll come back to them later. Now again we'll take a random pivot. Let's take five as the pivot this time. So we have five. Now we compare five with each number in the list. We compare five with the three. Since three is smaller than five, we place it to the left of five. We compare four with the five, and again it's lesser. So we place it to the left. So we also know that five is in the correct position. Now we'll focus on the numbers to the left of five. We'll again take a random pivot. Let's take four as the pivot. Since three is smaller than four, we'll place it to the left. And now we have these numbers sorted. Now we know six was already on the correct position. So we'll focus on the numbers to the right of six, the eight and the seven. We'll again take a random pivot. Let's take seven for instance and compare all numbers to seven. Since we have eight, we compare seven with eight and understand that eight is bigger. So we will place eight to the right of seven. And since we only have two numbers, we now know that these are also sorted. We place them back and we have a sorted array using Quicksort.
"How to Sort a Random List in Ascending Order Using Quicksort"
Choose a random number from the list and call it the pivot.
Compare each number in the list with the pivot.
If the number is smaller than the pivot, place it to the left of the pivot. If it's bigger, place it to the right.
The pivot is now in the correct position in the entire list.
Ignore the numbers to the right of the pivot and focus on the numbers to the left.
Choose a new pivot from the remaining numbers and repeat the process until all the numbers are sorted.
Place the sorted numbers back into the original list.
Let us now try to understand the practical implications of an algorithm being of the O(n), O(), or O(n*logn). Aishwarya will demonstrate through a simple mathematical example as to how time complexities actually affect the time taken by an algorithm. We will try to see that it makes a huge difference if two different algorithms are run having O(n) or in O(nlogn) time complexity.
Great! We are finally done with understanding thoroughly how Quicksort works, seen its Java implementation and also discussed its time complexity. It is now time for us to compare the various sorting algorithms we have learnt so far. Let us know more about it in the next segment.