High-speed sorting algorithm

Source: Internet
Author: User

High-speed sequencing Algorithm

Author July January 4, 2011
------------------------------------------

Before writing, let's say humorous digression.
Each time I write an article, I will follow the following principles:
First, keep the layout as clear as possible, to maintain good typography.
Second, strive to write something, clear and easy to understand, illustrated
Third, the best possible to ensure that the written things accurate, practical value.

Because, I think, since you want to publish your article, then you must be responsible for your readers.
Otherwise, don't publish it. Everything, to serve the reader.

OK, not much to say. Next, we immediately go into the topic of this article, sorting algorithms.
As we all know, high-speed sorting algorithm is the play in the sorting algorithm.
Therefore, this series starts with the high-speed sequencing.
------------------------------------------------------

First, the basic characteristics of high-speed sequencing algorithm
Complexity of Time: O (N*LGN)
Worst: O (n^2)
Space complexity: O (N*LGN)
Not stable.

High-speed sorting is a sort algorithm, the average time is O (NLGN) for an input array including n, and the worst case is O (n^2).
is generally the best choice for sorting. Because, based on the comparison of the sorting, the fastest can only reach O (NLGN).


Descriptive narration of high-speed sequencing algorithm
Introduction to Algorithms, 7th chapter
High-speed sequencing is based on split-mode processing,
The divide-and-conquer process for a typical subarray A[P...R] is three steps:
1. Decomposition:
A[p. R] is divided into two (possibly empty) sub-arrays a[p. Q-1] and a[q+1. R], making
A[p. Q-1] <= a[q] <= a[q+1. R
2. Workaround: High-speed sorting by recursive invocation, a[p of the sub-array. Q-1] and a[q+1. R] Sort.
3. Merger.

Three, high-speed sorting algorithm

Version Number one:
QUICKSORT (A, p, R)
1 if p < r
2 then Q←partition (A, p, r)//Key
3 QUICKSORT (A, p, q-1)
4 QUICKSORT (A, q + 1, R)

Array partitioning
The key to the high-speed sequencing algorithm is the partition process, which is a[p. R] for in-place re-scheduling:
PARTITION (A, p, R)
1 X←a[r]
2 i←p-1
3 for J←p to R-1
4 do if a[j]≤x
5 then i←i + 1
6 Exchange A[i] <-> A[j]
7 Exchange A[i + 1] <-> A[r]
8 Return i + 1

OK, let's give a detailed and complete example.
To the following array, high-speed sorting,
2 8 7 1 3 5 6 4 (main element)


One

I p/j

2 8 7 1 3 5 6 4 (main element)
J refers to the 2<=4, so i++,i also refers to 2, 2 and 2 swaps, the original array unchanged.
J move back until you point to 1.
Two
J (point 1) <=4, so i++
I pointed to 8, so that 8 switched with 1.
The array becomes:
I J
2 1 7 8 3 5 6 4
Three, J moved back, pointing to the 3,3<=4, so i++
I this was pointed to 7, thereupon 7 with 3 exchanged.
The array becomes:
I J
2 1 3 8 7 5 6 4
Four, J continue to move back, found no more than 4 small number, so, carried out to the last step,
This is the 7th line of the partition (A, p, r) code section above.
Therefore, I move back one unit, pointing to the 8
I J
2 1 3 8 7 5 6 4
A[i + 1] <-> a[r], i.e. 8 and 4 swap, so the array finally becomes such as the following form,
2 1 3 4 7 5 6 8
OK, high-speed sequencing the first trip is complete.


4 divides the entire array into two parts, 2 1 3,7 5 6 8, and then recursively sorts the two parts at high speed.
I p/j
2 1 3 (main element)
2 and 2 are interchangeable, unchanged, then 1 and 1 are interchangeable, or unchanged, finally, 3 and 3 are interchangeable, unchanged,
Finally, 3 2 1 3, divided into two parts, 2 1, and 3.
Again to 2 1, a recursive sort, finally the result became 1 2 3.

7 5 6 8 (main element), 7, 5, 6, are smaller than 8, so the first trip, or 7 5 6 8,
Just now 8 put 7 5 6 8, divided into 7 5 6, and 8. [7 5 6->5 7 6->5 6 7]
Again on 7 5 6, the recursive sort, finally turns out to be 5 6 7 8.

OK, all the process, complete analysis.
Finally, look at the picture I drew:

High-speed sorting algorithm version number two

Only, this version number is no longer selected (as on the first version number) of the last element of the array as the main element,
Instead, the first element in the array is a primary.

/**************************************************/
/* Function function: High speed sorting algorithm */
/* Function Reference: Structure Type table's pointer variable tab */
/* Integer variable left and right border subscript */
/* Function return value: null */
/* File name: quicsort.c function Name: quicksort () */
/**************************************************/
void quicksort (table *tab,int left,int right)
{
int i,j;
if (left<right)
{
I=left;j=right;
tab->r[0]=tab->r[i]; Prepare to divide the value of the leftmost element into a standard, saving its value first
Do
{
while (TAB-&GT;R[J].KEY&GT;TAB-&GT;R[0].KEY&AMP;&AMP;I&LT;J)
j--; Find 1th from right to leftless thanPosition of the standard value J
if (I&LT;J)//found, position is J
{
tab->r[i].key=tab->r[j].key;i++;
}//Put the first J element on the left and reset I
while (TAB-&GT;R[I].KEY&LT;TAB-&GT;R[0].KEY&AMP;&AMP;I&LT;J)
i++; Find a 1th from left to rightGreater thanPosition of the standard value I
if (I&LT;J)//found, position is I
{
tab->r[j].key=tab->r[i].key;j--;
}//Put element I to the right and reset J
}while (I!=J);

tab->r[i]=tab->r[0]; Put the standard value into its final position, the end of this division
Quicksort (tab,left,i-1); Call this function recursively on the left half of the standard value
Quicksort (Tab,i+1,right); Call this function recursively on the right half of the standard value
}
}

----------------

OK, let's use the same array as above to demonstrate this sort process by applying the version number two of this fast algorithm:
This time, the first element in the array is 2 as the primary.
2 (Master) 8 7 1 3 5 6 4

Please see:
  2  8  7  1  3  5   6  4
  i->                      <-J
   (find big)                 (small)
one, J
J Find the first element less than 2 (overwrite reset) I refers to the element 2
gets:
  1  8  7     3  5  6  4
      i        J      
     
II, I
I find the first element greater than 2 8,8 assigned (overwrite reset) j refers to the element (null<-8)
  1     7   8  3  5  6  4
      i   <-j

Three, J
J continues to move left, before meeting I, no more than 2 small elements were found, ending.
Finally, the main element 2 is mended.
At the end of the first run, the array becomes:
1 2 7 8 3 5 6 4

Second trip,
7 8 3 5 6 4
I-> <-j
(For Big) (For small)

A, J
J found 4, smaller than the primary 7, 4 assigned to 7 locations
Get:
4 8 3) 5 6
I-> J
Second, I
I find the first element that is larger than 7 8,8 overrides the element of J (NULL)
4 3 5) 6 8
I J
4 6 3) 5 8
I-> J
I and J meet, end.
Third trip:
4 6 3 5 7 8
......
Below, the analysis principle, consistent, skip.
The final result, for example, is as seen in:
1 2 3 4 5 6 7 8
I believe that after detailed analysis of the above content, you must understand.

