iOS algorithm (a) fast sorting algorithm

Source: Internet
Author: User

Quick Sort is when large data is encountered , sort fast and efficient method (company interview, basically will be asked ...)

The basic idea of this method is:

1. First, a number is taken from the series as the base number.

2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.

3. Repeat the second step for the left and right intervals until there is only one number for each interval.

simple to understand is , find a base number (any number to be sorted, usually the first element selected), place elements that are less than or equal to the base number to the left of the base number, and place elements that are larger than the base number on the right side of the base number . After the platoon, On the left and right side of the base number as a whole , left : Continue to select the base number of elements less than or equal to the base number of the left side , the elements greater than the base number on the right side, the right is the same . Until each interval has only one number of positions .

Fast sorting is faster because each exchange is a jump-through compared to the bubble sort. Set a datum point each time it is sorted, place a number less than or equal to the datum point to the left of the Datum point, and place the number greater than or equal to the datum point to the right of the datum point. This will not be the same as the bubble in each exchange of the same time can only be exchanged between the adjacent number, the exchange distance is much larger. Therefore, the total comparison and the number of exchanges is less, the speed naturally increased. Of course, in the worst case, it is still possible to exchange two consecutive numbers. So the worst time complexity for fast sorting is the same as the bubble sort is O (N2), and its average time complexity is O (nlogn).

The picture interprets the thought above

< written language interpretation >

1. Algorithm idea
Quick Sort isC.r.a.hoare inA sort of divided exchange proposed in 1962. It uses a strategy of division, which is often referred to as a divide-and-conquer method.(Divide-and-conquermethod).

(1) The basic idea of the Division and Administration law
The basic idea of divide-and-conquer method is: to decompose the original problem into several sub-problems with smaller size but similar structure to the original problem. Solve these sub-problems recursively, then combine the solutions of these sub-problems into the solution of the original problem.

(2) The basic idea of fast sequencing
Set the current unordered area to be sorted asR[low. High], the basic idea of fast sequencing can be described by using the divide-and-conquer method:
① decomposition:
InR[low. High] Select a record as the benchmark(Pivot), which divides the current unordered division into left and right two smaller sub-rangesR[low. PIVOTPOS-1) andR[pivotpos+1..high], and make all the records in the left sub-range are less than or equal to the baseline recordpivot) keyword pivot.key, all records in the right sub-range are greater than or equal to the keyword pivot.key, while the benchmark record pivot is located in the correct location
   Note:
     partitioning the key is to require the location of the baseline record pivotpos. The results of the partitioning can simply be expressed as (note pivot=r[pivotpos]):
      r[low. Pivotpos-1].keys≤r[pivotpos].key≤r[pivotpos+1..high].keys
                    where low≤pivotpos≤high.
② Solver: &NBSP;
      quick Sort by recursive call to left and right sub-range r[low. PIVOTPOS-1] and r[pivotpos+1..high] quick sort.

③ Combination:
Since the two recursive calls in the solve step end, their left and right two sub-bands are ordered. For a quick sort,the combination step does not have to do anything and can be seen as an empty operation.

Code Implementation :

#import <Foundation/Foundation.h>

#define COUNT// define the number of array elements

int A[count], n; Define global variables, which need to be used in child functions

Give the quick Sort method A parameter , start position (left ), and end position (right )

void quicksort (int left, int. right) {

int I, J, T, temp;

if (Left > right)// start position coordinates are greater than the end position coordinates, return directly to end the operation below

Return

temp = A[left]; the base number is stored in temp (the base number is random, but is usually the first element)

i = left;

j = right;

while (i! = j)

{

The order is important, start looking from the right.

while (A[j] >= temp && i<j)

j--;

and find the left.

while (A[i] <= temp && i<j)

i++;

Swap the position of two numbers in the array

if (I < J)

{

t = A[i];

A[i] = A[j];

A[J] = t;

}

}

at this point i = j, finally the base number is returned to the position

A[left] = A[i];

A[i] = temp;

Recursive invocation

Quicksort (left, i-1);// continue to handle the right side, here is a recursive process

Quicksort (i+1, right);// continue to handle the left , here is a recursive process

}

int main (int argc, const char * argv[])

{

int i;

read in Data

scanf ("%d", &n);

for (i = 1; I <= n; i++) {

scanf ("%d", &a[i]);

}

Quicksort (1, N); Quick Sort Call

Results after sorting the output

for (i = 1;i <= n; i++) {

printf ("%d", a[i]);

}

return 0;

}

iOS algorithm (a) fast sorting algorithm

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.