快排

发布于 / Algorithm / 0 条评论
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
void quick_sort(vector<int> &v,int low,int high){
	int i=low;
	int j=high;
	if(i>=j) return;
	int base = v[low];
	while (i<j) {
		while (i<j && v[j]>=base) {
			j--;
		}
		swap(v[j], v[i]);
		while (i<j && v[i]<=base) {
			i++;
		}
		swap(v[i], v[j]);
	}
	quick_sort(v, low, i-1);
	quick_sort(v, i+1, high);
	
	
	
}


int main(int argc, char *argv[]) {
	vector<int>v;
	int n;
	for(int i=0;i<6;i++){
		cin>>n;
		v.push_back(n);
	}
	quick_sort(v, 0, 5);
	cout<<v[0];
	for(int j=1;j<6;j++)
	cout<<" "<<v[j];
	cout<<endl;
}
转载原创文章请注明,转载自: 静沐暖阳 » 快排
Not Comment Found