Eight Sort (c) ------- Quick Sort

Quick sort introduction:

Quick sort (Quicksort) is an improvement on the bubble sort. The basic idea is: a trip by ordering the data to be sorted into two independent parts, the part where all the data than the other part of all data to be small, then this method of data quickly sort the two parts separately, the entire sorting process can be recursively in order to achieve the entire data becomes an ordered sequence

 

 

 

 

 

 

 

 

The ability to feel not enough to be a problem, or algorithms to explain clearly enough, just on the basis of someone else's understanding, to understand the meaning of the code, to know the process execution, knowing the principle, but to tell myself, or depth say its underlying principles, can not do so many things rough and vague, forgive me

So this is my reference to the following someone else's explanation, forgotten sources

 

Suppose we now "61279345108" is the number of sort 10. First, just to find a number in this sequence as a reference number (do not be scared term is used to refer to a number, you know it will be used in the Zuosha). For convenience, let the first number as the reference number 6 bar. Next, all the sequence larger than the reference count on the right side need to 6, the reference number smaller than the number 6 on the left, similar to this arrangement.
 
       31254697108
 
        in the initial state, the bit number 6 of the first sequence. Our goal is to 6 moved to somewhere in the middle of the sequence, assuming that this position is k. Now we need to find the k, and the k-th bit to the demarcation point, the number is less than or equal to 6 on the left, the right number greater than or equal to 6. Think about it, there are ways you can do it?
 
        Give you a hint of it. Recall that bubble sort, is how "swap" step by step so that each number homing. At this point you can also by means of "exchange" to achieve their goals. Step by step exactly how to exchange it? How to exchange only convenient and save time? Do not rush to look down, he took out a pen, drawing on paper look. First learning bubble sort algorithm when my high school, I felt the bubble sort is a waste of time, each time only to compare two numbers adjacent, which is obviously too unreasonable. So I thought of a way, and later I realized that this is the "quick sort", please allow me a little narcissistic (^ o ^).
 
        The method is simple: start from both ends "probe" from the initial sequence "61279345108." First from right to left to find a number of less than 6, and then from left to right to find a number greater than 6, then swap them. Here two variables i and j, respectively, the leftmost and rightmost point sequence. We played a good name "Sentinel i" and "Sentinel j" for these two variables. At first i let the sentry point to the left-most sequences (ie, i = 1), point number 6. Let j Sentinel rightmost point sequence (i.e., j = 10), 8 point numbers.
 
       First Sentinel j started to take action. Because the reference number set here is the leftmost number, you need to let sentry j first out, it is very important (please think about why). Sentinel j move step by step to the left (ie j--), until you find a number less than 6 of stop. Then again i Sentinel move step by step to the right (ie, i ++), until it finds a number greater than the number 6 to stop. Finally Sentinel j parked in front of the numbers 5, Sentinel i stopped in front of number 7.
        Sentinel now exchange value i and j Sentry pointed elements. After the exchange of the following sequence.
        61259347108
 
        to this, the first end of the exchange. Then start sentinel j continue to move to the left (Friendly reminder again, each must first start sentinel j). He found that after four (baseline to 6 small to meet the requirements ratio) stopped. After Sentinel i also move further to the right, he found a 9 (larger than the reference number 6, to meet the requirements) stopped. At this exchange again, after the exchange of the following sequence.
        61254397108
 
        end of the second exchange, "probe" to continue. Sentinel j continue to move to the left, he found a 3 (6 less than the reference number, to meet the requirements) and later stopped. Sentinel i continue to move to the right, Goodness me! At this time, Sentinel and Sentinel j i met, Sentinel and Sentinel j i walked in front of all 3. Description at this time "detection" end. We will reference number 6 and 3 are exchanged. After the exchange of the following sequence.
        31254697108
 
        this first round "probe" real end. At this time, the reference number 6 is the demarcation point, the number 6 on the left are the right number of 6,6 or less is greater than or equal to 6. Just look at the process, in fact Sentinel j's mission is to find less than the reference count, and sentinel i's mission is to find the reference count is greater than, until i and j meet so far.
 
        OK, interpretation is completed. 6 benchmark has now homing, it is just in the first six sequence. At this point we have the original sequence to 6 as a boundary point split into two sequences, the sequences left "31254", the right sequence is "97,108." Next, also need to address these two sequences. 6 Because the left and right of the sequence currently still very confusing. But it does not matter, we have mastered the method, just as long as the method for simulating the next sequence to 6 left and right separately. Now the first sequence to handle 6 to the left of it now.
 
        The sequence left "31,254." Please reference the sequence number 3 is adjusted such that the number 3 is less than the left to the right are equal to the number 3,3 3 or greater. Well I started writing it.
 
        If you are modeling is not their fault, order of the sequence after the adjustment is completed should be.
        3. 4. 5. 1 2
 
        the OK, now has three homing. Next, the processing sequence requires 3 to the left of "21" and the right sequence of "54." Sequence "21" as a reference to adjust the number 2, after the end of the processing sequence is "12", this has 2 homing. Sequence "1" is only a number, you do not need any treatment. At this point we sequence "21" have been processed to obtain sequence is "12." Sequence "54" is performed also modeled this method, the resulting sequence is as follows.
        12345697108
 
        for the sequence "97108" is also just simulation procedure until an inseparable until a new sequence. The final sequence will be such as follows.
        12345678910
 
        this sort completely finished. Observant students may have found that every round of rapid processing is actually sort of a benchmark of these homing, until all the numbers are the property of bit far, sorting is over. FIG previous vested process will be described below under the overall algorithm.
 
 
        The quick sort of faster, because compared to bubble sort, each exchange is leaps and bounds. Every time when sorting a set reference point, the reference point will be less than equal to the number of all placed on the left of the reference point, the reference point will be greater than the number equal to the total reference into the right point. So every time the exchange will not be the same as the bubble sort can only be exchanged between adjacent numbers from the exchange of the big more. Therefore, the total number of comparison and exchange have less natural speed would be increased. Of course, in the worst case, it may still be number two adjacent were exchanged. So the worst time complexity of quick sort and bubble sort is the same is O (N2), its average time complexity of O (NlogN). In fact, quick sort it is based on something called the "binary" thinking.

 This is the c language version of the source code

