快速排序

include

include

typedef int Status;
typedef int ElemTypes;

typedef struct
{

int d[10];
int length;

}SqList;

void swap(SqList L,int i,int j) /swap L->data[i] and L->data[j]*/
{

int temp=L->d[i];
L->d[i]=L->d[j];
L->d[j]=temp;

}
void print(SqList L)
{

int i;
for (i = 0; i < L.length; i++)
{
    printf("%d,",L.d[i]);
}
printf("\n");

}

Status Partition(SqList *L,int low,int high)
{

int key;
key=L->d[low];
while(low<high) 
{ 
     while(low<high&&L->d[high]>=key)
        high--;
     swap(L,low,high);
     while(low<high&&L->d[low]<=key)
        low++;
     swap(L,low,high);
}
    return low;

}

void QSort(SqList *L,int low,int high)
{

int key;
if (low<high)
{
    key=Partition(L,low,high);
    QSort(L,low,key-1);
    QSort(L,key+1,high);
}

}

void main()
{

int a[9]={50,10,90,30,70,40,80,60,20};
int i;
SqList L;
int low=0,high=8;
for(i=0;i<9;i++) L.d[i]=a[i];
L.length=9;
printf("initdata is :\n");
print(L);
Partition(&L,low,high);
QSort(&L,low,high);
print(L);

}

你可能感兴趣的:(快速排序)