Quick sort is a divide and conquer algorithm. Its divided large list in mainly three parts:
1.Elements less than pivot element.
3.Elements greater than pivot element.
Where pivot as middle element of large list. Let’s understand through example:
List : 3 7 8 5 2 1 9 5 4
In above list assume 4 is pivot element so rewrite list as:
3 1 245 8 9 5 7
Here, I want to say that we set the pivot element(4) which has in left side elements are less than and right hand side elements are greater than. Now you think, how’s arrange the less than and greater than elements? Be patient, you get answer soon.
Now let’s start understand the concept of quick sort. The steps are:
1.Pick a pivot element.
2.Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partitionoperation.
3.Recursivelysort the sub-list of lesser elements and the sub-list of greater elements.
Thebase caseof the recursion are lists of size zero or one, which never need to be sorted