#include <stdio.h>
 int A [ 101 ], n-; // define global variables, these variables need to be used in the subroutine 
void quicksort ( int left, int right)
{
    int i, J, T, TEMP;
     IF (left> right)
        return ; // when the value i becomes the left, end of the first recursive conditions are on the left, when there is only one number, an incoming itself recursive left ,
         // right incoming i-1, this time is equal right to left, left after subtraction is less than a sure, in this case the end of the recursive function return. This condition is to determine only the end of a case where the number of sorting
         // left passed i + 1, is equal to the right of the left at this time, right after adding a certainly larger than, at this time the end of this return recursive function. This condition is a case where only the determination of the end of the next number priority 
       
    TEMP = A [left]; // TEMP is stored in the reference number 
    I = left;
    j=right;
    while(i!=j)
    {
                   // order is important, first started looking from the right 
                   the while (A [J]> = TEMP && I < J)
                            J - ;
                    // find the left of 
                   the while (A [I] <= TEMP && I < J)
                            I ++ ;
                    // exchange number two position in the array of 
                   IF (I < J)
                   {
                            t=a[i];
                            a[i]=a[j];
                            a[j]=t;
                   }
    }
    // will eventually baseline homing homing only remember the reference number and i and j about has nothing to do 
    A [left] = A [i];
    a[i]=temp;
    
    quicksort (left, I - . 1 ); // continue processing the left, here is a process of recursive 
    quicksort (I + . 1 , right); // continue processing the right, there is a recursive process 
}
 int main ()
{
    int i,j,t;
    //读入数据
    scanf("%d",&n);
    for(i=1;i<=n;i++)
                   scanf("%d",&a[i]);
    quicksort ( 1 , the n-); // quick sort call
    
    // outputting the sorted results 
    for (I = . 1 ; I <= n-; I ++ )
        printf("%d ",a[i]);
    getchar();getchar();
    return 0;
}

