// 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAXSIZE 10
#define Sqtype int
typedef struct
{
Sqtype r[MAXSIZE+1];
int length;
}SqList;
void QSort(SqList *L,int low,int high);
int Partition(SqList *L,int low,int high);
void swap(SqList *L,int i,int j);
void QuickSort(SqList *L)
{
QSort(L,0,L->length-1);
}
void QSort(SqList *L,int low,int high)
{
int pivot;
if(low<high)
{
pivot = Partition(L,low,high);
QSort(L,low,pivot-1);
QSort(L,pivot+1,high);
}
}
int Partition(SqList *L,int low,int high)
{
Sqtype pivotkey = L->r[low];
while(low<high)
{
while(low<high&&L->r[high]>=pivotkey)
high--;
swap(L,low,high);
while(low<high&&L->r[low]<=pivotkey)
low++;
swap(L,low,high);
}
return low;
}
void swap(SqList *L,int i,int j)
{
Sqtype temp = L->r[i];
L->r[i] = L->r[j];
L->r[j] = temp;
}
int main(int argc, char* argv[])
{
SqList L;
int i=0;
Sqtype temp[MAXSIZE] = {20,90,60,70,10,50,80,40,30,0};
memcpy(L.r,temp,sizeof(temp));
L.length = MAXSIZE;
QuickSort(&L);
for(i=0;i<L.length;i++)
{
printf("%d\n",L.r[i]);
}
return 0;
}
快速排序算法
最新推荐文章于 2024-05-27 00:10:51 发布