Data Structure and algorithm analysis-sorting, data structure and algorithm sorting

Source: Internet
Author: User

Data Structure and algorithm analysis-sorting, data structure and algorithm sorting

Author: xiabodan Source: http://blog.csdn.net/xiabodan

Sorting Algorithm is an integral part of computer algorithms. It is also part of the program = Algorithm + data structure (algorithm ).

Lab platform: raspberry 2 B + Ubuntu Mate

Insert sort outer loop I from 1 to N-1, inner loop from j from I to 1, each cycle inserts A [j] into the correct position of sequence A [0]-A [I, this ensures that the sequence A [0]-A [I] after each External Loop is sorted, the External Loop inserts itself between A [0]-A [I] without affecting the correctness of the original sequence. Like the bubble algorithm, it must undergo O (N ^ 2) comparison and exchange, as shown in

// Insert sort // stable // O (N ^ 2) comparisons and swaps // Adaptive: O (n) time when nearly sorted // Very low overheadvoid insertion (elementtype A [], int n) {int p = 0; int j = 0; for (p = 1; p <n; p ++) {elementtype tem = A [p]; for (j = p; j> 0 & A [J-1]> tem; j --) {A [j] = A [J-1];} A [j] = tem ;}}

Hill sort the hill sort can sometimes become incremental and narrow sorting. Select a hk-sized range, insert and sort hk elements separated by hk elements, and gradually decrease the hk range until hk becomes 1.
// Hill sorting // O (N ^ 3/2) unstable // Adaptive: O (N. lg (N) time when nearly sorted // Very low overheadvoid shell (elementtype A [], int n) {int I, j, inc; elementtype tem; for (inc = N/2; inc> 0; inc/= 2) {for (I = inc; I <N; I ++) {tem = A [I]; for (j = I; j> = inc; j-= inc) {if (tem <A [j-inc]) A [j] = A [j-inc]; elsebreak;} A [j] = tem ;}}}
Bubble sort is more violent, the External Loop I from A [0]-A [N-1], the internal loop from A [N-1]-A [I ], at the same time, the inner loop keeps approaching I from the end, and finds out the smallest element that comes to A [I + 1]. It is called Bubble sorting, which is very violent but stable, 1 + 2 + 3 + 4 + ....... + N = N * (N + 1) = O (N ^ 2) comparison and exchange, low efficiency. However, when an inner loop detects that there is no exchange, it means that A [N-1]-A [I] are arranged in order and do not need to be sorted. Therefore, if the given data is close to sorting, the time complexity of the bubble algorithm is only O (N ).
// Bubble sort // O (N ^ 2) stable // Adaptive: O (N) time when nearly sorted // Very low overheadvoid bubble (elementtype A [], int n) {int flag = 1; int I, j; for (I = 0; I <n; I ++) {flag = 0; for (j = n-1; j> I; j --) {if (A [j] <A [J-1]) {flag = 1; swap (A + j, A + J-1 );}} if (flag = 0) break ;}}

Select sort

Select sort, External Loop I from A [0]-A [N-1], inner loop from A [I + 1]-A [N-1 ], the inner loop is responsible for finding the elements A [I + 1]-A [N-1] smaller than A [I] and marking its position with A k, when the inner loop ends, A [I] And A [k] are exchanged, then A [I + 1]-A [N-1] than A [I] Small element A [k] is placed in A [I] This position. The number of comparisons selected for sorting is O (N ^ 2), but the number of exchanges is only O (N ). Because each cycle only marks the smallest element, there is no real-time de-swapping element.

// Select sort // Not stable // O (1) extra space // compute (n2) comparisons // compute (n) swaps // Not adaptivevoid selection (elementtype A [], int n) {int I, j; int k; for (I = 0; I <n; I ++) {k = I; for (j = I + 1; j <n; j ++) {if (A [j] <A [k]) {k = j ;}} swap (A + I, A + k );}}


Quick sorting is A typical Grouping Strategy. It divides A large set of A into small modules and processes them one by one. Select Pivot ElementAnd then divide the entire set A into two halves. One is an element smaller than the limit, and the Set Al is placed on the left. The other is A group of elements larger than the limit on the right, called the set Ar. Then, the Al and Ar subsets of the left and right sides are recursive in the same way until the Al and Ar collections have only one element. In actual application, generally, the Al and Ar types are not recursive to one element. A required method is that when Al and Ar values are less than 10 ( Threshold CUT), Instead of using the fast sorting algorithm, it is more efficient to insert the sorting algorithm. Because this test is not conducive to sorting big data, we only set the threshold CUT to 10. See

Another problem is thatSelection of pivot element scheme,SelectPivot ElementThere are many methods. A common method is the three-digit median algorithm. That is, selectSet A's first and last and median ElementsAnd then select the median of the three numbers as the pivot element.


// Quick sorting // not Stable // O (lg (n) extra space (see discussion) // O (n2) time, but typically O (n · lg (n) time // Not adaptive # define CUT 3 elementtype median3 (elementtype A [], int left, int right) {int center = (left + right)/2; if (A [left]> A [center]) swap (& A [left], & A [center]); if (A [left]> A [right]) swap (& A [left], & A [right]); if (A [center]> A [right]) swap (& A [center], & A [right]); swap (& A [center], & A [right-1]); return A [right-1];} void Qsort (elementtype A [], int left, int right) {int I, j; elementtype comment; if (left + CUT <= right) {centers = median3 (A, left, right); // select middle element as follows Ti = left; j = right-1; for (;) {while (A [++ I] <strong) {} while (A [-- j]> lead) {} if (I <j) swap (& A [I], & A [j]); elsebreak;} swap (& A [I], & A [right-1]); Qsort (, left, I-1); Qsort (A, I + 1, right);} elseinsertion (A + left, right-left + 1);} void quick1 (elementtype A [], int n) {Qsort (A, 0, n-1 );}


To be continued .......


Summary:

Bubble and insert are the first to find the maximum or minimum values. You can find the maximum or minimum values directly to the first one. devide-merge-conquer is used for merging and fast sorting ), during this period, the most basic algorithms mentioned above will still be used; the heap sorting uses the method of selecting the sort; the bucket sorting uses the method of changing the space for time.

Refer:

Data Structure and algorithm analysis-C language description [M], Mechanical Industry Press

Blog vamei blog: http://www.cnblogs.com/vamei/archive/2013/03/12/2948847.html

Tianjin City College a boutique Course: http://sjjp.tjuci.edu.cn/sjjg/datastructure/ds/web/paixu/paixu8.1.1.1.htm

A foreign sorting website has an animation, analysis, for the code: http://www.sorting-algorithms.com/

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.