C語言 排序演算法 - 資料結構學習筆記

來源:互聯網
上載者:User

標籤:c語言   資料結構   排序   快速   冒泡   插入   

/**  功能:     排序  *日期:     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)&&LT(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);          // 對高子表遞迴排序  }} // QSortStackNode 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);        //調用遞迴快速排序} // QuickSortint 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;}

本文出自 “android_home” 部落格,請務必保留此出處http://yangzheng0809.blog.51cto.com/6024606/1968204

C語言 排序演算法 - 資料結構學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.