[Algorithm] fast sorting

Source: Internet
Author: User

Fast sorting is also based on the sharding mode like Merge Sorting, which divides large problems into several small problems for solving.

For example, a series:

 

data[10] = {3,5,2,78,45,32,90,56,5,67}

 

 

The idea of quick sorting is:

1. Select a tag value, which can be any element in the set.

2. Move all elements smaller than the tag value to the left of the tag value, and all elements greater than the tag value to the right of the tag value.

Then repeat the process on the left and right elements.

The above series

 

3 5 2 78 4532 90 56 5 67

 

Sort it:

1 Selected tag value, such as the last element 67

2. The moving process is as follows: if the value is less than 67, the moving forward area is greater than 67.

You do not need to move the first three times because it is less than 67.

For the fourth time, 78 is greater than 67, but it does not need to be moved in the first four elements with the most edges.

The fifth 45 is less than 67 and must be moved to the front of 78.

 

3 5 2 45 7832 90 56 5 67

 

The sixth 32 is smaller than 67 and must be moved to the front of 78.

 

3 5 2 45 3278 90 56 5 67

 

No need to move for the seventh 90-> 67

 

3 5 2 45 3278 90 56 5 67

 

The eighth 56 is smaller than 67 and must be moved to the front of 78.

 

3 5 2 45 3256 90 78 5 67

 

The ninth step 5 is smaller than 67 and needs to be moved to the front of 90

 

3 5 2 45 3256 5 78 90 67

 

Finally, move 67 to the back of 5 to ensure that all elements earlier than 67 are smaller than 67, and all elements later than 67

This division is complete.

And so on.

...

Pseudo-code implementation:

 

quikSort(A,p,r)if p < rthen q <---- PARTITION(A,p,r)quikSort(A,p,q-1)quikSort(A,q+1,r)PARTITION(A,p,r)x = A[r]i <----p - 1for j <---p to r-1do if A[j] <= xthen i = i + 1;swap(A[i],A[j])swap(A[i + 1],A[j]);return i + 1

C code implementation:

 

 

#include <stdio.h>#include <stdlib.h>void quikSort(int *data, int p, int r);int PARTITION(int *data,int p,int r);int main(){int data[10] = {3,5,2,78,45,32,90,56,5,67};quikSort(data,0,9);int i = 0;for(i = 0; i < 10; i ++){printf("%3d",data[i]);}return 0;}
int PARTITION(int *data,int p,int r){int x = data[r];int i = p -1;int temp = 0;int j = 0;for(j = p; j < r; j ++){        if(data[j] <= x)        {        i = i + 1;        temp = data[i];        data[i] = data[j];        data[j] = temp;        }}temp = data[i + 1];data[i + 1] = data[r];data[r] = temp;return i + 1;
void quikSort(int *data, int p, int r){  if(p < r)  {        int q = PARTITION(data,p,r);        int i = 0;        for(i = 0; i < 10; i ++)        {        printf("%3d ",data[i]);        }        printf("\n");        quikSort(data,p,q-1 );        quikSort(data,q+1,r);;   }}

 

 

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.