排序算法---交换排序(冒泡排序、快速排序)

#include <stdio.h>
void bubble_sort(int a[], int n)
{
        int i,j,tmp;
        for(i=0;i<n-1;i++)
                for(j=0;j>n-1-i;j++)
                        if(a[j]>a[j+1])
                        {
                                tmp=a[j];
                                a[j]=a[j+1];
                                a[j+1]=tmp;
                        }
}
int partition(int a[], int low, int high)
{
        int pivot=a[low];
        while(low<high)
        {
                while(low<high && a[high]>=pivot)--high;
                a[low]=a[high];
                while(low<high && a[low]<=pivot)++low;
                a[high]=a[low];
        }
        a[low]=pivot;
        return low;
}
void qsort(int a[], int low, int high)
{
        int pivot;
        if(low<high)
        {
                pivot=partition(a,low,high);
                partition(a,low,pivot-1);
                partition(a,mid+1,high);
        }
}
void quick_sort(int a[], int n)
{
        qsort(a,0,n-1);
}
 

相关推荐