Fast sorting and its improved algorithm C + + implementation __web

Source: Internet
Author: User
Tags rand

Algorithm Description:

A quick sort can be seen as an improvement of the insertion sort, which is a sort of divide-and-conquer algorithm, which differs from the insertion sort in that the quick sort is the key word that controls the partitioning process directly in the final position, not in the right place. The idea is: for element ki to place it in the final position (that is, all the elements on the left are less than or equal to it, the right element is greater than equal to it), it divides the original array into two parts, and then recursively solves a left and a sub array.

The following gives a quick sort of integer array implemented by C + +:

#include <iostream> #include <math.h> using namespace std;
const int NUM=20;
        void Exch (int* s,int a,int b) {int mid=s[a];
        S[A]=S[B];
S[b]=mid;
                int qsort (int* s,int low,int up)/Quick Sort {if (low<up) {int key=s[low];
                int i=low;
                int j=up;
                up++;
                        while (low<up) {low++;
                        while (Low<up&&s[low]<=key) low++;
                        up--;
                        while (Low<=up&&s[up]>=key) up--;
                if (low<up) Exch (s,low,up);
                } Exch (S,i,up);
                Qsort (s,i,up-1);
        Qsort (S,UP+1,J);
else return 0; int main () {int array[num]; Srand (2);//Initialize random number for (int i=0;i<num;i++)//Initialize array 0 to 100between {Array[i]=rand ()%;} for (int i=0;i<num;i++)//Output the original array {printf ("%d", Array[i]);}
Qsort (array,0,num-1);
printf ("\ n");
    for (int i=0;i<num;i++) {printf ("%d", array[i]);
return 0;
 }


Several improvement schemes for fast sorting:

1: When the size of the array drops to a certain time switch to the insertion sort (for decimal groups, the insertion sort efficiency is higher generally take 5~15 between).

C + + implementation:

#include <iostream> #include <math.h> using namespace std;
const int NUM=50;
        const int scale=8;//decimal Group threshold void Exch (int* s,int a,int B)/exchange {int mid=s[a];
        S[A]=S[B];
S[b]=mid;
        } void Move (int* s,int a,int B)//moves {int mid=s[a];
        for (int i=a;i>b;i--) {s[i]=s[i-1];
} S[b]=mid; } void Insertsort (int* s,int low,int up)/insert sort {for (int i=low+1;i<=up;i++) {for (int J
                =low;j<i;j++) {if (S[i]<s[j]) move (S,I,J);
            Qsort (int* s,int low,int up)//fast Sort {if (Up<=low+scale)//is a decimal group with insert sort {
            Insertsort (S,low,up);
        return 0;
                else {int Key=s[low];
                int i=low;
                int j=up;
                up++;
                  while (Low<up) {      low++;
                        while (Low<up&&s[low]<=key) low++;
                        up--;
                        while (Low<=up&&s[up]>=key) up--;
                if (low<up) Exch (s,low,up);
                } Exch (S,i,up);
                Qsort (s,i,up-1);
        Qsort (S,UP+1,J);
        int main () {int array[num];
        Srand (2);//Initialize random number for (int i=0;i<num;i++)//Initialize array 0 to 100 {array[i]=rand ()% 101;
        for (int i=0;i<num;i++)//outputs the original array {printf ("%d", array[i]);
        } qsort (array,0,num-1);
        printf ("\ n");
        for (int i=0;i<num;i++) {printf ("%d", array[i]);
return 0;
 }


2: Three sampling segmentation. For fast sorting, if the segmentation is unbalanced, it will be inefficient. So "three-sample segmentation" means to use the median of a subset of the elements to cut the fractional group to ensure the relative equilibrium of the segmentation. There is no implementation here.

3: The optimal sort of entropy.

For a sorted array with a large number of duplicates, if the original quick sort is used, even if the entire array of elements is duplicated, the algorithm can cut the array into three blocks, less than, equal to, and greater than the sub array of the Shard element.

C + + implementation:

#include <iostream> #include <math.h> using namespace std;
const int NUM=50;
        void Exch (int* s,int a,int b) {int mid=s[a];
        S[A]=S[B];
S[b]=mid;
        int qsort (int* s,int low,int up)/Quick Sort {if (Up<=low) return 0;
                else {int Key=s[low];
                int i=low+1;
                int gt=up;
                int lt=low; while (I&LT;=GT) {if (s[i]<key) Exch (s,lt++,i++)
                        ;
                        else if (s[i]>key) Exch (s,i,gt--);
                else i++;
                } qsort (s,low,lt-1);
        Qsort (S,gt+1,up);
        int main () {int array[num];
        Srand (7);//Initialize random number for (int i=0;i<num;i++)//Initialize array 0 to 100 {array[i]=rand ()% 101;
 for (int i=0;i<num;i++)//output original Array {               printf ("%d", array[i]);
        } qsort (array,0,num-1);
        printf ("\ n");
        for (int i=0;i<num;i++) {printf ("%d", array[i]);
return 0;
 }



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.