[Introduction to algorithms] sorting (1)

Source: Internet
Author: User

Although I have heard of it for a long time, I first came into contact with the introduction to algorithms. I saw the introduction to algorithms in the MIT course of Netease open course. I remember that the first episode talked about insertion sorting and Merge Sorting.

I also bought the introduction to algorithms a few weeks ago ~

This week, I am mainly looking at the first two parts. Apart from talking about the progressive time and recursive analysis, all of them are in the dark.

I feel more and more fascinated. It is indeed a classic.

Okay, start. This article mainly uses C ++ to implement the algorithm in the book, as well as some notes.

1. Insert sorting.

The insert algorithm is designed using the incremental (incremental) method: After the sub-array a [1 .. J-1] is prepared

Element A [J] is queried to form the sorted sub-array a [1. J]

The efficiency of insertion sorting is O (n ^ 2), which is more efficient when the data size is small.

The best case is that an array is already sorted, with only O (n) required ). The worst case is that the input data is in reverse order, O (N ^ 2 ). For an algorithm, it is generally used to test its worst-case running time.

.

Pseudocode: note that most programming languages use arrays starting from 0, while pseudocode starts from 1.

C ++ implementation:

Template <typename T> void insert_sort (T * begin, T * End) {// use the call Method T * P, * t, key that is closer to C ++ STL sort; for (P = begin + 1; p! = End; ++ p) {// insert a [J] into the sorted a [1... in J-1] Key = * P; t = p-1; while (T> = begin & * T> key) {* (t + 1) = * t; -- t ;} * (t + 1) = Key ;}}

Ii. Bubble sorting, selection sorting, and exchange sorting.

I remember seeing a piece of code in Liu rujia's "Getting started to algorithm competitions". The book said it was a bubble sort, but it was a little different from the bubble sort process. It has not been known until recently,

It should be regarded as an exchange sort.


Select sort pseudocode:



Bubble sort pseudocode:


// Select the sort void select_sort (int * Start, int * End) {int * P, * q, * min; For (P = start; P <end; ++ P) {min = P; For (q = start + 1; q <end; ++ q) {If (* q <* min) min = Q;} swap (Min, p) ;}} // Bubble Sorting void bubble_sort (int * Start, int * End) {int * P, * q; For (P = start; P <end; + + p) {for (q = end-1; q> Start; -- q) {If (* q <* (q-1) Swap (Q, q-1 );}}}

The efficiency of the three sorting statements is O (n ^ 2)

It can be seen that the selection of sorting and Bubble sorting are the smallest of all numbers after the current position I, and are switched to the I position, but the selection of sorting only needs to be exchanged once, bubble Sorting may be exchanged multiple times, and the speed should be faster ..

In addition to sorting and Bubble sorting, there is also an idea called "Exchange sorting", which is almost the same. It compares the numbers I and I respectively, once a number is smaller than it, it is switched.

Iii. Merge Sorting (Merge Sorting)

Merge Sorting is an efficient sorting algorithm. It adopts the three-step splitting method:

Division Problems: Divide the sequence into two halves with equal numbers of Elements

Recursive Solution: Sort the two half elements separately.

Merge Problems: Merge two ordered tables into one

Merge-Sort pseudocode:

Merge is the key:

The code implementation here is to put a "Sentinel card" at the bottom of each pile, which can be used to simplify the code.

However, I use the C ++ Implementation Method in another form. Although the code can be simplified with the "Sentinel card", this "Sentinel value" is somewhat limited, it may be different on different platforms and machines.

// Algorithm introduction implementation version template <typename T> void Merge (T array [], int P, int Q, int R) {int N1, N2, I, J, K; n1 = Q-p + 1; N2 = r-Q; T * Left = new T [N1], * Right = new T [n2]; for (I = 0; I <N1; ++ I) // left [I] = array [p + I]; for (I = 0; I <N2; ++ I) // assign the right [I] = array [q + I + 1] to the array; I = J = 0; k = P; while (I <N1 & J <N2) {If (left [I] <= right [J]) array [k ++] = left [I ++]; else array [k ++] = right [J ++] ;}for (; I <N1; ++ I) // If the left array has element surplus, then, the remaining elements are merged into the array [k ++] = left [I]; for (; j <N2; ++ J) // if the right array has element surplus, merge the remaining elements to the array [k ++] = right [J]; Delete [] Left; // remember to release space Delete [] Right ;} template <typename T> void merge_sort (T array [], int P, int R) {If (P <r) {int q = (p + r)/2; merge_sort (array, p, q); merge_sort (array, q + 1, R); merge (array, P, Q, R );}}

Parallel sorting is useful in ACM and can be used to calculate Reverse Order Number pairs (P144 in algorithm competition getting started), because if the efficiency of direct enumeration is O (n ^ 2) timeout.

Inspired by the Union sorting, merge operations are performed from small to large, when a [J] on the right is copied to T (here t is the parallel sorting Implementation of Liu rujia, which will be given later ), the numbers that have not been copied to T on the left are all the numbers greater than a [J] on the left, that is, the reverse order of a [J ].

Parallel sorting of Liu rujia shiniu + finding the reverse order number by the way

Void merge_sort (int * a, int X, int y, int * t) {// T is a dedicated temporary array. This greatly saves a lot of time (the above method needs to open up array space multiple times) if (Y-x> 1) {int M = x + (Y-x)/2; // divide int P = x, q = m, I = x; merge_sort (A, X, M, T); merge_sort (a, m, Y, t ); while (P <M | q <Y) {// non-recursive call can also save stack call time if (q> = Y | (P <M & A [p] <= A [Q]) T [I ++] = A [p ++]; else {T [I ++] = A [q ++]; CNT ++ = m-P ;} // CNT is used to calculate the reverse order number pair. It is cleared before calling using global variables} for (I = x; I <Y; ++ I) A [I] = T [I] ;}}


-- The meaning of life is to give it meaning.

Original Http://blog.csdn.net/shuangde800 ,
D_double



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.