Quick Sort--powershell Version

Source: Internet
Author: User

Read on. Halle algorithm feeling series, continue to sublimate. The previous one was a bubble sort, summarizing the drawbacks of bubble sort at the end--time complexity O (n*n) is too large. This article is about quick sorting, and quick sorting can overcome the drawbacks of bubbling sorting in most cases (the worst case scenario is the same as the time complexity of the bubbling sort). Let's start with the idea and process of fast sequencing, which differs from the previous one from process to thought, and this time our thinking process is from thought to process

The idea of quick sequencing:

Using the idea of dichotomy, first select a base number (as an intermediate value) in the sub-array to be sorted, and a left initial bit (the left endpoint bit of the subarray to be sorted). If the entire array is sorted, the left endpoint is an array of 0 index bits, and a right initial bit (the right endpoint bit of the subarray to be sorted). If the entire array is sorted, the right endpoint bit is the index bit at the length-1 of the group, and then a position is found where the number to the left of the position is less than the base number and the number on the right is greater than the base number. On this basis, treats the sorted sub-array to make two points, respectively, the base number in the sub-array to be sorted to the left, the left initial bit to the right of the grandson Array 1, and the base number to be sorted in the sub-array to right, right initial bit to the left of the grandson Array 2. After recursion, the grandchildren array 1 and 2 are sorted as the sub-array to sort.

The process of fast sequencing (and the idea in the previous article is the opposite of the abstraction and summary of the process, which is the concrete and the implementation of the idea):

1, select the sub-array to be sorted and its base number, left initial bit, right initial bit (if the left initial bit is less than the right initial bit to continue, otherwise stop);

2, find the base number of the exchange position, in the left initial and the right initial position to arrange a sentinel, first the right sentry left, until the first to find the number of a smaller than the base number; then the left sentry goes right until it finds the first number that is larger than the base number. Swap the values at the position of the left and right Sentinel, and then the Sentinel continues to move forward (first and left), continuing the exchange until the Sentinel meets before it stops. At this point the first round of the end of the order, the left Sentinel to the left is less than equal to the baseline number, right Sentinel to the right is greater than the baseline number;

3, the exchange of the Left Sentinel and the number of reference numbers, so that the base number to the left is less than equal to the base number, the base number of the right is greater than or equal to the base number;

4, in the position of the base number is treated as a sub-array of two points, divided into grandchildren array 1 and 2;

5, to the grandson Array 1 and 2 recursion, back to the first step, the left initial bit of the array 1 is the left initial bit of the subarray to be sorted and still the number of the left initial bit is the new base number, the right initial bit is the base number-1, the left initial bit of the grandson Array 2 is the base digit +1 and the new number of digits in the left initial bit, The right initial bit is the right initial bit of the sub-array to be sorted.

This sort is over when the left initial bit of the grandson Array 2 meets the right initial bit of the grandson Array 1. The entire array of sub-arrays will be sorted from small to large.

Next we will convert the specific process into code:

#Exchange the value.functionExchangevalue ($i,$j,$studentScore,$studentName){    #Exchange the score.    $s=$studentScore[$i]     $studentScore[$i] =$studentScore[$j]    $studentScore[$j] =$s    #Exchange the name.    $n=$studentName[$i]     $studentName[$i] =$studentName[$j]    $studentName[$j] =$n}functionSortscores ($left,$right,$studentScore,$studentName){    if ($left-le $right) {#Sort begin.        #the $left is base number, the $left and $right also represents the location of the sentries ' start points.         $i=$left        $j=$right        #The right sentry must go first every time and the sentries not meet each other.        While ($i-ne $j) {  while(($studentScore[$j]-ge $studentScore[$left])-and($j -GT $i))            {                $j--            }            #And then the left one .             while(($studentScore[$i]-le $studentScore[$left])-and($i -lt $j))            {                $i++            }            #If The left entry doesn ' t meet the right entry, exchange the left and right number.            If($i -lt $j) {Exchangevalue$i $j $studentScore $studentName            }        }        #Exchange The value from the locations of the left entry and the base number.Exchangevalue$left $i $studentScore $studentName        #Now "$i" are the new location of the base number.        #Sort The new left part.        $newRight=$i-1$newLeft=$i+1Sortscores$left $newRight $studentScore $studentNameSortscores$newLeft $right $studentScore $studentName    }}functionPrintsortedscores ($count,$studentScore,$studentName){    #Print the sorted result.Write-host"Below is the sorted result:"     for($i= 0;$i -le $count-1;$i++)    {        $j=$i+1$tip="NO."+$j+":"+$studentName[$i]+", Score:"+$studentScore[$i] Write-host$tip-foregroundcolor Green}}#This is the entry of the .#Collect the students ' scores.$count= Read-host"Enter The amout number of the students"#Convert to int type.$count= [INT]$count$studentName= new-Object System.Collections.ArrayList$studentScore= new-Object System.Collections.ArrayList for($i= 1;$i -le $count;$i++){    $name= Read-host"Enter the name"    $score= Read-host"Enter the score"    #Convert to int type.    $score= [float]$score    $studentName. ADD ($name)    $studentScore. ADD ($score)}$leftNum= 0$rightNum=$count-1Sortscores$leftNum $rightNum $studentScore $studentNamePrintsortedscores$count $studentScore $studentName

The function sortscores is the core of the fast sorting algorithm, in which the key judgment is highlighted in Orange .

Run this code in PowerShell and enter some test data, with the following results:

The exchange distance of the bubble sort is adjacent to two elements, and the quick sort in the left and right Sentinel is not adjacent, so the exchange of two elements is not adjacent, the worst case, around the Sentinel when the exchange will be exchanged for adjacent two elements. If every time is the worst case (each time the Sentinel is adjacent to each other), the time complexity of the quick sort is the same as the bubble sort. But as long as it is not the worst case, the time complexity of the quick sort is less than the bubble sort, and its average time complexity is O (NLOGN).

Quick Sort--powershell Version

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.