Basic sorting (bubbling, selection, insertion) study notes

Sorting: An operation often performed in a computer, the purpose of which is to adjust a set of "out of order" record sequences to an "ordered" record sequence.

The simplest sorting algorithm is: bubble sorting, selection sorting and insertion sorting.

Bubble Sort

The basic idea: Two numbers compare in size, the larger one sinks, and the smaller one rises.

  • Compare the two adjacent data, if the second number is small, exchange positions.
  • Compare two pairs from back to front, all the way to the first two data. Eventually the smallest number is swapped to the frontmost position, so we put the smallest number at the frontmost position.
  • Continue to repeat the above process, we can achieve the second \ (2 \) small number in the second \ (2 \) position in the second round, and the \ (3 \) small number in the third round. The \ (3 \) position, ..., after performing the \ (n-1 \) round, we can ensure that the \ (n \) number is arranged in order from small to large.

The implementation code is as follows:

#include <bits/stdc++.h>
using namespace std;
int n, a[1000];
int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    for (int i = 1; i < n; i ++) {  // 需要执行n-1轮
        for (int j = n-1; j >= i; j --) {
            if (a[j] > a[j+1]) {
                swap(a[j], a[j+1]); // swap函数用于交换两个数
            }
        }
    }
    for (int i = 1; i <= n; i ++) cout << a[i] << " ";
    return 0;
}

Select sort

Basic idea:

  • In an unordered array of length \ (n \) , after the first traversal, \ (n-1 \) number, find the smallest value and exchange it with the first element;
  • After the second traversal \ (n-2 \) number, find the smallest value and exchange it with the second element;
  • ……
  • Iterate through the last number in the \ (n-1 \) time, find the smallest value and exchange it with the \ (n-1 \) th element, the sorting is completed.

The implementation code is as follows:

#include <bits/stdc++.h>
using namespace std;
int n, a[1000];
int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    for (int i = 1; i < n; i ++) {  // 执行n-1轮
        for (int j = i+1; j <= n; j ++) {   // j从i+1到n遍历
            if (a[i] > a[j]) {  // 如果a[i]比a[j]大,交换他俩的值
                swap(a[i], a[j]);   // 这样能够该轮循环结束时保证a[i]比它后面的所有数都要小
            }
        }
    }
    for (int i = 1; i <= n; i ++) cout << a[i] << " ";
    return 0;
}

Insert sort

Basic idea:

In a group of numbers to be sorted, assuming that the first n-1 numbers are already sorted, now insert the nth number into the previous ordered number sequence, so that the n numbers are also sorted. Repeat this cycle until all are in order.

We can follow the example of rational playing cards to remember the insertion order visually:

The implementation code is as follows:

#include <bits/stdc++.h>
using namespace std;
int n, a[1000];
int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    for (int i = 1; i <= n; i ++) {
        int j = i, tmp = a[i];
        while (j>1 && a[j-1]>tmp) {
            a[j] = a[j-1];
            j --;
        }
        a[j] = tmp;
    }
    for (int i = 1; i <= n; i ++) cout << a[i] << " ";
    return 0;
}

Guess you like

Origin www.cnblogs.com/quanjun/p/12712451.html