Fast sequencing--the realization of bilateral scanning and unilateral scanning

Source: Internet
Author: User

Quick sort time Complex read O (n*logn), Worst O (n^2), average O (N*LOGN)

The main idea is to choose a flag, greater than the sign to the right, less than the flag bit to the left, in the mark as a division, divided by the system, left recursion, right recursion, until completed.

The idea of quick sequencing (bilateral scanning)

Fast sorting is like a data fast, there is a subscript (pointer) i/j, randomly selected (here to remove the labeled 0) an element as the flag bit, stored in the temporary variable (TMP), J forward from the Back (j--) until the number is smaller than the TMP and I exchange, when I started like after walking, Until the first number that is larger than TMP is encountered, swap with J.

recursion until complete.


Fast sequencing ideas (single-sided scanning)

Sweep from left to right again, similar to bubble sort, just keep the value less than the flag bit to the left, greater than the right side, find the split position will be placed in the flag.

Single-sided scanning how to put less than the mark (subscript end) on the left side, greater than the flag position on the right side, the bilateral scanning is through the excavation of the way to bury the number of ways to achieve, one-sided scan can be recorded through a small flag the smallest number of consecutive, index has been moving forward to find the first discontinuous value of less than Swap with small++ (the first value greater than the flag bit) until the end of the loop, placing the value of the small++ at the subscript position of the flag end.

Note: If there are errors, hope to criticize, the algorithm is mainly thought, followed by the idea to achieve. bilateral int Partition2 (long *str,int start,int end,int length) {if (str = = NULL | | length <=1 | | start >end) return 0;int I =start;//flag bit long tmp = Str[start];int j=end;while (i!=j) {while (i<j && str[j]>tmp) j--;if (i<j) Swap (& Amp;str[j],&str[i++]), while (I<j && str[i]<tmp) i++;if (i<j) swap (&str[i],&str[j--]);} Swap (&str[i],&tmp); return i;} Single-sided scan int partition (long *str,int start,int end,int length) {if (str ==null | | length<=0 | | start<0 | | end >lengt               h) return;int I=start;int small = i-1;for (i;i<end;i++) {//adjust the position of small by contrast until the small position reaches the right-most data with a continuous less than the flag bit  if (Str[i]<str[end]) {++small;if (small! = i) swap (&str[i],&str[small]);}} ++small;swap (&str[small],&str[end]); return small;} Recursive invocation, implementing quick sort void QuickSort (long *str,int start,int end,int length) {if (str = = NULL | | length<1 | | start >end) return ;//int mid = Quick (str,start,end,length); int mid = partiTion2 (str,start,end,length); int i=0;for (i;i<length;i++) printf ("%ld\t", Str[i]);p rintf ("\ n"); QuickSort (str,start,mid-1,length); QuickSort ( Str,mid+1,end,length);}
Full code

Operating environment: Ubuntu 14.04 Kylin, GCC

#include <stdio.h> #include <malloc.h>void swap (long *a,long *b) {    long tmp;   & Nbsp;tmp = *a;    *a = *b;    *b = tmp;} int Partition2 (long *str,int start,int end,int length) {    if (str = = NULL | | length <=1 | | start >end )         return 0;    int i=start;    long tmp = Str[start] ;    int j=end;    while (i!=j) {        while (i<j && str[j]>tmp)             j--;         if (i<j)             swap (&str[j],&str[i++]);                     while (I<j & & str[i]<tmp)             i++;        if (i&LT;J)             swap (&str[i],&str[j--]);             }    swap (&str[i],&tmp);    return i;} int  partition (Long *str,int start,int end,int length) {    if (str ==null | | length<=0 | | start< 0 | | End >length)         return;    int i=start;    int small = i-1;    for (i;i<end;i++) {        if (Str[i]<str[end]) {             ++small;            if (Small! = i)                 swap (&str[i], &str[small]);        }            }     ++small;    swap (&str[small],&str[eND]);        return Small;} void QuickSort (Long *str,int start,int end,int length) {    if (str = = NULL | | length<1 | | start >end) & nbsp;       return;        //int mid = Quick (Str,start,end, Length);        int mid = Partition2 (str,start,end,length);     int i=0;    for (i;i<length;i++)         printf ("% Ld\t ", Str[i]);    printf (" \ n ");        quicksort (str,start,mid-1, Length);            quicksort (str,mid+1,end,length);} int genrand (int num,long * array) {    if (num>10000)         return 0; &n bsp;   srand ((unsigned int) time (0));    int i=0;    for (i=0;i<num;i++)         array[i] = rand ();     return 1;    }void Main () {    int num = 10;    long *array= ( Long *) malloc (sizeof (long) *num);     Genrand (num,array);         int i=0 ;    /*for (i=0;i<num;i++)         printf ("%ld\n", Array[i]);     */    quicksort (array,0,9,10);    printf ("\ n");         for (i=0;i<num;i++)         printf ("%ld\n", Array[i]);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Fast sequencing--the realization of bilateral scanning and unilateral scanning

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.