算法2.6&2.7快速排序

来源:互联网 发布:贺卫方反对网络实名制 编辑:程序博客网 时间:2024/06/04 23:31
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>using namespace std;int S[9];void Partition(int low,int high,int &pivotpoint){    int pivotitem=S[low];    int j=low;    for(int i=low+1;i<=high;i++)//自己差点忘掉 = high    {        if(S[i]<pivotitem)        {            j++;            swap(S[i],S[j]);        }    }    pivotpoint=j;    swap(S[j],S[low]);}void quicksort(int low,int high){    int pivotpoint;    if(low<high)    {        Partition(low,high,pivotpoint);        quicksort(low,pivotpoint-1);        quicksort(pivotpoint+1,high);    }}int main(){    S[0]=0;    for(int i=1;i<=8;i++)    {        cin>>S[i];    }    quicksort(1,8);    for(int i=1;i<=8;i++)    {        cout<<S[i]<<endl;    }    return 0;}
0 0
原创粉丝点击