quick_sort

文章介绍了快速排序算法的C++实现(quick_sort1),以及两个优化版本(quick_sort2和quick_sort3),后者通过选择合适的基准元素来提高效率。
摘要由CSDN通过智能技术生成

quick_sort

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void quick_sort1(int* a, int n) {
	if (n < 2)
		return;
	int l = 0,
		r = n - 1;
	int t = a[0];
	int f = 2;

	while(l < r){
		if (f == 2)
		{
			if (a[r] >= t)
			{
				r--;
				continue;
			}

			/*a[r] < t,a[r]放入左边*/
			a[l] = a[r];
			f = 1;//左下标移动
			l++;
			continue;
		}

		if (f == 1)
		{
			if (a[l] <= t)
			{
				l++;
				continue;
			}

			/*a[l] > t,a[l]放入右边*/
			a[r] = a[l];
			f = 2;//右下标移动
			r--;
			continue;
		}
	}

	a[l] = t;//a[r]=t;

	/*分比t小,比t大的进行递归*/
	quick_sort1(a, l);
	quick_sort1(a + l + 1, n - l - 1);

	return;
}

//optimize1: quick_sort1 + insert_sort
void insert_sort(int* a, int n) {
	if (n < 2)
		return;
	int t, j;
	for (int i = 1; i < n; i++) {
		t = a[i];
		for (j = i - 1; j >= 0 && t < a[j]; j--)
			a[j + 1] = a[j];
		a[j + 1] = t;
	}
	return;
}

void quick_sort2(int* a, int n) {
	
	int l = 0,
		r = n - 1;
	int t = a[0];
	int f = 2;
	while (l < r) {
		if (f == 2)
		{
			if (a[r] >= t)
			{
				r--;
				continue;
			}

			/*a[r] < t,a[r]放入左边*/
			a[l] = a[r];
			f = 1;//左下标移动
			l++;
			continue;
		}

		if (f == 1)
		{
			if (a[l] <= t)
			{
				l++;
				continue;
			}

			/*a[l] > t,a[l]放入右边*/
			a[r] = a[l];
			f = 2;//右下标移动
			r--;
			continue;
		}
	}
	a[l] = t;//a[r]=t;

	if (l<10)
		insert_sort(a, l);
	else
		quick_sort1(a, l);
	if (n - l - 1 < 10)
		insert_sort(a + l + 1, n - l - 1);
	else
	quick_sort1(a + l + 1, n - l - 1);
}
//above optimize1

//optimize2:select reasonable reference_elem

void quick_sort3(int* a, int n) {
	if (n < 2)
		return;

	//select reasonable reference_elem
	int c = 0;
	vector<int> v;
	for(int i=0;i<n;i+=5)//5 or 3 or...
		v.push_back(i);
	sort(v.begin(), v.end());
	int vl = v.size();
	for (int i = 0; i < n; i += 5) {
		if (a[i] == v[vl / 2]){
			c = i;
			break;
		}
	}
	int t = a[c];
	//
	int l = 0,
		r = n - 1;
	int f = 2;
	a[c] = a[l];
	while (l < r) {
		//while (r) {
		//	if (a[r] < t) {
		//		a[l] = a[r];
		//		break;
		//	}
		//	r--;
		//}
		//
		//if (r == l)
		//	break;
		//else {
		//	l++;
		//	while (l < r) {
		//		if (a[l] > t) {
		//			a[r] = a[l];
		//			break;
		//		}
		//		l++;
		//	}
		//}
		//
		//if (r == l)
		//	break;
		//else
		//	r--;
		if (f == 2)
		{
			if (a[r] >= t)
			{
				r--;
				continue;
			}

			/*a[r] < t,a[r]放入左边*/
			a[l] = a[r];
			f = 1;//左下标移动
			l++;
			continue;
		}

		if (f == 1)
		{
			if (a[l] <= t)
			{
				l++;
				continue;
			}

			/*a[l] > t,a[l]放入右边*/
			a[r] = a[l];
			f = 2;//右下标移动
			r--;
			continue;
		}
	}
	a[l] = t;
	quick_sort3(a, l);
	quick_sort3(a + l + 1, n - l - 1);
}

int main() {
	int a[] = { 44,3,38,5,47,15,36,26,27,2,46,4,19,50,48 };
	//quick_sort1(a, sizeof(a) / sizeof(a[0]));
	//for (const auto& e : a)
	//	cout << e << " ";
	//cout << endl;

	//int b[] = { 44,3,38,5,47,15,36,26,27,2,46,4,19,50,48 };
	//quick_sort2(b, sizeof(b) / sizeof(b[0]));
	//for (const auto& e : b)
	//	cout << e << " ";
	//cout << endl;

	int c[] = { 44,3,38,5,47,15,36,26,27,2,46,4,19,50,48 };
	quick_sort3(c, sizeof(c) / sizeof(c[0]));
	for (const auto& e : c)
		cout << e << " ";
	cout << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值