Skip to main content

Python Quick Sort

Here you get python quick sort program and algorithm.

Quick sort is based on divide and Conquer technique. We divide our array into sub-arrays and that sub-arrays divided into another sub-arrays and so on, until we get smaller arrays. Because it is easy to solve small arrays in compare to a large array.

Sorting smaller arrays will sort the entire array.

Python Quick Sort

To understand Quick Sort let’s  take an example:-

Example

We have an array [48,44,19,59,72,80,42,65,82,8,95,68]

First of all we take first element and place it at its proper place. We call this element Pivot element.

Note: We can take any element as Pivot element but for convenience the first element is taken as Pivot.

There are two condition to place Pivot at its proper place.

  • All the elements to the left of Pivot element should be smaller than
  • All the elements to the right of Pivot element should be greater than

In given array, we’ll take first element as Pivot element which is 48. Now place it at its proper place according to first two conditions.

Python Quick Sort 1

Here all the elements to the left is smaller and to right are greater than Pivot.

If you confused that how we placed Pivot at its proper place then wait we’ll discuss it later in partition algorithm.

Well, now we’ll take two sub-lists/sub-arrays left and right of our Pivot element (48).  Sub-lists will be

[42,44,19,8] and [80,72,65,82,59,95,68]

and will also take first element as Pivot element in both and place the their Pivot elements at their proper places using partition algorithm. After this step each of the sub-lists will be divided into two parts then new sub-lists will be divided into another two parts and so on until we reached the end.

Let’s see how it will look like.

Python Quick Sort 2

Pivot element represented by Green color.

We can write a recursive function for this procedure. There would be two terminating conditions. When the sub-list has only 1 element or when no sub-list is formed. If the value of low is equal to up then there will be only one element in the sub-list and if the value of low exceeds up then no sub-list is formed. So our algorithm will be.

Algorithm

Quick[array, low,up]

Where low and up are lower and upper bound of the array.

STEP 1:    if low>=up then return

STEP 2:    set piv_loc = call partition(array,low,up)

                   [calling partition to find the location of pivot]

STEP 3:    call Quick(array,low,piv_loc-1)

                        [Calling Quick for left sub-list]

STEP 4:    call Quick(array,piv_loc+1, up)

                        [Calling Quick for right sub-list]

Partition [array, low, up]

This algorithm is to find the location of pivot element and return it.

STEP 1:  set i = low+1

Set j = up

Set pivot = array[low]

STEP 2:  while(i <= j)

{

While(( array[i] < pivot ) and ( i < up ))

Increase i by 1

While ( array[j] > pivot )

Decrease j by 1

If ( i < j )

{

Swap the value of array[i] with array[j]

Increase i by 1

Decrease j by 1

}

Else

Increase i by 1

}

STEP 3:    set array[low] = array[j]

Set array[j] = pivot

Return j

Time Complexity

Best Case: O (n log2n)

Average Case: O (n log n)

Worst Case: O (n2)

Program for Quick Sort in Python

Array = [48,44,19,59,72,80,42,65,82,8,95,68]
low = 0
up = len(Array) - 1

def partition(Array,low,up):
        i = low+1
        j = up
        pivot = Array[low]
        while(i<=j):
                while(Array[i]<pivot and i<up):
                        i = i+1
                while(Array[j]>pivot):
                        j = j-1
                if(i<j):
                        Array[i],Array[j] = Array[j],Array[i]
                        i = i+1
                        j = j-1
                else:
                        i = i+1
        Array[low] = Array[j]
        Array[j] = pivot
        return j

def quick(Array,low,up):
        if(low>=up):
                return
        piv_loc = partition(Array,low,up)
        quick(Array,low,piv_loc-1)
        quick(Array,piv_loc+1,up)

quick(Array,low,up)

for i in Array:
        print i,

Output

8 19 42 44 48 59 65 68 72 80 82 95

Comment below if you have doubts related to above python quicksort tutorial.

The post Python Quick Sort appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/12/python-quick-sort.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do