Fast sorting and Its Related Applications (median, number of digits, etc)

Source: Internet
Author: User

Fast sorting and Its Related Applications (median, number of digits, etc)

The reason for calling it fast sorting is very simple, just because its average speed is the fastest in all sorts. First, let's sortCode.

Int partition (int * r, int low, int high)

{

Int T = R [low];

While (low

{

While (low

High --;

R [low] = R [High];

While (low

Low ++;

R [High] = R [low];

}

R [low] = T;

Return low;

}

Int sort (int * r, int low, int high)

{

If (low> = high)

Return 0;

Int optional tloc = 0;

Required tloc = partition (R, low, high );

Sort (R, low, pivotloc-1 );

Sort (R, repeated tloc + 1, high );

}

Void quick_sort (int * r, int N) // calls the sort process.

{

Sort (R, 0, n-1 );

}

Let's talk about other related applications of quick sorting:

1. Median: In the fast sorting, the number smaller than R [low] is placed on the left, and the number greater than R [low] is placed on the right. The return line is the dividing line.Pivotloc,Median is to find(N & 0x01 )? R [Mid] :( R [Mid] + R [Mid + 1])/2, And the number in the middle, mid = n/2. Each timePartition (int * r, int low, int high)Then the location of the demarcation line is obtained. If the demarcation line is smaller than mid, the median is on the right; otherwise, the median is on the left. If you have followed my blog, you will remember my article"Find the minimum 0.1 million number from 1000"The heap is used, and it is also true to find the largest number. I will not implement it in heap mode. Here I will use quick sorting to solve this problem. The Code is as follows:

Int median (int * r, int N)

{

Int left = 0;

Int right = n-1;

Int mid = (left + right)/2;

Int num;

While (1)

{

Num = partition (R, left, right );

If (num = mid)

Break;

If (Num <mid) // The description is on the right.

Left = num + 1;

If (Num> mid)

Right = num-1;

}

Return (N & 0x01 )? R [Mid] :( R [Mid] + R [Mid + 1])/2;

}

In this way, the problem is solved ........

2. Find the number I from the number of N: That's the same. You just need to change it a little bit, so I will not post the code.

There is also the idea that "Quick Sort" is to use sub-governance, and I have time to sort out sub-governance and recursion. Now I want to learn too many things. The most important thing I want to learn is C ++. Although I have learned it before, it is really rare to actually use it, especially generic programming .. There are too many ways to use C and implement C ++. There is also a happy National Day...

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.