快速排序法C++代码

#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <stack>
#include <list>
#include <unordered_set>
#include<algorithm>
#include <algorithm>
#include <math.h>  
#include <bitset>
#include <memory>
#include "math.h"
using namespace std;


class  Solution
{
public:

	//函数名称:Qsort()
	//入参:    数组
	//返回值:  无
	//备注:    完成数组排序
	void QuickSort(vector<int> &array) {
		Qsort(array, 0, array.size() - 1);
	}


	//函数名称:Qsort()
	//入参:    数组,起始值
	//返回值: 无
	//备注:   通过递归调用,完成数组排序
	void Qsort(vector<int> &array, int start, int end) {
		if (start >= end) {
			return;
		}
		int pivotkey = Partition(array, start, end); //找到我的小枢纽
		Qsort(array, start, pivotkey - 1);    //对枢纽左边的数组进行排序
		Qsort(array, pivotkey + 1, end);      //对枢纽右边的数组进行排序
	}

	//函数名称:Partition()
	//入参 :  数组,起始值
	//返回值: 枢纽的位置
	//备注:   数组排成枢纽左边的都比枢纽值小,枢纽右边的都比枢纽值大
	int Partition(vector<int> &array, int start, int end) {
		int pivotkey;//定义一个枢纽
		pivotkey = array[start];//第一个值当成枢纽
		while (start < end) {   //从表的两端向中间扫描
			while (start < end && array[end] >= pivotkey) {
				end--;
			}
			swap(array[start], array[end]);   //把比枢纽小的值交换到低端

			while (start < end && array[start] <= pivotkey) {
				start++;
			}
			swap(array[start], array[end]);   //把比枢纽大的值交换到高端
		}
		return start;                         //前后指针重合的位置,就是枢纽的位置
	}


};

int main()
{
	Solution solo;
	vector<int> array = {50,10,90,30,70,40,80,60,20};
	solo.QuickSort(array);
	for (auto i : array) {
		cout << i << endl;
	}

    std::cout << "Hello World!\n";
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值