This article mainly realizes the comparison classic bubble sorting algorithm (to already ordered or the basic order table complexity greatly reduces), and the dichotomy method searches, everybody crossing looks the code bar
//bubble sorting algorithm and binary search method#include "stdio.h"typedef struct{intKey;} Sstable_elem_type;typedef struct{Sstable_elem_type*elem;intLength;} Sstable_typedef;voidBubble_sort (sstable_typedef*st) {unsigned Chari =0, j =0;unsigned CharFinish_flag =0;//0: Complete sortingSstable_elem_type tmp; for(i =0; I < st->length; i++) {Finish_flag =1;//Assuming the sorting is complete for(j =0; J < St->length-i-1; J + +) {if(St->elem[j].key>st->elem[j +1].key) {Finish_flag =0;//Exchange dataTMP = st->elem[j];//Always take the maximumST->ELEM[J] = st->elem[j +1]; St->elem[j +1] = tmp; } }if(Finish_flag = =1)//A trip to bubble sort did not take place, indicating that the order has been sorted Break;printf("sorting\r\n"); }}//Initialization Order tablevoidInit_sstable (sstable_typedef* sstable, Sstable_elem_type*buf,intLen) {Sstable->elem = BUF; Sstable->length = Len;}//binary Find//buf already sortedintBisearch (sstable_typedef* sstable,intSearch_data) {intLow =0;inthigh=sstable->length-1;intMid while(Low <= High) {mid = (low + high)/2;if(Search_data = = Sstable->elem[mid].key)return(mid+1);Else if(Search_data < Sstable->elem[mid].key) High = mid-1;ElseLow = mid +1; }return 0;}#define LEN 5intMainvoid) {Sstable_typedef ST; Sstable_elem_type st_elem[Ten];inti =0; Init_sstable (&st, St_elem,Ten); for(i =0; I <Ten; i++) {St_elem[i].key =Ten-I.; }printf("unsorted:"); for(i =0; I <Ten; i++) {printf("%d", St.elem[i].key); } bubble_sort (&ST);printf("\r\nsorted:"); for(i =0; I <Ten; i++) {printf("%d", St.elem[i].key); }printf("%d", Bisearch (&st,- One) );printf("\ r \ n"); while(1);return 0;}
Results:
Bubble sorting algorithm of sequential table and binary search code implementation