快排在面試時候或者筆試時經常遇到,代碼就是不會寫也要記住思想怎麼排。用筆和紙好好畫畫,最經典的排序。
8642
時間限制:1000MS
題型:編程題
Sample Input 10 5 4 8 0 9 3 2 6 7 1
Sample Output 1 4 2 0 3 5 9 6 7 8
0 1 2 4 3 5 9 6 7 8
0 1 2 4 3 5 9 6 7 8
0 1 2 3 4 5 9 6 7 8
0 1 2 34 5 8 6 7 9
0 1 2 3 4 5 7 6 8 9
0 1 2 3 4 5 6 7 8 9
Hint
作者 yqm
答案:
#include
#include
#define OK 1
#define ERROR 0
#define ElemType int
typedef struct
{
int *elem;
int length;
int listsize;
}SqList;
int InitList_Sq(SqList &L,int m)
{
L.elem=(ElemType*)malloc(m*sizeof(ElemType));
if(!L.elem)
return ERROR;
L.length=0;
L.listsize=m;
return OK;
}
int Load_Sq(SqList &L)
{
int i;
if(L.length==0)
printf("The List is empty!");
else
{
for(i=0;i
}
printf("\n");
return OK;
}
int Partition(SqList &L,int low,int high)
{
int m,pivotkey=L.elem[low];
while(low
while(low
high--;
{m=L.elem[low];L.elem[low]=L.elem[high];L.elem[high]=m;}
while(low
{m=L.elem[low];L.elem[low]=L.elem[high];L.elem[high]=m;}
}
return low;
}
void QSort(SqList &T,int low,int high)
{
int pivotloc;
if(low
pivotloc=Partition(T,low,high);
Load_Sq(T);
QSort(T,low,pivotloc-1);
QSort(T,pivotloc+1,high);
}
}
int main()
{
SqList T;
int m,i;
ElemType e, x;
scanf("%d",&m);
if(InitList_Sq(T,m)) // 判斷順序表是否創建成功
{
for(i=0;i
scanf("%d",&x);
T.elem[i] = x;
}
T.length = m;
QSort(T,0,m - 1);
}
}