From sorting (1) Bubble sorting, insert sorting, and select sorting

Source: Internet
Author: User

How can I learn algorithms without understanding sorting? However, we often get used to sort and qsort. We may have forgotten the specific sorting. We start with the common order of O (n ^ 2. Bubble Sort: Speaking of sorting, you can't just talk about Bubble Sort. It is very simple. In the Wikipedia, this explains "I have repeatedly visited the series to be sorted, compare two elements at a time, and exchange them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. The name of this algorithm comes from because the smaller elements will slowly 'float 'to the top of the sequence through the exchange ." Complexity: Worst time complexity: O (n ^ 2) optimum time complexity: O (n ^ 2) Average time complexity: O (n ^ 2) stability: stable I usually write bubble like this: [cpp] void bubbleSort (int arr [], int n) {for (int I = 0; I <n; ++ I) {for (int j = I + 1; j <n ;++ j) {if (arr [I]> arr [j]) {int t = arr [j]; the basic idea of arr [j] = arr [I]; arr [I] = t ;}}} is to extract the first element from the array, then we start to compare it with the elements behind it. As long as we find that it is lighter than it, we exchange two elements. In this way, the lightest (smallest) elements come to the top of the series. Then the second round of the light elements come up, and so on, the last series is ordered. In a word, the smallest number is selected every time to let it come out. Bubble can also be written as follows: [cpp] void bubbleSort (int arr [], int n) {for (int I = 0; I <n-1; ++ I) {for (int j = 0; j <n-i-1; ++ j) {if (arr [j]> arr [j + 1]) {int t = arr [j + 1]; arr [j + 1] = arr [j]; arr [j] = t ;}}}} this is the biggest element that is sunk every time. The effect is equivalent. Bubble can also be optimized, but after all, it is an inefficient algorithm and cannot place an average O (n ^ 2) Fate. A common optimization is to record whether there is an exchange value in each traversal process. If not, it indicates that it is already in order. directly jump out of the loop [cpp] void bubbleSort (int arr [], int n) {for (int I = 0; I <n; ++ I) {bool flag = false; for (int j = I + 1; j <n; ++ j) {if (arr [I]> arr [j]) {int t = arr [j]; arr [j] = arr [I]; arr [I] = t; flag = true;} if (! Flag) break ;}}} Insertion Sort: Insertion Sort is also an average O (n ^ 2) sorting algorithm. "It works by building an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it. Insert sorting usually uses in-place sorting (that is, sorting of the extra space of O (1). Therefore, during the scanning from the back to the forward, the sorted elements need to be moved backward repeatedly to provide the insert space for the new elements." This image on the Wiki intuitively explains insertion sorting: The algorithm steps are as follows: 1. From the ordered series {a [0]} and the unordered series {a [1], a [2], a [3],..., A [n-1]} starts sorting. 2. when processing the I element (I =, 3 ,..., N-1), number of columns {a [0], a [1], a [2],…, A [I-1]} is ordered, and the series {a [I], a [I + 1],..., A [n-1]} is unordered. With a [I] And a [I-1], a [I-2],..., A [0] is compared to find a suitable position to insert a [I]. 3. Repeat the second step to perform n-I insertion, and the sequence is all ordered. Complexity: Worst time complexity: O (n ^ 2) optimum time complexity: O (n ^ 2) Average time complexity: O (n ^ 2) Stability: stable implementation: [cpp] void insertSort (int arr [], int n) {for (int I = 1; I <n; I ++) if (arr [I] <arr [I-1]) {int j, t = arr [I]; for (j = I-1; j> = 0 & arr [j]> t; j --) arr [j + 1] = arr [j]; arr [j + 1] = t ;}} selection Sort: the basic idea is: "First, find the smallest (large) element in the unordered sequence and store it to the starting position of the sorting sequence. Then, then, find the smallest (large) element from the remaining unordered elements and put it at the end of the sorted sequence. And so on until all elements are sorted ." Similar to bubble, it is easy to understand, but it is much less frequently exchanged than bubble. When n is relatively small, the sorting is obviously faster than bubble. Complexity: Worst time complexity: O (n ^ 2) optimum time complexity: O (n ^ 2) Average time complexity: O (n ^ 2) stability: why is instability? Let's look at an example: select and sort 4 5 6 4 2 3. At the beginning, we found the minimum element 2 and exchanged it with the first element 4. Then the sequence became 2 5 6 4 4 2 3, as if there was no problem, right? But now, the location of the two 4 s has changed compared with the original location! Therefore, sorting is unstable. Pay attention to this, because sometimes it may cause some problems without notice. Implementation: [cpp] void selectionSort (int arr [], int n) {for (int I = 0; I <n; I ++) {int index = I; // find the index of the smallest element for (int j = I + 1; j <n; ++ j) {if (arr [index]> arr [j]) index = j;} // swap the minimum element found with arr [I] if (index! = I) {int t = arr [index]; arr [index] = arr [I]; arr [I] = t ;}}}

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.