Yet another post for the crawlers to better index my site for algorithms and as a repository for Python code. The quick sort algorithm is well explained in the topmost Google search result for ‘*Quick Sort Python Code’*, but the code is unnecessarily convoluted. Instead, go with the code below.

In it, I assume the **pivot** to be the *first element*. You can easily add a function to randomize selection of the pivot. Choosing a random pivot minimizes the chance that you will encounter worst-case O(n^{2}) performance. Always choosing first or last would cause worst-case performance for nearly-sorted or nearly-reverse-sorted data.

*Also read:*

Computing Work Done (Total Pivot Comparisons) by Quick Sort

Karatsuba Multiplication Algorithm – Python Code

Merge Sort

### Like this:

Like Loading...

Hi, there is a bug in your code. When I use list = [18, 16, 17] to test you code, it returns [17, 16, 18].

LikeLike

Very very useful

Code is very easy to understand

Thank you

LikeLike