This is a technique to sort a given array in an acending order.
Here is the steps we follow while performing quick sort :-
- The last element of array is assumed to be "infinity".
- " i " (which is an integer type variable) point to the lowest index and " j " (another integer type variable) points to the highest index of the array.
- First element is assinged asthe "pivot" element.
- " i " is incremented till larger or equal element than pivot is found.
- " j " is decremented till smaller or equal element then pivot is found.
- If " i " is less than " j " then interchange a[i] with a[j] and repeat the steps 4 and 5, else interchange a[j] and "pivot".
When the element (a[j]) is exchanged with "pivot" then the position of that element is fixed in the array and divides the array into two parts ie. on the right of the array all maximum elemnts will be there and on the left of the array all minimum elements will be there. Now sort the two parts in the same manner as of the parent array.
Now as we see that the no. of iterations will be " n log2(n )" since " 2x " times of comparison is made in one iteration and total number of iterations is "n", hence the total number of iterations will be " n X 2x " ie. " n log2(n) ".
Hence, the complexcity of this sorting technique is n log2(n).
" n " here is the size of the array.
Now we will discuss how did the term log2(n) came.
Since 2x iterations are made and in the worst case the "2x = n".
x = log2(n)
Where " log2 " refers to the base of logarithmic function ie. 2.
Hope when you read this article your understanding will we enhanced and you will be able to write the coding efficiently.
Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.