浙大数据结构学习:快速排序

09-排序1 排序 (25 分)

给定N个(长整型范围内的)整数,要求输出从小到大排序后的结果。

本题旨在测试各种不同的排序算法在各种数据情况下的表现。各组测试数据特点如下:

数据1:只有1个元素;
数据2:11个不相同的整数,测试基本正确性;
数据3:103个随机整数;
数据4:104个随机整数;
数据5:105个随机整数;
数据6:105个顺序整数;
数据7:105个逆序整数;
数据8:105个基本有序的整数;
数据9:105个随机正整数,每个数字不超过1000。
输入格式:
输入第一行给出正整数N(≤10^5​​ ),随后一行给出N个(长整型范围内的)整数,其间以空格分隔。

输出格式:
在一行中输出从小到大排序后的结果,数字间以1个空格分隔,行末不得有多余空格。

输入样例:
11
4 981 10 -17 0 -20 29 50 8 43 -5
输出样例:
-20 -17 -5 0 4 8 10 29 43 50 981

按照自己的理解写的快排代码

#include <iostream>
#define MaxSize 100000
int a[MaxSize];

using namespace std;

void GetData(int a[], int N) {
	for (int i = 0; i < N; i++) {
		cin >> a[i];//这里数据不需要后移
	}
}

void DispArr(int a[], int N) {
	cout << a[0];
	N--;
	int i = 1;
	while (N--) {
		cout << " " << a[i++];
	}
}

void swap(int* val1, int* val2) {
	int tmp = *val1;
	*val1 = *val2;
	*val2 = tmp;
}

int Median3(int a[], int left, int right) {
	/*对数组a[],在a[left],a[right],a[center]中进行排序,返回中位数作为pivot*/
	int center = (left + right) / 2;
	if (a[left] > a[center]) swap(&a[left], &a[center]);
	if (a[left] > a[right]) swap(&a[left], &a[right]);
	if (a[center] > a[right]) swap(&a[center], &a[right]);
	/*将a[center]藏到a[right-1]*/
	swap(&a[center], &a[right - 1]);
	return a[right - 1];
}

void QSort(int a[], int left, int right) {
	if (left >= right) return;
	int pivot = Median3(a, left, right);
	int low, high;
	low = left;
	high = right - 2;
		while (low <= high) {
			if (a[low] >= pivot) {
				if (a[high] <= pivot) {
					swap(&a[low], &a[high]); high--; low++;
				}
				else high--;
			}
			else low++;
		}
		swap(&a[low], &a[right - 1]);
		//cout << "过程结果:"; DispArr(a, right - left + 1); cout << endl;
		QSort(a, left, low - 1);
		QSort(a, low + 1, right);
}

void QuickSort(int a[], int N) {
	QSort(a, 0, N - 1);
}

int main() {
	int N;
	cin >> N;
	GetData(a, N);
	QuickSort(a, N);
	DispArr(a, N);
	return 0;
}

测试结果

快速排序(自写)

在这里插入图片描述

另外的补充

第二个测试点偶尔会出错,不知道是为什么,不过快排确实考验细节。有时候由于程序书写会导致死循环的特点,尤其是QSort(a,0,1)的情况,因此在小于一定规模的时候使用插入排序也算是一种解决方法。

#include <iostream>
#define MaxSize 100000
int a[MaxSize];

using namespace std;

void GetData(int a[], int N) {
	for (int i = 0; i < N; i++) {
		cin >> a[i];//这里数据不需要后移
	}
}

void DispArr(int a[], int N) {
	cout << a[0];
	N--;
	int i = 1;
	while (N--) {
		cout << " " << a[i++];
	}
}

void swap(int* val1, int* val2) {
	int tmp = *val1;
	*val1 = *val2;
	*val2 = tmp;
}

int Median3(int a[], int left, int right) {
	/*对数组a[],在a[left],a[right],a[center]中进行排序,返回中位数作为pivot*/
	int center = (left + right) / 2;
	if (a[left] > a[center]) swap(&a[left], &a[center]);
	if (a[left] > a[right]) swap(&a[left], &a[right]);
	if (a[center] > a[right]) swap(&a[center], &a[right]);
	/*将a[center]藏到a[right-1]*/
	swap(&a[center], &a[right - 1]);
	return a[right - 1];
}

void QSort(int a[], int left, int right) {
	if (left >= right) return;
	int pivot = Median3(a, left, right);
	int low, high;
	low = left;
	high = right - 2;
		while (low <= high) {
			if (a[low] >= pivot) {
				if (a[high] <= pivot) {
					swap(&a[low], &a[high]); high--; low++;
				}
				else high--;
			}
			else low++;
		}
		swap(&a[low], &a[right - 1]);
		QSort(a, left, low - 1);
		QSort(a, low + 1, right);
}

