/** function: Sort * Date: 2017 September 24 * yzh * development environment: qt **/#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STACK_LENGTH 100 //non-recursive fast sort call stack #define max_length 20 //array Maximum//For numeric keytype#define eq (A, B) ((a) == (b)) //comparison a == b ? #define &NBSP;LT (A, a) ((a) < (b)) //comparison a < b ? #define &NBSP;LQ (A, A, a) ((a) <= (b)) //comparison a <= b ?typedef int keytype; // Find keyword Type Typedef char infotype; //Other Information typedef struct { // Information Body keytype key; infotype otherinfo;} redtype;typedef struct { //Array Structure redtype r[max_length+1]; //r [0] idle or used as a Sentinel; int length;} sqlist;typedef struct { //stack node int l,h;} stacknode;typedef struct { //Stack StackNode node[MAX_STACK_LENGTH]; int idx;} stack;//inserting analog data 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;} Print array 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]); } }}//Insert Sort int sort_i (sqlist *list) { int i,j;&Nbsp; 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]; } }}//Insert Sort improvements: Binary Insert sort 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]; } }}//Bubble sort 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]; } } }}//Select Sort 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]; }}//Quick Sort 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;} Recursive algorithm Void qsort (sqlist *l, int low, int high) { //algorithm 10.7 // L.r[low The subsequence in the sequential table L: High] for quick sort int pivotloc; if (low < high) { // Length greater than 1 pivotloc = partition (L, low, high); // will l.r[ Low.. High] split qsort (l, low, pivotloc-1); // recursive ordering of low sub-tables, Pivotloc is the pivot position qsort (l, pivotloc+1, high); // 高子表 recursive ordering }} // 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&nBsp;= l;} Non-recursive algorithm 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) { Algorithm 10.8&NBsp; // Order table L Quick Sort //sort_q (l, 1, l->length-1); //Call non-recursive quick sort //qsort (l, 1, l->length-1); //call Recursive Quick sort} // quicksortint main (int argc, char *argv[]) {// Test Stack// 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; &NBSP;&NBSP;&NBSP;SQLIST&NBSP;LIST;&NBSP;&NBSP;&NBSP;&NBSP;INSERTSQ (&list); // Insert analog Data printf_sq (&list), //Print test data //(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); //Bubble sort// sort_i (&list); //Insert Sort// sort_s (&list); //Select Sort// sort_b_i (&list); //Insert Sort improvements: Eclectic insertion sort quicksort (&list); //Quick Sort &NBSP;PRINTF_SQ (&list); //print sorted array //( 41,145,169,281,334,358,436,464,467,478,491,500,705,724,827,827,942,961,962,995) printf ( "-------------------------------------------\ n"); return 0;}
This article is from the "Android_home" blog, make sure to keep this source http://yangzheng0809.blog.51cto.com/6024606/1968204
C Language Sorting algorithm-Data structure learning notes