Fast sorting algorithm

Source: Internet
Author: User
Tags array length

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

Fast sorting is a sort algorithm, with an average time of O (NLGN) for an input array containing n, and the worst case is O (n^2).
is usually the best choice for sorting. Because, based on the comparison of the sorting, the fastest can only reach O (NLGN).


Ii. description of the fast sorting algorithm
Introduction to Algorithms, 7th chapter
The quick sort is based on the divide-and-conquer mode,
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: Fast sort by recursive call, a[p to sub-array. Q-1] and a[q+1. R] Sort.
3. Merger.

Three, fast sorting algorithm

There are many versions of the fast-sorting algorithm, which is also based on the different ways of thinking, acceptance and understanding of each person. There is no obvious merits and demerits of the points, as long as they can understand, you can! The next thing I'm going to talk about is the version of the algorithm introduction.

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)


The key to the fast sorting algorithm is the partition process (array partitioning), which is to a[p. R] for in-place re-scheduling:
PARTITION (A, p, R)
1 X←a[r]//With the last element, A[r] as the main element
2 i←p-1
3 for J←p to R-1//j is pointing from p to r-1, not R.
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

Give an example of how this array is divided: 2 8 7 1 3 5 6 4

Note: With the last element 4 as the main element, and the I,j both hands starting from the beginning,J one ago, I one after. I refers to the previous position of the element, and J points to the first element in the column to be sorted.

I p/j R

2 8 7 1 3 5 6 4 (main element)
J refers to the element is 2, because 2<=4 (the principal element), so i++,i also refers to the element 2, so a[i] <-> A[j], that is 2 and 2 interchange, the original array unchanged.
Then, J continues to move back until it points to 1.
Ii

p/i J R

2 8 7 1 3 5 6 4 (main element)

J points to Element 1, because 1<=4 (the principal element), so i++,i points to 8, so 8 is exchanged with 1.

The array becomes:
P I j R
2 1 7 8 3 5 6 4

P I j R
2 1 7 8 3 5 6 4

Then J continues to move back, pointing to Element 3, since 3<=4 (the principal element), so i++,i this is pointing to 7, whereupon 7 is exchanged with 3.
The array becomes:
P I j R
2 1 3 8 7 5 6 4

J continued to move back, until R-1 's position, were found no more than 4 small numbers, so, executed to the of the partition process 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
P I j R
2 1 3 8 7 5 6 4
Execute A[i + 1] <-> a[r], i.e. 8 and 4 swaps, so the array eventually becomes the following form,
2 1 3 4 7 5 6 8
At this point, the quick sort first trip is complete.


4 (main element) divides the entire array into two parts, 2 1 3 and 7 5 6 8, and then recursively sorts the two parts quickly.
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 (the main element) put 2 1 3, divided into two parts, 2 1 and 3.
Again on 2 1, the recursive sort, the final result becomes 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,
However, at the moment 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, the final result becomes 5 6 7 8.

OK, all the fast sequencing processes, complete analysis.

And finally we have to say that the most critical partition process (array partitioning) in the fast sort, from the above process, can be seen in each partition process, J scanned the entire array once, as long as once encountered than 4 (main element) small elements, I on + +, then, KJ, ki Exchange. So why did I have to + + when J found the element that was smaller than 4? Do you think, if I always stop in place, and kj each exchange of KI is not the same element? So, what sort of talk? So, j in front of the open circuit, I followed by J, J as long as the encounter than 4 small elements, I went forward further, and then the J found more than 4 small elements, assigned to I, then, J before moving forward.

The analogy is, you can think, I pass every step, must be a smaller than 4 elements, otherwise, I can not continue to move forward. Like J is the forerunner, for I open-bridge, the small elements as a springboard to I, to pave the way forward AH.

At this point, the J scan to the end, has also completely detected a smaller than 4 elements, only the last master 4, then handed to I processing, because the last step, Exchange A[i + 1] <-> a[r]. In this way, not only is it completely ensured that the elements that are smaller than 4 are exchanged to the front of the array, and the larger elements that were not processed before J are swapped to the back, and the time complexity of O (N), you have to admire the ingenious design of this algorithm.

The time complexity of the fast sorting algorithm

OK, probably understand the fast sort, then, can also quickly determine: The fast sorting algorithm average time complexity, that is, O (NLGN). What's the reason? Because you see, j,i scan the array once and spend how much? Yes, scan again, of course, O (n), then, how many times to scan the column, LGN to n times, the fastest lgn, the slowest n times. And it can be proven that the average time complexity of fast sorting is O (N*LGN).

Partition can do the most balanced division, the resulting sub-problem can not be greater than N/2. Because the size of one of the sub-problems is |_n/2_|. The size of the other sub-problem is |-n/2-|-1.

In this case, the quick sort is much faster. For:

t (n) <=2t (N/2) +o (n). Can be proven, t (n) =o (NLGN).

Java code implementation of fast sorting algorithm

 Public classQuickSort {/*** Quick sort of Main method *@paramarray: Arrays to be sorted *@parambegin: The subscript of the first element of the array (actually 0) *@paramEnd: The subscript of the last element of the array (actually the array length-1) *@return     */     Public Static int[] QuickSort (int[] Array,intBeginintend) {                if(Begin <end) {            intK =partition (array, begin, end); QuickSort (Array,begin,k-1); QuickSort (Array,k+1, end); }        returnArray; }     Public Static intPartitionint[] Data,intHeadinthi) {                 intKey=data[hi];//with the last element, Data[hi] as the main element         intI=head-1;  for(intj=head;j///Note, J points from p to r-1, not R.          {          if(data[j]<=key) {i=i+1;          Swap (DATA,I,J); }} swap (Data,i+1, HI); returnI+1; }     Public Static voidSwapint[] Data,intIintj) {        inttemp =Data[i]; Data[i]=Data[j]; DATA[J]=temp; }         Public Static voidMain (string[] args) {int[] Array = {2,8,7,1,3,5,6,4,9}; intLen = array.length-1; Array= QuickSort (Array, 0, Len);  for(inti = 0; i < Array.Length; i++) {System.out.print (array[i]); }    }}

Organized from: http://blog.csdn.net/v_JULY_v/article/details/6211155

http://blog.csdn.net/v_JULY_v/article/details/6116297

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.