iOS algorithm (one) fast sorting algorithm

Source: Internet
Author: User

Quick Sort is when large data is encountered , Sort Fast , 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.

simply to understand is Find a base number ( Any number to be sorted is typically the first element selected puts elements less than or equal to the base number to the left of the base number places elements greater than the base number on the right side of the base number After in the left and right of the datum as a whole ,  left : Continue to select the base number to place the element less than or equal to the base number to the left of the base number place the element greater than the base number on the right side of the base number the same as the right side Until each interval has only one number position

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

650) this.width=650; "Src=" http://img.blog.csdn.net/20140916073859874?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvtuhuaw9z/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "/>


< Written language interpretation >

1, Algorithmic thinking
Quick Sort isC.r.a.hoareIn1962A sort of division exchange proposed in the year. 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 Treatment method
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 quick sorting
Set the current unordered area to be sorted asR[low. High], the basic idea of fast sequencing can be described by the method of divide and conquer:
① decomposition:
InR[low. High]Select a record as the benchmark(Pivot), the current unordered division is divided into left and right two smaller sub-intervals with this datumR[low. PIVOTPOS-1)AndR[pivotpos+1..high]And makes all the recorded keywords in the left sub-range less than or equal to the datum record(Remember aspivot)The key wordPivot.key, all the recorded keywords in the right sub-range are greater than or equal toPivot.key, while Benchmark recordsPivotis in the right place(Pivotpos), it does not need to participate in subsequent sorting.
Attention:
The key to partitioning is to ask where the benchmark record isPivotpos。 The results of the partitioning can be simply expressed as(AttentionPivot=r[pivotpos]):
  R[low. Pivotpos-1].keys≤r[pivotpos].key≤r[pivotpos+1..high].keys
whichLow≤pivotpos≤high。
② Solution:
   Quick sort by recursive call to left and right sub-rangeR[low. PIVOTPOS-1]AndR[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 :


//

Main.m

//  Fast Sorting algorithm

//

Copyright (c) year [email protected] All rights reserved.

//



#import <Foundation/Foundation.h>

#define COUNT // Define the number of array elements

int A[count], n; / / Define global variables, these two variables need to be used in the child function

// 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//temp is the base number (the base number is random , but is generally 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 two numbers in the position of 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 // output

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

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

}

return 0;

}



// Additional additions : algorithm complexity and stability checklist

650) this.width=650; "Src=" http://img.blog.csdn.net/20140916073936109?watermark/2/text/ ahr0cdovl2jsb2cuy3nkbi5uzxqvtuhuaw9z/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/ Southeast "/>


iOS algorithm (one) 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.