void QSort2(int a[], int left, int right) {
	if (left >= right) return;
	/*对数组a[],在[left,right]区间进行快速排序*/
	int pivot = Median3(a, left, right);
	int low = left;
	int high = right - 1;
	while (1) {
		while (a[++low] < pivot);//退出循环时a[low]>=pivot
		while (a[--high] > pivot);//退出循环时a[high]<=pivot
		if (high > low)swap(&a[low], &a[high]);
		else break;
	}
	if (low < right - 1) {
		swap(&a[low], &a[right - 1]);
	}
	QSort2(a, left, low - 1);
	QSort2(a, low + 1, right);
}

void QuickSort(int a[], int N) {
	QSort(a, 0, N - 1);
}

void QuickSort2(int a[], int N) {
	QSort2(a, 0, N - 1);
}
int main() {
	int N;
	cin >> N;
	GetData(a, N);
	QuickSort2(a, N);
	DispArr(a, N);
	return 0;
}

在这里插入图片描述

加入了插入排序的代码

#include <iostream>
#define MaxSize 100000
int a[MaxSize];

using namespace std;

void GetData(int a[], int N) {
	for (int i = 0; i < N; i++) {
		cin >> a[i];//这里数据不需要后移
	}
}

void DispArr(int a[], int N) {
	cout << a[0];
	N--;
	int i = 1;
	while (N--) {
		cout << " " << a[i++];
	}
}

void swap(int* val1, int* val2) {
	int tmp = *val1;
	*val1 = *val2;
	*val2 = tmp;
}

int Median3(int a[], int left, int right) {
	/*对数组a[],在a[left],a[right],a[center]中进行排序,返回中位数作为pivot*/
	int center = (left + right) / 2;
	if (a[left] > a[center]) swap(&a[left], &a[center]);
	if (a[left] > a[right]) swap(&a[left], &a[right]);
	if (a[center] > a[right]) swap(&a[center], &a[right]);
	/*将a[center]藏到a[right-1]*/
	swap(&a[center], &a[right - 1]);
	return a[right - 1];
}

void QSort(int a[], int left, int right) {
	if (left >= right) return;
	int pivot = Median3(a, left, right);
	int low, high;
	low = left;
	high = right - 2;
		while (low <= high) {
			if (a[low] >= pivot) {
				if (a[high] <= pivot) {
					swap(&a[low], &a[high]); high--; low++;
				}
				else high--;
			}
			else low++;
		}
		swap(&a[low], &a[right - 1]);
		QSort(a, left, low - 1);
		QSort(a, low + 1, right);
}

void QSort2(int a[], int left, int right) {
	if (left >= right) return;
	/*对数组a[],在[left,right]区间进行快速排序*/
	int pivot = Median3(a, left, right);
	int low = left;
	int high = right - 1;
	while (1) {
		while (a[++low] < pivot);//退出循环时a[low]>=pivot
		while (a[--high] > pivot);//退出循环时a[high]<=pivot
		if (high > low)swap(&a[low], &a[high]);
		else break;
	}
	if (low < right - 1) {
		swap(&a[low], &a[right - 1]);
	}
	QSort2(a, left, low - 1);
	QSort2(a, low + 1, right);
}

void InsertSort(int a[], int N) {
	int tmp;
	int i, j;
	for (i = 1; i < N; i++) {
		tmp = a[i];
		for ( j = i; j > 0; j--) {
			if (tmp < a[j - 1])a[j] = a[j - 1];
			else break;
		}
		a[j] = tmp;
	}
}


void QSort3(int a[], int left, int right) {
	int cutoff = right - left + 1;
	if (cutoff >= 100) {
		/*对数组a[],在[left,right]区间进行快速排序*/
		int pivot = Median3(a, left, right);
		int low = left;
		int high = right - 1;
		while (1) {
			while (a[++low] < pivot);//退出循环时a[low]>=pivot
			while (a[--high] > pivot);//退出循环时a[high]<=pivot
			if (high > low)swap(&a[low], &a[high]);
			else break;
		}
		if (low < right - 1) {
			swap(&a[low], &a[right - 1]);
		}
		QSort3(a, left, low - 1);
		QSort3(a, low + 1, right);
	}
	else {
		InsertSort(a + left, right - left + 1);
	}
}



void QuickSort(int a[], int N) {
	QSort(a, 0, N - 1);
}

void QuickSort2(int a[], int N) {
	QSort2(a, 0, N - 1);
}
void QuickSort3(int a[], int N) {
	QSort3(a, 0, N - 1);
}
int main() {
	int N;
	cin >> N;
	GetData(a, N);
	QuickSort3(a, N);
	DispArr(a, N);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值