首页 > 编程语言 > 详细

快速排序

时间:2021-06-02 09:50:14      阅读:25      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include<vector>
using namespace std;

void quickSort(vector<int > & vec ,int start,int end) {
	if (start < end) {
		int left = start, right = end;
		int priot = vec.at(start);
		while (left <right)
		{
			while (left < right && vec.at(right) >= priot) right--;
			if (left < right) vec.at(left++) = vec.at(right);
			while (left < right && vec.at(left) <= priot) left++;
			if (left < right) vec.at(right--) = vec.at(left);
		}
		vec.at(right) = priot;
		quickSort(vec, start, left - 1);
		quickSort(vec, left + 1, end);
	}
}

int main() {
	int a;
	vector<int > vec;
	cout << "请输入数字,进行快速排序:" << endl;
	cout << "中间用空格 比如 :15 56 18 13 69Enter" << endl;

	while (cin>>a)
	{
		vec.push_back(a);
		if (cin.get() == ‘\n‘) break;

	}
	quickSort(vec, 0, vec.size() - 1);
	cout <<endl<< "quick排序后" << endl;
	for (auto & ele : vec) {
		cout << ele << " ";
	}
	return 0;
}

快速排序

原文:https://www.cnblogs.com/appearAndLeave/p/14839100.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!