排序算法

选择排序

void xuanze_sort(int a[], int n) { // 选择排序
	for (int i = n; i >= 2; i--) {
		int mx = 1;
		for (int j = 1; j <= i; j++) {
			if (a[j] > a[mx]) mx = j;
		}
		swap(a[mx], a[i]);
	}
}

 

冒泡排序

void maopao_sort(int a[], int n) { // 冒泡排序
	for (int i = n; i >= 2; i--) {
		for (int j = 1; j < n; j++) {
			if (a[j] > a[j + 1]) swap(a[j], a[j + 1]);
		}
	}
}

插入排序

void charRu_sort(int a[], int n) { // 插入排序 
	for (int i = 2; i <= n; i++) {
		for (int j = i; j >= 2; j--) {
			if (a[j] < a[j - 1]) swap(a[j], a[j - 1]);
		}
	}
}

桶排序

void tong_sort(int a[], int n) { //桶排序
	memset(tong, 0, sizeof tong);
	int mx = 0;
	for (int i = 1; i <= n; i++) tong[a[i]]++, mx = max(mx, a[i]);
	int cnt = 0;
	for (int i = 1; i <= mx; i++) {
		if (tong[i]) {
			while (tong[i]--) a[++cnt] = i;
		}
	}
}

快速排序

void quick_sort(int a[], int l, int r) { // 快速排序
	if (l >= r) return;
	int i, j;
	i = l - 1, j = r + 1; //指针先向左有一定一位,用do循环
	int mid = l + r >> 1;
	while (i < j) {
		do i++;
		while (a[i] < a[mid]);
		do j--;
		while (a[j] > a[mid]);
		if (i < j)
			swap(a[i], a[j]);
	}
	quick_sort(a, l, j), quick_sort(a, j + 1, r);
}

归并排序

void merge_sort(int q[], int l, int r) { // 归并排序
	if (l >= r) return;

	int mid = l + r >> 1;
	merge_sort(q, l, mid);
	merge_sort(q, mid + 1, r);

	int k = 0, i = l, j = mid + 1;
	while (i <= mid && j <= r)
		if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
		else tmp[k ++ ] = q[j ++ ];

	while (i <= mid) tmp[k ++ ] = q[i ++ ];
	while (j <= r) tmp[k ++ ] = q[j ++ ];

	for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j];
}

sort

sort(q + 1, q + n);

完整程序

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;

const int N = 1e5 + 10;

int f[N], q[N], tmp[N], tong[N];
void xuanze_sort(int a[], int n) { // 选择排序
	for (int i = n; i >= 2; i--) {
		int mx = 1;
		for (int j = 1; j <= i; j++) {
			if (a[j] > a[mx]) mx = j;
		}
		swap(a[mx], a[i]);
	}
}

void maopao_sort(int a[], int n) { // 冒泡排序
	for (int i = n; i >= 2; i--) {
		for (int j = 1; j < n; j++) {
			if (a[j] > a[j + 1]) swap(a[j], a[j + 1]);
		}
	}
}

void charRu_sort(int a[], int n) { // 插入排序 
	for (int i = 2; i <= n; i++) {
		for (int j = i; j >= 2; j--) {
			if (a[j] < a[j - 1]) swap(a[j], a[j - 1]);
		}
	}
}
void tong_sort(int a[], int n) { //桶排序
	memset(tong, 0, sizeof tong);
	int mx = 0;
	for (int i = 1; i <= n; i++) tong[a[i]]++, mx = max(mx, a[i]);
	int cnt = 0;
	for (int i = 1; i <= mx; i++) {
		if (tong[i]) {
			while (tong[i]--) a[++cnt] = i;
		}
	}
}
void quick_sort(int a[], int l, int r) { // 快速排序
	if (l >= r) return;
	int i, j;
	i = l - 1, j = r + 1; //指针先向左有一定一位,用do循环
	int mid = l + r >> 1;
	while (i < j) {
		do i++;
		while (a[i] < a[mid]);
		do j--;
		while (a[j] > a[mid]);
		if (i < j)
			swap(a[i], a[j]);
	}
	quick_sort(a, l, j), quick_sort(a, j + 1, r);
}

void merge_sort(int q[], int l, int r) { // 归并排序
	if (l >= r) return;

	int mid = l + r >> 1;
	merge_sort(q, l, mid);
	merge_sort(q, mid + 1, r);

	int k = 0, i = l, j = mid + 1;
	while (i <= mid && j <= r)
		if (q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
		else tmp[k ++ ] = q[j ++ ];

	while (i <= mid) tmp[k ++ ] = q[i ++ ];
	while (j <= r) tmp[k ++ ] = q[j ++ ];

	for (i = l, j = 0; i <= r; i ++, j ++ ) q[i] = tmp[j];
}

int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> q[i];
//	xuanze_sort(q, n);
//	maopao_sort(q, n);
//	charRu_sort(q, n); 
//	tong_sort(q, n);
//	quick_sort(q, 1, n);
//	merge_sort(q, 1, n);
	sort(q + 1, q + n);
	for (int i = 1; i <= n; i++) cout << q[i] << ' ';
}

 

posted @ 2022-10-11 16:28  Luli&  阅读(144)  评论(0编辑  收藏  举报