IOS algorithm (I) set the quick sorting algorithm and ios algorithm sorting

Source: Internet
Author: User

IOS algorithm (I) set the quick sorting algorithm and ios algorithm sorting

Quick sorting is performed when large data volumes are encountered.,Fast sorting,Efficient Method (the company will basically be asked during interviews ...)

The basic idea of this method is:

1. First, extract a number from the series as the reference number.

2. In the partitioning process, place all the numbers greater than this number to its right, and all the numbers smaller than or equal to it to its left.

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

Simply putFind a reference number (any number of elements to be sorted, usually the first element is selected), and place the elements smaller than or equal to the reference number to the left of the reference number, place the element greater than the base number on the right of the base number. after the baseline number is arranged, the left and right sides of the baseline number are regarded as a whole. On the left side, select the baseline number and place the elements smaller than or equal to the baseline number to the left of the baseline number, place the element greater than the base number on the right of the base number .. until each interval has only one data location.

Fast sorting is faster, because compared with Bubble sorting, each exchange is Skip. Set a benchmark for each sort. Place all the numbers smaller than or equal to the benchmark to the left of the benchmark, and all the numbers greater than or equal to the benchmark to the right of the benchmark. In this way, the exchange between adjacent numbers is not the same as the bubble sort. The exchange distance is much larger. Therefore, the total number of comparisons and exchanges is reduced, and the speed naturally increases. Of course, in the worst case, it is still possible that two adjacent numbers are exchanged. Therefore, the worst time complexity of fast sorting is O (N2), and the average time complexity of Bubble Sorting is O (NlogN ).

Interpreting the above ideas



<Written explanation>

1. algorithm ideas
Quick sorting is A sort by Division and exchange proposed by C. R. A. Hoare in 1962. It adopts a sub-Governance Policy, usually called Divide-and-ConquerMethod ).

(1) Basic Idea of divide and conquer Law
The basic idea of the division and control law is to break down the original problem into several subproblems with smaller sizes but similar structures as the original problem. Recursively solve these subproblems, and then combine the solutions of these subproblems into the solutions of the original problem.

