C++ Basic Algorithm ④—Sorting Algorithm (Insertion, Bucket Attached Complete Code)

Sorting Algorithm

1. Insertion sort

2. Bucket sorting


1. Insertion sort

Basic idea : Divide the initial data into an ordered part and an unordered part; at each step, insert the first value of the unordered part into the previously sorted ordered part until all elements are inserted.
The steps are as follows :

  1. Each time, the first value is taken from the unordered part, and then compared with the values ​​in the ordered part from back to front;
  2. Ordered partial comparison: exchange values ​​after the current is greater than;
  3. Repeat steps ①②; until there is no more exchange, the cycle ends!
  4. Finally output the loop result.

If there are 6 element values ​​[5,2,3,9,4,7]  , the ordered part is [2,3,5,9] and the unordered part is [4,7]; next, the unordered part The " 4 " element in the part is inserted into the sorted part to show the operation process of insertion sort.

Insert image description here

  1. Among them, light green represents the ordered part and yellow represents the disordered part.
  2. Select elements from the unordered part to be inserted into the ordered part.

Insert image description here

  1. Compares the inserted element with the element of the nearest sorted part to the left. Since 4 < 9, 9 moves backward and 4 moves forward.
    •  Insert image description here
  2.  Continue the comparison with the elements of the nearest sorted part to the left. Since 4 < 5, 5 moves backward and 4 continues to move forward.
    1. Insert image description here
  3. Continue comparing 4 to 3. Since 4 > 3, there is no forward comparison and is inserted at the current position. At this time, the ordered part changes from [2,3,5,9] to [2,3,4,5,9].
    1. Insert image description here
//3.插入排序 
#include<iostream>
using namespace std;
int a[1000],n;
int main(){
	cin>>n;
	for(int i=0;i<n;i++){ //1.输入值到数组 
		cin>>a[i];
	}
	for(int i=1;i<n;i++){
		for(int j=i-1;j>=0;j--){
			if(a[j+1]<a[j]){ // 后一个数小于前一个数 
				swap(a[j+1],a[j]); // 交换 
			}
			else{ //因都是有序的,都满足后数>前数,那循环结束了。 
				break;
			} 
		}
	}
	for(int i=0;i<n;i++){
		cout<<a[i]<<" ";
	} 
}

  1. Stability : When using insertion sort, when elements are moved from the unordered part to the ordered part, they must be unequal (greater than or less than) before they are moved. If they are equal, they will not be processed, so direct insertion sorting is stable.

  2. Time complexity : The time complexity of selection sort is O( n^{2}).

  3. Applicable scenario : The sequence to be sorted has a small number of elements (<=50), and the elements are basically in order.


2. Bucket sorting

Basic idea : Put the value k to be sorted into the k-th bucket. The bucket number is the value to be sorted. The values ​​of each bucket are output sequentially to obtain an ordered sequence.

For example, if there is [5,4,9,4], put the values ​​into bucket a in sequence; it can be seen that  a[4] = 2, a[5] = 1, a[9] = 1 ;

When outputting, the bucket numbers 4 4 5 9 are output in order;
steps:

  1. First create the b bucket array and initialize the value to 0.
  2. Put the k value into the k-th bucket b [k]++ , and accumulate the same bucket number entered.
  3. k is the input bucket number, which can also be represented by a[i], b[a[i]]++.
  4. When the number of bucket numbers  b[i]!=0, it means that the i-th bucket has a value, and the bucket number is output;
  5. After output, there is one less value to subtract, namely b[i]--.

Programming to sort numbers within 10,000:

#include<iostream>
using namespace std;
int a[100000],b[100000],n;
int main(){
	cin>>n; 
	for(int i=1; i<=n; i++){
		cin >> a[i]; //输入桶号
		b[a[i]]++; // 统计桶号数量1 
	}
	for(int i=1; i<=10000; i++){
		while(b[i]!=0){ //当有桶号 
			cout<<i<<" "; //输出桶号值
			b[i]--;  //桶号数量减1
		}
	}
	return 0;
} 

  1. Time complexity : The time complexity of selection sort is O(n).
  2. Applicable scenarios : Bucket sorting is suitable for sorting scenarios with a general amount of data.

Guess you like

Origin blog.csdn.net/weixin_44775255/article/details/129693678