Python Eight sort Quick sort

Source: Internet
Author: User

A, Quick Sort

1) Set two variables I, J, when sorting begins: I=0,j=n-1;2) takes the first array element as the key data, assigns the value to Key, i.e. Key=A[0];3) forward search from J, that is, after the start of a forward search (j--), find the first less than KeyValue a[j], a[j] and A[i] interchange, 4) from I start backward search, that is, to start backward search (i++), to find the first one greater than KeyA[i], will a[i] and A[j] interchange;

5) Repeat 3rd, 4, until i=j, (3,4 step, did not find the matching criteria, that is, 3 a[j] is not less than key, 4 A[i] is not larger than the time of the key change J, I value, so j=j-1,i=i+1, until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).

As a list:

  LST =[6,1,2,7,9,3,4,5,10,8]

[6, 1, 2, 7, 9, 3, 4, 5, 10, 8]
Start right-to-left to move the first time
When J moves to 5 this time lst[j] is less than key6
I j Value Interchange
[5, 1, 2, 7, 9, 3, 4, 6, 10, 8]
Start moving from left to right for the first time
When I moves to 7 this time lst[i] is greater than key6
I j Value Interchange
[5, 1, 2, 6, 9, 3, 4, 7, 10, 8]
Start from right to left to move the second time
When J moves to 4 this time lst[j] is less than key6
I j Value Interchange
[5, 1, 2, 4, 9, 3, 6, 7, 10, 8]
Move from left to right to start the second time
When I moves to 9 this time lst[i] is greater than key6
I j Value Interchange
[5, 1, 2, 4, 6, 3, 9, 7, 10, 8]
Move the third time from right to left
When J moves to 3 this time lst[j] is less than key6
I j Value Interchange
[5, 1, 2, 4, 3, 6, 9, 7, 10, 8]
At this point 6 is in the middle.

LST is divided into two parts

Left [5, 1, 2, 4, 3]

Right [9, 7, 10, 8]

Re-follow the just sort method

Will get results
Left [3, 1, 2, 4, 5]

Right [8, 7, 9, 10]

Continue sorting

..... (recursive)

Implementation method:

Process oriented:

#-*-CODING:GBK-*-__author__='Hzq'LST= [6,1,2,7,9,3,4,5,10,8]i=0j=len (LST)-1Key=Lst[0]Print(LST)Print("start right-to-left to move the first time") whileLst[j]>=key andJ>i:j-=1Print("when J moves to {} at this time lst[j] is less than key{}". Format (lst[j],key))Print("I J Value Interchange") Lst[i],lst[j]=Lst[j],lst[i]Print(LST)Print("start moving from left to right for the first time") whileLst[i]<=key andJ>i:i+=1Print("when I moves to {} at this time lst[i] is greater than key{}". Format (lst[i],key))Print("I J Value Interchange") Lst[i],lst[j]=Lst[j],lst[i]Print(LST)Print("start from right to left to move the second time") whileLst[j]>=key andJ>i:j-=1Print("when J moves to {} at this time lst[j] is less than key{}". Format (lst[j],key))Print("I J Value Interchange") Lst[i],lst[j]=Lst[j],lst[i]Print(LST)Print("move from left to right to start the second time") whileLst[i]<=key andJ>i:i+=1Print("when I moves to {} at this time lst[i] is greater than key{}". Format (lst[i],key))Print("I J Value Interchange") Lst[i],lst[j]=Lst[j],lst[i]Print(LST)Print("move the third time from right to left") whileLst[j]>=key andJ>i:j-=1Print("when J moves to {} at this time lst[j] is less than key{}". Format (lst[j],key))Print("I J Value Interchange") Lst[i],lst[j]=Lst[j],lst[i]Print(LST)

Object-oriented:

#-*-CODING:GBK-*-__author__='Hzq'LST= [6,1,2,7,9,3,4,5,10,8]defMinddle_index (Lst,low,high): Start_index=low#far leftEnd_index=high#left and right side    ifStart_index<end_index:#precondition right greater than leftkey=Lst[start_index] #设置参照key whileEnd_index>Start_index: whileLst[end_index]>=key andEnd_index>Start_index:end_index-=1Lst[start_index],lst[end_index]=Lst[end_index],lst[start_index] whileLst[start_index]<=key andEnd_index>Start_index:start_index+=1Lst[start_index],lst[end_index]=Lst[end_index],lst[start_index]#Loop Sort, recursiveMinddle_index (lst,low,start_index-1)#left lst sorted againMinddle_index (Lst,end_index+1,high)#right lst again sortMinddle_index (Lst,0,len (LST)-1)Print(LST)

Python Eight sort Quick sort

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.