(2) Basic Idea of fast sorting
Set the unordered area to R [low .. high], and describe the basic idea of fast sorting using the division and control method as follows:
① Decomposition:
In R [low .. select a record in high] as the benchmark to divide the current disordered zoning into two smaller subintervals (left and right) R [low .. pivotpos-1) and R [pivotpos + 1 .. high], and make the keywords of all records in the left subinterval less than or equal to the benchmark record (may be recorded as the benchmark) keyword. key, the key of all records in the subinterval on the right is greater than or equal to limit. the benchmark record is located at the correct position (pivotpos), and does not need to be sorted in the future.
Note:
The key to division is to determine the location of the benchmark record, TPOs. The division result can be simply expressed as (note that partition = R [pivotpos]):
R [low... pivotpos-1]. keys ≤ R [small TPOs]. key ≤ R [small TPOs + 1 .. high]. keys
Here, low ≤ TPOs ≤ high.
② Solution:
Use recursive call to quickly sort the Left and Right subintervals R [low... pivotpos-1] and R [small TPOs + 1 .. high.

③ Combination:
Because when the two recursive calls in the "solving" Step end, the left and right subintervals are ordered. For quick sorting, the "Combination" step does not need to be done, and can be considered as a null operation.

Code Implementation:


//

// Main. m

//Quick Sorting Algorithm

//

// Copy right (c) 2014YearSummer2014mht@sina.com. All rights reserved.

//



# Import <Foundation/Foundation. h>

# Define COUNT 100 // defines the number of array elements

Int a [COUNT], n; // defines global variables. These two variables must be used in subfunctions.

// Connect parameters to the quick sort method, including the start position (left) and end position (right)

Void quicksort (int left, int right ){

Int I, j, t, temp;

If (left> right) // when the start coordinate is greater than the end coordinate, return directly to end the following operations

Return;

Temp = a [left]; // The reference number is stored in temp (the reference number is random, but generally it is the first element)

I = left;

J = right;

While (I! = J)

{

// The order is very important. You need to start from the right

While (a [j]> = temp & I <j)

J --;

// Find another one on the left

While (a [I] <= temp & I <j)

I ++;

// Swap the positions of two numbers in the array

If (I <j)

{

T = a [I];

A [I] = a [j];

A [j] = t;

}

}

// At this time, I = j, and the reference number is eventually Reset

A [left] = a [I];

A [I] = temp;

// Recursive call

Quicksort (left, I-1); // continue to process the left, here is a recursive process

Quicksort (I + 1, right); // continue to process the right side. Here is a recursive process.

}


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

{


Int I;

// Read data

Scanf ("% d", & n );

For (I = 1; I <= n; I ++ ){

Scanf ("% d", & a [I]);

}

Quicksort (1, n); // call Quick Sort

// Output the sorted result

For (I = 1; I <= n; I ++ ){

Printf ("% d", a [I]);

}

Return 0;

}



// Additional supplement: List of algorithm complexity and Stability



Compile an algorithm for fast sorting

/*************************************** *********************************/
/* Fast sorting */
/*************************************** *********************************/
Void CommonALG: quickSort (float * arr, int left, int right)
{
Int ltemp = left;
Int rtemp = right;
Float f = arr [(left + right)/2];
Float t = 0;
While (ltemp <rtemp)
{
While (arr [ltemp] <f)
++ Ltemp;
While (arr [rtemp]> f)
-- Rtemp;

If (ltemp <= rtemp)
{
T = arr [ltemp];
Arr [ltemp] = arr [rtemp];
Arr [rtemp] = t;
-- Rtemp;
++ Ltemp;
}
}

If (ltemp = rtemp)
Ltemp ++;
If (left <rtemp)
QuickSort (arr, left, ltemp-1 );
If (ltemp <right)
QuickSort (arr, rtemp + 1, right );
}

By the way, the C language's quick sorting algorithm describes the change process in text, simple array sorting

# Include "stdio. h"
Typedef int InfoType; // defines the data item type.
# Define MAX_SIZE 20 // maximum length of a small ordered table
Typedef int KeyType; // The type of the keyword is integer.
Struct RedType // record type
{
KeyType key; // key
InfoType otherinfo; // other data items
};
Struct SqList // sequence table Type
{
RedType r [MAX_SIZE + 1]; // r [0] idle or used as a sentry Unit
Int length; // The length of the sequence table.
};
// Records of the switch sequence table L subtable L. r [low... high] So that pivot records are in place
// And return the location where it is located. At this time, the records before (after) are not large (small ).
Int Partition (SqList * L, int low, int high)
{
RedType t;
KeyType effectkey;
Repeated tkey = (* L). r [low]. key; // use the first record of the subtable as the pivot record.
While (low {
While (low {
-- High;
}
T = (* L). r [low]; // swap records smaller than pivot records to the low end
(* L). r [low] = (* L). r [high];
(* L). r [high] = t;
While (low {
++ Low;
}
T = (* L). r [low]; // swap records larger than pivot records to high-end
(* L). r [low] = (* L). r [high];
(* L). r [high] = t;
}
Return low; // return the position indicated by the pivot.
}
// Sort the subsequence L. r [low... high] in sequence table L quickly
Void QSort (SqList * L, int low, int high)
{
Int optional tloc;
// Length greater than 1
If (low {
Break tloc = Partition (L, low, high); // split L. r [low... high] into two parts.
QSort (L, low, pivotloc-1); // performs recursive sorting on the lower sub-table, where repeated tloc is the pivot position.
QSort (L, repeated tloc + 1, high); // recursively sorts high subtables.
}
}
// Perform quick sorting on the sequence table L
Void QuickSort (SqList * L)
{
QSort (L, 1, (* L). length );
}
Void print (SqList L)
{
Int I;
For (I = 1; I <= L. length; ++ I)
{
Printf ("(% d, % d)", L. r [I]. ke ...... the remaining full text>

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.