java version of the source code

 java code

public class QuickSort {

        public static void main(String[] args) {
                //int[] arr = {-9,78,0,23,-567,70, -1,900, 4561};
                
                
                quickSort(arr, 0, arr.length-1);
                
        }

        public  static  void QUICKSORT ( int [] ARR, int left, int right) {
                 int L = left; // lower left subscript 
                int R & lt = right; // lower right superscript
                 // Pivot axis value 
                int Pivot ARR = [(left + right) / 2 ];
                 int TEMP = 0; // temporary variables, as exchange
                 // object while loop is to the left into a value smaller than the pivot
                 // than the value larger pivot into the right 
                while (L < R & lt) { 
                         // has been looking at the left side of the pivot, pivot value greater than or equal to find, just quit 
                        while( arr[l] < pivot) {
                                l += 1;
                        }
                        // been looking for the right pivot, the pivot value less than or equal to find, just exit 
                        the while (arr [r]> pivot) {
                                r -= 1;
                        }
                        // If l> = r explained value of about two pivot has been left in accordance with all
                         @ less pivot value, all the right is greater than or equal pivot values 
                        IF (L> = R & lt) {
                                 BREAK ;
                        }
                        
                        // exchange 
                        TEMP = ARR [L];
                        arr[l] = arr[r];
                        arr[r] = temp;
                        
                        // If the exchange was finished, it was found that arr [l] == pivot r-- values are equal, forward 
                        IF (ARR [L] == Pivot) {
                                r -= 1;
                        }
                        // If the exchange was finished, it was found that equal l ++ arr [r] == pivot value, backward 
                        IF (ARR [R & lt] == Pivot) {
                                l += 1;
                        }
                }
                
                // if l == r, must l ++, r--, or stack overflow occurs 
                IF (L == r) {
                        l += 1;
                        r -= 1;
                }
                // left recursive 
                IF (left < R & lt) {
                        quickSort(arr, left, r);
                }
                // right recursive 
                IF (right> L) {
                        quickSort(arr, l, right);
                }
                
                
        }
}
c language
#include <stdio.h>
 int A [101], n-; // define global variables, these variables need to be used in the subroutine 
void quicksort ( int left, int right)
{
    int i, J, T, TEMP;
     IF (left> right)
        return ; // when the value i becomes the left, end of the first recursive conditions are on the left, when there is only one number, an incoming itself recursive left ,
         // right incoming i-1, this time is equal right to left, left after subtraction is less than a sure, in this case the end of the recursive function return. This condition is to determine only the end of a case where the number of sorting
         // left passed i + 1, is equal to the right of the left at this time, right after adding a certainly larger than, at this time the end of this return recursive function. This condition is a case where only the determination of the end of the next number priority 
       
    TEMP = A [left]; // TEMP is stored in the reference number 
    I = left;
    j=right;
    while(i!=j)
    {
                   // order is important, first started looking from the right 
                   the while (A [J]> = TEMP && I < J)
                            J - ;
                    // find the left of 
                   the while (A [I] <= TEMP && I < J)
                            I ++ ;
                    // exchange number two position in the array of 
                   IF (I < J)
                   {
                            t=a[i];
                            a[i]=a[j];
                            a[j]=t;
                   }
    }
    // will eventually baseline homing homing only remember the reference number and i and j about has nothing to do 
    A [left] = A [i];
    a[i]=temp;
    
    quicksort (left, I -1); // continue processing the left, here is a process of recursive 
    quicksort (I +. 1, right); // continue processing the right, there is a recursive process 
}
 int main ()
{
    int i,j,t;
    //读入数据
    scanf("%d",&n);
    for(i=1;i<=n;i++)
                   scanf("%d",&a[i]);
    quicksort ( 1, the n-); // quick sort call
    
    // outputting the sorted results 
    for (I =. 1; I <= n-; I ++ )
        printf("%d ",a[i]);
    getchar();getchar();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/cnng/p/12344934.html