直接插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<bits/stdc++.h>

using namespace std;

void insert_sort(vector<int> &nums, int n) {
int i, j;
for(i = 0;i<n-1;i++) {
if(nums[i+1] < nums[i]){
int temp = nums[i+1];
for(j=i; j>=0 && nums[j] > temp;j--) {
nums[j+1] = nums[j];
}
nums[j+1] = temp;
}
}
}

int main() {
vector<int> nums = {3, 5, 1, 4, 8, 6, 2};
insert_sort(nums, nums.size());
for(int i=0;i<nums.size();i++) {
cout << nums[i] << " ";
}
cout << endl;

return 0;
}

折半插入排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<vector>

using namespace std;

void zheban_insert_sort(vector<int> &nums, int n) {
int i, j, low, high, mid;
for(i = 0;i < n-1;i++) {
if(nums[i+1] < nums[i]) {
int temp = nums[i+1];
low = 0, high = i;
while(low <= high) {
mid = (low + high) / 2;
if(nums[mid] > temp) high = mid - 1;
else low = mid + 1;
}
for(j = i;j>=high+1;j--) {
nums[j+1] = nums[j];
}
nums[j+1] = temp;
}
}
}

int main() {
vector<int> nums = {3, 5, 1, 4, 8, 6, 2};
zheban_insert_sort(nums, nums.size());
for(int i=0;i<nums.size();i++) {
cout << nums[i] << " ";
}
cout << endl;
return 0;
}

希尔排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<vector>

using namespace std;


void shell_sort(int nums[], int n){
int j=0;
for(int dk = n/2; dk>=1; dk=dk/2){
for(int i=dk;i<n;i++){
if(nums[i] < nums[i-dk]){
int temp = nums[i];
for(j=i-dk;j>=0&&nums[j] > temp;j-=dk) {
nums[j+dk] = nums[j];
}
nums[j+dk] = temp;
}
}
}
}

int main() {
int nums[] = {3, 5, 1, 4, 8, 6, 2};
shell_sort(nums, 7);
for(int i=0;i<7;i++){
printf("%d ", nums[i]);
}
printf("\n");
system("pause");
return 0;
}

冒泡排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<vector>

using namespace std;

void bubble_sort(int nums[], int n) {
for(int i = 0;i<n-1;i++){
bool flag = false;
for(int j=n-1;j>=i + 1;j--){
if(nums[j] < nums[j - 1]){
int temp = nums[j];
nums[j] = nums[j-1];
nums[j-1] = temp;
flag = true;
}
}
if(flag == false) {
return;
}
}
}

int main() {
int nums[] = {3, 5, 1, 4, 8, 6, 2};
bubble_sort(nums, 7);
for(int i=0;i<7;i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}

快速排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<vector>

using namespace std;
int partition(int nums[], int low, int high);
void quick_sort(int nums[], int low, int high) {
if(low < high) {
int privot = partition(nums, low, high);
quick_sort(nums, low, privot-1);
quick_sort(nums, privot+1, high);
}
}

int partition(int nums[], int low, int high){
int privot = nums[low];
while(low < high) {
while(nums[high] >= privot && low < high) high --;
nums[low] = nums[high];
while(nums[low] <= privot && low < high) low ++;
nums[high] = nums[low];
}
nums[high] = privot;
return high;
}

int main() {
int nums[] = {3, 5, 1, 4, 8, 6, 2};
quick_sort(nums, 0, 6);
for(int i=0;i<7;i++){
printf("%d ", nums[i]);
}
printf("\n");
system("pause");
return 0;
}

简单选择排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<vector>
#include<stdio.h>

using namespace std;

void simple_select_sort(int nums[], int n){
for(int i=0;i<n;i++) {
int min = i;
for(int j=i+1; j<n;j++) {
if(nums[j] < nums[min]) min = j;
}
if(min != i) {
int temp = nums[min];
nums[min] = nums[i];
nums[i] = temp;
}
}
}

int main() {
int nums[] = {3, 5, 1, 4, 8, 6, 2};
simple_select_sort(nums, 7);
for(int i=0;i<7;i++){
printf("%d ", nums[i]);
}
printf("\n");
system("pause");
return 0;
}

堆排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<vector>

using namespace std;

void headjust(int nums[], int k, int len) {
nums[0] = nums[k];
for(int i=2*k;i<=len;i*=2) {
if(i<len && nums[i] < nums[i+1]) {
i++;
}
if(nums[0] >= nums[i]) break;
else {
nums[k] = nums[i];
k = i;
}
}
nums[k] = nums[0];
}

void build_heap(int nums[], int len){
for(int i=len/2;i>0;i--) {
headjust(nums, i, len);
}
}

void heap_sort(int nums[], int len) {
build_heap(nums, len);
for(int i=len;i>1;i--){
int temp = nums[i];
nums[i] = nums[1];
nums[1] = temp;
headjust(nums, 1, i-1);\
}
}


int main() {
int nums[] = {-1, 3, 5, 1, 4, 8, 6, 2};
heap_sort(nums, 7);
for(int i=1;i<8;i++){
printf("%d ", nums[i]);
}
printf("\n");
system("pause");
return 0;
}



归并排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<vector>

using namespace std;

int temp[7] = {0};
void merge(int nums[], int low, int mid, int high) {
for(int i=low; i<=high;i++){
temp[i] = nums[i];
}
int i=0, j=0, k=0;
for(i=low, j=mid+1, k=i;i<=mid&&j<=high;k++){
if(temp[i]<=temp[j]){
nums[k] = temp[i++];
}
else {
nums[k] = temp[j++];
}
}
while(i<=mid) nums[k++] = temp[i++];
while(j<=high) nums[k++] = temp[j++];
}
void merge_sort(int nums[], int low, int high){
if(low < high) {
int mid = (low + high) / 2;
merge_sort(nums, low, mid);
merge_sort(nums, mid + 1, high);
merge(nums, low, mid, high);
}
}



int main() {
int nums[] = {3, 5, 1, 4, 8, 6, 2};
merge_sort(nums, 0, 6);
for(int i=0;i<7;i++){
printf("%d ", nums[i]);
}
printf("\n");
system("pause");
return 0;
}


各种排序算法性质总结

sort_table