数据结构与算法

排序

快速排序

参考文章:

https://blog.csdn.net/weixin_43296982/article/details/123098472

#define _CRT_SECURE_NO_WARNINGS
#include
#include
void q_sort(int* a, int left, int right) {
	int cen = a[left];
	int i = left;
	int j = right;
	if (i > j)
		return;
	while (i != j) {
		while (a[j] >= cen && i < j)
			j--;
		while (a[i] <= cen && i < j)
			i++;
		
		if (i < j) {
			int temp = a[i];
			a[i] = a[j];
			a[j] = temp;
		}
	}
	a[left] = a[i];
	a[i] = cen;
	q_sort(a, left, i - 1);
	q_sort(a, i + 1, right);
}
int main() {
	int n;
	scanf("%d", &n);
	int* a = (int*)malloc(sizeof(int) * n);
	for (int i = 0; i < n; i++)
		scanf("%d", &a[i]);
	q_sort(a, 0, n - 1);
	for (int i = 0; i < n; i++)
		printf("%d ", a[i]);
	//printf("%d\n",a[n-1]);
	return 0;
}

你可能感兴趣的:(Specialization,算法,c++,c语言)