Bubble sorting algorithm of sequential table and binary search code implementation

Source: Internet
Author: User

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-&GT;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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.