Python algorithm-Quick sort

Source: Internet
Author: User
Tags benchmark

Quick sort:

To learn a quick sort, you should first review the following recursion:

2 Conditions for recursion:

1. Function calls itself

2. There is an exit condition

Exercise: Calculate n! based on recursive next function and the value of n equals 10 is calculated.

N! =n * n-1*.....*1

#enconding = Utf-8

def func (N):

If n<=1:

Return 1

Else

Return N*func (n-1)

Print func (10)

Results: 3628800

Quick sort: Also based on recursive thinking

Core idea: For an array 12 23 3 4 56 21

The quick row is a random selection from the inside of a number, such as 21, all the lower than the number of the row on its left, all the number greater than its row on the right.

Use pointer to indicate position, traverse array:j-> 0 to 4

I means that the left side of subscript I is less than 21, including subscript I

The initial value i=-1-1 is the extreme case where the minimum number is exactly the last one

The initial value j=0, J controls the subscript of the traversal array.

Using J to compare with 21, if greater than 21, then the void is not processed, if less than 21:i to add 1, I and J point to the element exchange position

Manual sorting:

I=-1 j=0

Remove the 12,12<21-ài+1=0;i and J points of the element exchange position, at this point i=j=0, are pointing to lista[0];j++

12 23 3 4 56 21

I=0,j=1

Remove 23,21 23>21, empty

12 23 3 4 56 21

I=0, j=2

Remove 3,21, 3<21-> i+1=1;i and J corresponding element position exchange, lista[1],lista[2] =

12 3 23 4 56 21

I=1 j=3

Remove 4<21,i+1=2; I and J corresponding element position Exchange

12 3 4 23 56 21

I=1;j=4

Take out 56>21; empty.

12 3 4 23 56 21

Swap the elements of the subscript i+1 and the last element for a position

12 3 4 21 56 23

This completes the first round of sorting.

Why choose the last number?

It's easy to find. In fact, whichever final result is the same. So for the sake of simplicity we directly take the last number as the benchmark number.

Next, let's write out the results of 12 3 4 and right sub-list 56 23 after a quick row

Left: 3 4 12

Right: 23 56

Quick schedule:

1. For Listx call quick-row

2. For Listx left sub-list 12 3 4 for fast row, for Listx right sub-list 56 23

3. Until the list has only one element, exit

#enconding = Utf-8

def quick_sort (lista):

def pathsort (lista,sindex,eindex): #第一重排序

Flag = Lista[eindex]

i = SIndex-1

For j in Xrange (Sindex,eindex):

If Lista[j]<flag:

I+=1

LISTA[I],LISTA[J] = Lista[j],lista[i]

Else

Pass

LISTA[EINDEX],LISTA[I+1] = Lista[i+1],lista[eindex]

Return i+1

def qSort (Listb,sindex,eindex):

If Sindex >= eindex: #如果开始位置大于等于起始位置, return

Return

Middle = pathsort (Listb,sindex,eindex)

#递归调用左子列表

QSort (listb,sindex,middle-1)

#递归调用右子列表

QSort (Listb,middle+1,eindex)

QSort (Lista,0,len (Lista)-1)

return lista

if __name__ = = ' __main__ ':

Print Quick_sort ([12,3,4,21,56,23])

deformation Exercise 1 : modified to be arranged from large to small.

#enconding = Utf-8

def quick_sort (lista):

def pathsort (lista,sindex,eindex): #第一重排序

Flag = Lista[eindex]

i = SIndex-1

For j in Xrange (Sindex,eindex):

If Lista[j]>flag:

I+=1

LISTA[I],LISTA[J] = Lista[j],lista[i]

Else

Pass

LISTA[EINDEX],LISTA[I+1] = Lista[i+1],lista[eindex]

Return i+1

def qSort (Listb,sindex,eindex):

If Sindex >= eindex: #如果开始位置大于等于起始位置, return

Return

Middle = pathsort (Listb,sindex,eindex)

#递归调用左子列表

QSort (listb,sindex,middle-1)

#递归调用右子列表

QSort (Listb,middle+1,eindex)

QSort (Lista,0,len (Lista)-1)

return lista

if __name__ = = ' __main__ ':

Print Quick_sort ([12,3,4,21,56,23])

Complexity of Time:

First round: After finishing the fast row found that the benchmark number is the largest one, we compared the n-1 times, the last one

Second round: n-2 times

Third round: n-3 times

...

N-1 round: 1 times

1+2+3....+n-1 = N^2/2

Time Complexity of O (NLOGN)

Average situation:

N

The first round: n-1, arranges 2 lists, determines the position of 1 nodes

Second round: n-3, arranges 4 lists, determines the position of 3 nodes

Round three: n-7, arranging 8 lists to determine the position of 7 nodes

Fourth round: n-15, arranges 16 lists, determines the position of 15 nodes

.....

Average number of comparisons N-x

2^i-1

How many rounds do you need in total to complete the quick row?

2^1 + 2^2 +.....2^i-i = n

(1-2^i)/1-2–i =n

2^ (i+1) -2-i =n

i+1 ~ Logn

I ~ Logn

Log (N-x)

Final time complexity of O (NLOGN)

Python algorithm-Quick sort

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.