3-5 Review, quick dichotomy of doubts: why first right J move? Because setting A[left] is the datum point

Source: Internet
Author: User
Tags benchmark

quick dichotomy of doubt: why first right J move? Because setting A[left] is the base number

Ideas : (Viewing the 2 articles on the Internet, the key point is who is the benchmark number, I myself have tried different sorts, different benchmark number left,right. Come out?? 's conclusion)

Steps:

1. set A[left] as the base number in the sequence. Left set I, rightmost J (target is from large to small)

2. from right to left to find a number greater than the base number, and then from left to right to find the number less than the base number, find the second exchange position

3. Then repeat steps 2 and 3. Until I and J finally meet, end this cycle.

4. Exchange the number of benchmarks and final encounters and end them. This step locates a number.

5. Repeat the 1-4 steps for the left and right sequences of the original base number. Continue to position the new base number. Until all digital positioning is complete.

2nd step, move right first because the datum point is set to A[left]. If the left I move first to get the a[i] is necessarily smaller than a[left].

The 4th Exchange A[i] and A[left] caused the datum point A[left] to the right there is a smaller number than it. does not conform to the principle that each loop locates 1 datum points, from the big to the small sort will fail. ( Of course, if it is the same from small to large, it can cause similar problems.) )

So if the datum point is positioned to the far left, J will move first. Of course the datum point is positioned to the far right, I move first.

def quicksort (a,left,right) If left > Right # Sets when to stop the call to the method. Return end

temp = A[left]#设定最左边的为基准数i = left j= right while I! = J# I and J move from side to center until I and J coincide, ending this round cycle.

# Select the comparison operation symbol based on whether it is ascending or descending.

While A[j] <= temp && i < J#根据基准点是a [left], you should first look to the right. J-= 1 end while a[i] >= temp && i < J #再从左向右找, this is the number less than the base number.  i + = 1 End If I < J #当i and J do not meet, exchange data T = a[i] a[i] = a[j] a[j] = t End End A[left] = A[i]#将基准数归位. A[i] = Temp

Print a.to_s + "\ n" #打印每次交换的结果

Quicksort (a,left,i-1) # in the Quciksort method, call this method again to form a recursive process.                                       This is the left side of processing I.  # until exhausted, that is, stop calling the method when Left>right. Quicksort (a,i+1,right) # This processing I right end

A = [7, a,0,9, 1]quicksort, 0, 9,,,,,,,,,,,,,,,,,, and 9--the leftmost and rightmost key print a of the array

Results:

[86, 68, 42, 46, 9, 91, 77, 46,7, 1][91, the, 42, 46, 9, 68, 77, 46, 7, 1][,86, 42, 46, 9, 68, 77, 46, 7, 1][91, 86,,46, 46, 68, 42, 9, 7, 1][91, 86,,46, 46, 68, 42, 9, 7, 1][91, 86, 77,,46, 46, 42, 9, 7, 1][91, 86, 77, the, 46, 46, 42, 9, 7, 1][91, 86, 77, 68,,46, 42, 9, 7, 1][91, 86, 77, 68, $, 46, 42, 9, 7, 1][91, 86, 77, 68, 46, $, 42, 9, 7, 1][91, 86, 77, 68, 46,,9, 7, 1] #最终结果

3-5 Review, quick dichotomy of doubts: why first right J move? Because setting A[left] is the datum point

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.