C++快速排序

来源:互联网 发布:mac osx vmware tools 编辑:程序博客网 时间:2024/05/02 02:10

#include<iostream>

#include<vector>


using namespace std;


void quicksort(vector<int> &v,int left, int right){

        if (left >= right)

        {

                return;

        }

        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> v;

        int i =0;

        v.push_back(3);

        v.push_back(4);

        v.push_back(6);

        v.push_back(9);

        v.push_back(3);

       for(;i<v.size();i++){

                cout<<v[i]<<"\t";

        }

        cout<<endl;

        quicksort(v,0,v.size()-1);

        for(i=0;i<v.size();i++){

                cout<<v[i]<<"\t";

        }

        cout<<endl;

        return0;

}

原创粉丝点击