快速排序

来源:互联网 发布:淘宝助理5.8图片搬家 编辑:程序博客网 时间:2024/06/09 16:38

这里写图片描述
这里写图片描述
这里写图片描述

分治思想:涉及递归,例如归并排序、堆排序、快速排序等

#include<stdio.h>int Partion(int a[],int low,int high){    int i,j,t;    j=low-1;    for(i=low;i<=high;i++){        if(a[i]<=a[high]){            j++;            t=a[i];            a[i]=a[j];            a[j]=t;        }    }    return j;}void QuickSort(int a[],int low,int high){    int pivot;    if(low<high){        pivot=Partion(a,low,high);        QuickSort(a,low,pivot-1);        QuickSort(a,pivot+1,high);    }}int main(){    int i,j,t;    int size=5;    int a[5]={        2,1,5,3,8    };    QuickSort(a,0,4);    for(i=0;i<size;i++){        printf("%d ",a[i]);    }} 
//写在一起void QuickSort(int a[],int low,int high){    int i,j,t;    j=low-1;    for(i=low;i<=high;i++){        if(a[i]<=a[high]){            j++;            t=a[i];            a[i]=a[j];            a[j]=t;        }    }    if(low<high){        QuickSort(a,low,j-1);        QuickSort(a,j+1,high);    }}

这里写图片描述

0 0
原创粉丝点击