(分享)快速排序的最原始实现, 不带优化的
各种排序是常见的面试题, 我希望能够帮助大家轻松的秒杀那些小菜鸟//快速排序的划分过程 属于 前序遍历二叉树。/
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 15
#define less(A, B) ((A) < (B))
#define exch(A, B) {char t = A; A = B; B = t;}
void quickSort(char a[], int left, int right);
int partition(char a[], int left, int right);
int main(void)
{
char a[MAX_NUM+1] = "ASORTINGEXAMPLE";
int i;
quickSort(a, 0, MAX_NUM-1, 1);
for (i = 0; i < MAX_NUM; i++)
{
printf("%c ", a[i]);
}
getchar();
return 0;
}
int partition(char a[], int left, int right)
{
int i = left-1, j = right;
char v = a[right];
while(1)
{
while (less(a[++i], v))
{
continue;
}
while (less(v, a[--j]))
{
if (j == left)
break;
}
if (i >= j)
{
break;
}
exch(a[i], a[j]);
}
exch(a[i], a[right]);
return i;
}
void quickSort(char a[], int left, int right)
{
int i;
if (right <= left)
return;
i = partition(a, left, right);
quickSort(a, left, i-1);
quickSort(a, i+1, right);
}
快速排序子文件结构:
ASORTINGEXAMPLE
AAE
AA
TINGOXSMPLR
LINGOPM
LIG
IL
OPN
PO
XTS
TX
快速排序子文件排序过程
AAEETINGOXSMPLR
AAE
AA
LINGOPMRXTS
LIGMOPN
GIL
IL
NPO
OP
STX
TX
如果按照正序/逆序文件调用排序函数, 那么快速排序的复杂度会退化为 0(n^2), 当然这里只是泛泛的说法,/
如下
abcdefghijklmno
abcdefghijklmn
abcdefghijklm
abcdefghijkl
abcdefghijk
abcdefghij
abcdefghi
abcdefgh
abcdefg
abcdef
abcde
abcd
abc
ab
onmlkjihgfedcba
nmlkjihgfedcbo
nmlkjihgfedcb
mlkjihgfedcn
mlkjihgfedc
lkjihgfedm
lkjihgfed
kjihgfel
kjihgfe
jihgfk
jihgf
ihgj
ihg
hi
aaaaaaaaaaaaaaa
aaaaaaa
aaa
aaa
aaaaaaa
aaa
aaa
[ 本帖最后由 BlueGuy 于 2011-1-8 13:07 编辑 ]