#define MAX_SIZE 10
typedef struct
{
int m[MAX_SIZE ];
int length;
} SqList;
//用排序,铁定跑不了这个swap函数,最好自己写一个
void swap(SqList *q, int i, int j);
void swap(SqList *q, int i, int j)
{
int temp = q->m[i];
q->m[i] = q->m[j];
q->m[j] = temp;
{
QSort(L, 1, L->length);
}
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)
{
int pivot = L->m[low];
while (low < high)
{
while (low < high && L->m[high] >= pivot)
{
high--;
}
swap(L, high, low);
while (low < high && L->m[low] <= pivot)
{
low++;
}
swap(L, low, high);
}
return low;
}
typedef struct
{
int m[MAX_SIZE ];
int length;
} SqList;
//用排序,铁定跑不了这个swap函数,最好自己写一个
void swap(SqList *q, int i, int j);
void swap(SqList *q, int i, int j)
{
int temp = q->m[i];
q->m[i] = q->m[j];
q->m[j] = temp;
}
//快速排序标准版
//快速排序其实本质上就是前面第一章写的冒泡排序的升级版本
//而且我现在写的这个快速排序算法,被列为20世纪十大算法之一,绝对值得我们这些玩编程的人,去花费工夫去了解它
//时间复杂度最优情况下是O(n * log n)
//最差情况下是O(N*N);
void QuickSort(SqList *L){
QSort(L, 1, L->length);
}
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)
{
int pivot = L->m[low];
while (low < high)
{
while (low < high && L->m[high] >= pivot)
{
high--;
}
swap(L, high, low);
while (low < high && L->m[low] <= pivot)
{
low++;
}
swap(L, low, high);
}
return low;
}