分治算法---快速排序

//分治算法---快速排序
#include
using namespace std;
int partition(int a[],int l,int r);//对a数组[l,r]进行快速排序。 
void quicksort(int a[],int p,int r)
{
    if(pint q=partition(a,p,r);
        quicksort(a,p,q-1);
        quicksort(a,q+1,r);
    }
 }
int partition(int a[],int l,int r)
{
    int i=l;int j=r+1;
    int x=a[l];
    while(1)
    {
        while(a[++i]//这里需要注意防止访问位置出界 

        while(a[--j]>x);
        if(i>=j)
        {
            break;

        }
        int tem=a[i];
        a[i]=a[j];
        a[j]=tem;
    }
    a[l]=a[j];
    a[j]=x;
    return j;
}
int main()
{
    int a[10]={10,999,5,71,67,10,4,36,2,1000};
    quicksort(a,0,9);
    for(int i=0;i<10;i++)
    {
        cout<"  ";
    }
    cout<return 0; 
}
void quicksort(int a[],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);  
        }  
}

“`

你可能感兴趣的:(算法设计)