Quick sort algorithm in python | Python example | Python program
Quick Sort Algorithm in Python.
Quick Sort Algorithm is one of the fastest Sort algorithms.
How Quick Sort algorithm works.
1) First, we take a list.
7 | 1 | 10 | 2 | 11 |
2) Now we take a pivot element(selected any random element or first element or last element), I choose the first element.
0 1 2 3 4
1 | 10 | 2 | 11 |
-5 -4 -3 -2 -1
3) We will choose a 7 as a pivot element Now we will put the element left side who will less than and equal to pivot element(7). And put the element on the right side who is greater than the pivot element(7).
0 1 2 3 4
2 | 1 | 7 | 11 | 10 |
-5 -4 -3 -2 -1
4) Now we will cut the list or array into subarray 0 to1 and 3 to 4.
2 | 1 |
11 | 10 |
3) Now repeat this process in two subarrays.
Code until the one element is left.
1 | 2 | 7 | 10 | 11 |
4) And you get the result.
1 | 2 | 7 | 10 | 11 |
# Python program to sort the array by quicksort. |
Output:
If you have a better solution than me, then please share in comment or Email.
Nice bro