/** 功能: 排序
*日期: 2017年9月24日
*作者: yzh
*开发环境: QT
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STACK_LENGTH 100 //非递归快速排序调用栈
#define MAX_LENGTH 20 //数组最大值
//对于数值型 keyType
#define EQ(a,b) ((a) == (b)) //比较 a == b ?
#define LT(a,b) ((a) < (b)) //比较 a < b ?
#define LQ(a,b) ((a) <= (b)) //比较 a <= b ?
typedef int KeyType; //查找关键字类型
typedef char InfoType; //其他信息
typedef struct { // 信息体
KeyType key;
InfoType otherInfo;
}RedType;
typedef struct { //数组结构
RedType r[MAX_LENGTH+1]; //r[0]闲置或用作哨兵;
int length;
}SqList;
typedef struct { //栈节点
int l,h;
}StackNode;
typedef struct { //栈
StackNode node[MAX_STACK_LENGTH];
int idx;
}Stack;
//插入模拟数据
int insertSQ(SqList *list){
for(int i = 1;i<MAX_LENGTH+1;i++){
list->r[i].key = rand()%1000;
list->r[i].otherInfo = 96+i;
}
list->length = MAX_LENGTH+1;
}
//打印数组
int printf_SQ(SqList *list){
for(int i = 1 ; i<list->length ; i++){
if(i<list->length-1){
printf("%d,",list->r[i]);
}else{
printf("%d\n",list->r[i]);
}
}
}
//插入排序
int sort_I(SqList *list){
int i,j;
for(i = 2;i<list->length;++i){
if(LT(list->r[i].key,list->r[i-1].key)){
list->r[0] = list->r[i];
list->r[i] = list->r[i-1];
for(j = i-2;LT(list->r[0].key,list->r[j].key); --j){
list->r[j+1] = list->r[j];
}
list->r[j+1] = list->r[0];
}
}
}
//插入排序改进:折半插入排序
int sort_B_I(SqList *l){
int i , j;
int low , high,mid;
for(i = 2; i<l->length;i++){
if(LT(l->r[i].key,l->r[i-1].key)){
l->r[0] = l->r[i];
l->r[i] = l->r[i-1];
low = 1;high = i-2;
while(low<=high){
mid = (low+high)/2;
if(LT(l->r[0].key,l->r[mid].key)){
high = mid -1;
}else{
low = mid+1;
}
}
for(j = i-2; j>high;j--){
l->r[j+1] = l->r[j];
}
l->r[high+1] = l->r[0];
}
}
}
//冒泡排序
int sort_B(SqList *list){
int i,j;
for(i = 1;i<list->length;i++){
for(j = i+1;j<list->length ; j++){
if(LT(list->r[j].key,list->r[i].key)){
list->r[0] = list->r[j];
list->r[j] = list->r[i];
list->r[i] = list->r[0];
}
}
}
}
//选择排序
int sort_S(SqList *list){
int i,j;
for(i = 1 ; i<list->length ;i++){
list->r[0] = list->r[i];
int min = i;
for(j = i+1; j<list->length ; j++){
if(LT(list->r[j].key,list->r[0].key)){
list->r[0] = list->r[j];
min = j;
}
}
list->r[min] = list->r[i];
list->r[i] = list->r[0];
}
}
//快速排序
int Partition(SqList *L,int l ,int h){
L->r[0] = L->r[l];
while(LT(l,h)){
while(LT(l,h)&<(L->r[0].key,L->r[h].key)){--h;}
L->r[l] = L->r[h];
while(LT(l,h)&&LQ(L->r[l].key,L->r[0].key)){++l;}
L->r[h] = L->r[l];
}
L->r[l] = L->r[0];
return l;
}
//递归算法
void QSort(SqList *L, int low, int high) { //算法10.7
// 对顺序表L中的子序列L.r[low..high]进行快速排序
int pivotloc;
if (low < high) { // 长度大于1
pivotloc = Partition(L, low, high); // 将L.r[low..high]一分为二
QSort(L, low, pivotloc-1); // 对低子表递归排序,pivotloc是枢轴位置
QSort(L, pivotloc+1, high); // 对高子表递归排序
}
} // QSort
StackNode pop(Stack *stack){
StackNode node = stack->node[stack->idx];
stack->idx--;
return node;
}
void push(Stack *stack , int l,int h){
stack->idx++;
stack->node[stack->idx].h = h;
stack->node[stack->idx].l = l;
}
//非递归算法
void sort_Q(SqList *L,int low,int high){
int pivotloc;
Stack stack;
push(&stack,low,high);
StackNode node;
while(stack.idx>0){
node = pop(&stack);
if(node.l <node.h){
pivotloc = Partition(L, node.l, node.h);
push(&stack,pivotloc+1,node.h);
push(&stack,node.l,pivotloc-1);
}
}
}
void QuickSort(SqList *L) { // 算法10.8
// 对顺序表L进行快速排序
//sort_Q(L, 1, L->length-1); //调用非递归快速排序
//QSort(L, 1, L->length-1); //调用递归快速排序
} // QuickSort
int main(int argc, char *argv[])
{
// 测试栈
// Stack stack;
// push(&stack , 1,2);
// push(&stack , 2,3);
// push(&stack , 4,6);
// StackNode node= pop(&stack);
// printf("%d,%d\n",node.l,node.h);
// StackNode node2= pop(&stack);
// printf("%d,%d\n",node2.l,node2.h);
// StackNode node3= pop(&stack);
// printf("%d,%d\n",node3.l,node3.h);
// return 0;
SqList list;
insertSQ(&list); //插入模拟数据
printf_SQ(&list); //打印测试数据
//(41,467,334,500,169,724,478,358,962,464,705,145,281,827,961,491,995,942,827,436)
printf("-------------------------------------------\n");
// sort_B(&list); //冒泡排序
// sort_I(&list); //插入排序
// sort_S(&list); //选择排序
// sort_B_I(&list); //插入排序改进:折中插入排序
QuickSort(&list); //快速排序
printf_SQ(&list); //打印排序后数组
//(41,145,169,281,334,358,436,464,467,478,491,500,705,724,827,827,942,961,962,995)
printf("-------------------------------------------\n");
return 0;
}
转载于:https://blog.51cto.com/yangzheng0809/1968204