Finally, post a picture of the sort process I drew:

Finish. Added January 5.
OK, the two algorithms above, clear one can.
-------------------------------------------------------------

Five, high-speed sequencing of the worst case and the fastest situation.
The worst-case scenario occurs when the two areas of the partitioning process include n-1 elements and a 0 element, respectively,
That is, the hypothesis that the algorithm appears every time the recursive call process, such a division is not correct. Then the cost of Division is O (n),
Since the recursive invocation of an array of size 0 returns T (0) =o (1).
The execution time of the estimation method can be expressed recursively:

T (n) =t (n-1) +t (0) +o (n) =t (n-1) +o (n).
Can prove to be T (n) =o (n^2).

Therefore, assuming that at each level of the algorithm recursion, the division is the maximum degree of incorrect, then the execution time of the algorithm is O (n^2).
That is, the worst-case scenario for a high-speed sorting algorithm is no better than an insert sort.

In addition, when the array is fully sequenced, the execution time of the high-speed sort is O (n^2).
In the same case, the execution time of the insert sort is O (n).

Note, please understand this sentence. We say that a sort of time complexity is only for one element.
It means inserting an element into the order, inserting it into an ordered sequence and taking the time N.


again to prove that, in the quickest case, that is, partition may do the most balanced division, get each sub-problem can not be greater than N/2.
Because the size of one of the sub-problems is |_n/2_|. There is also a sub-problem size of |-n/2-|-1.
In such cases, high-speed sequencing is much faster. For
t (n) <=2t (N/2) +o (n). Can be proven, t (n) =o (NLGN).

Intuitively, see, high-speed sequencing is a recursive number, in which, Partition always produces 9:1 of the division,
The total execution time is O (NLGN). The size of the sub-problem is shown in each node. The cost of each layer is shown on the right.
Each layer consists of a constant c.

Finish.
July, January 4, 2011.
=============================================

Would you please consider the following version number, corresponding to which version number above?
Hoare-partition (A, p, R)
1 X←a[p]
2 i←p-1
3 J←r + 1
4 while TRUE
5 Do repeat J←j-1
6 until A[j]≤x
7 Repeat I←i + 1
8 until A[i]≥x
9 If I < J
Ten then Exchange A[i]? A[J]
One else return J

I often think about why some people clearly read and understand an algorithm,
And after a while, it is completely unfamiliar with the algorithm and does not understand the column?

I think, the fundamental, or not completely clear the principle of this high-speed sequencing algorithm, and the ins and outs ...
What the improvement column, only can find the original author of the algorithm, from the original author, and then dig a little more practical things out.

July, February 15, 2011 update.

=========================================
Finally, a concise demonstration example of a high-speed sorting algorithm is given:
quicksort function
void quicksort (int l, int u)
{int i, m;
if (l >= u) return;
Swap (L, Randint (L, u));
m = l;
for (i = l+1; I <= u; i++)
if (X[i] < x[l])
Swap (++m, i);
Swap (L, m);
Quicksort (L, m-1);
Quicksort (m+1, u);
}

Assuming that the function is called quicksort (0, n-1), the code will sort a global array x[n].
The two parameters of a function are the subscripts of the sub-array to be sorted: L is the lower subscript, and U is the higher subscript.

The function call Swap (I,J) will exchange the two elements of X[i] and x[j].
The first exchange operation will randomly select a dividing element between L and U according to the uniform distribution.

OK, many others please refer to the second article I wrote about high-speed sorting algorithms: a continuation of the high-speed sequencing algorithm in-depth analysis , the third article: 12, one of the re-continuation: high-speed sorting algorithm of the full version of C + + implementation .

July, February 20, 2011 update.

High-speed 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.