排序算法

快速排序算法

【算法图解】:数据结构教程李春葆版P378

#include<iostream>
#include<vector>
using namespace std;

void quicksort(vector<int> &v, int left, int right)
{
	if (left < right)
	{
		int key = v[left];
		int low = left;
		int high = right;
		while (low < high)
		{
			while(low < high && v[high] >= key)
				high--;
			v[low] = v[high];

			while (low < high && v[low] < key)
				low++;
			v[high] = v[low];
		}
		v[low] = key;
		quicksort(v, left, low - 1);
		quicksort(v, low + 1, right);
	}
}

int main()
{
	vector<int> num = { 6, 8, 7, 9, 0, 1, 3, 2, 4, 5 };
	quicksort(num, 0, num.size() - 1);
	for (auto c : num)
		cout << c << " ";
	cout << endl;
	return 0;
}

  

原文地址:https://www.cnblogs.com/sunbines/p/9235834.html