C 语言 排序算法,冒泡排序,选择排序,插入排序,二分查找
排序算法,冒泡排序,选择排序,插入排序
chunli@CentOS/tmp/work$catsort.c#include<stdio.h>#include<stdlib.h>#include<time.h>intbubble_sort(intarray[],intsize){if(NULL==array||size<2){return-1;}inti=0;intj=0;for(i=0;i<size;i++){for(j=0;j<size;j++){if(array[i]<array[j]){intt=array[i];array[i]=array[j];array[j]=t;}}}return0;}intselect_sort(intarray[],intsize){if(NULL==array||size<2){return-1;}inti=0;intj=0;intindex=0;for(i=0;i<size-1;i++){index=i;for(j=i+1;j<size;j++){if(array[j]<array[index]){index=j;}}inttmp=array[i];array[i]=array[index];array[index]=tmp;}return0;}intinsert_sort(intarray[],intsize){if(NULL==array||size<2){return-1;}inti=0;intj=0;intt=0;for(i=1;i<size;i++){if(array[i-1]>array[i]){t=array[i];j=i;while(j>0&&array[j-1]>t){array[j]=array[j-1];j--;}array[j]=t;}}return0;}intmain(){srand((int)time(NULL));intarray[30];for(inti=0;i<sizeof(array)/sizeof(int);i++)array[i]=rand()%100;for(inti=0;i<sizeof(array)/sizeof(int);i++)printf("%02d",array[i]);printf("\n");bubble_sort(array,sizeof(array)/sizeof(int));//select_sort(array,sizeof(array)/sizeof(int));//insert_sort(array,sizeof(array)/sizeof(int));for(inti=0;i<sizeof(array)/sizeof(int);i++)printf("%02d",array[i]);printf("\n");return0;}chunli@CentOS/tmp/work$gccsort.c-Wall-std=c99&&./a.out350533148151221809175783100304829284722848017406819444625608010304050608091014171822283335444851565762727481818283849294chunli@CentOS/tmp/work$
二分查找:
chunli@CentOS/tmp/work$catsort.c#include<stdio.h>#include<stdlib.h>#include<time.h>intbubble_sort(intarray[],intsize){if(NULL==array||size<2){return-1;}inti=0;intj=0;for(i=0;i<size;i++){for(j=0;j<size;j++){if(array[i]<array[j]){intt=array[i];array[i]=array[j];array[j]=t;}}}return0;}intbinaray(intarray[],intsize,intkey){intlow=0;inthigh=size;intmid=0;while(low<=high){mid=(high+low)/2;if(key==array[mid]){returnmid;}elseif(key>array[mid]){low=mid+1;}else{high=mid-1;}}return-1;}intmain(){srand((int)time(NULL));intarray[30];intret=0;for(inti=0;i<sizeof(array)/sizeof(int);i++)array[i]=rand()%100;for(inti=0;i<sizeof(array)/sizeof(int);i++)printf("%02d",array[i]);printf("\n");bubble_sort(array,sizeof(array)/sizeof(int));for(inti=0;i<sizeof(array)/sizeof(int);i++)printf("%02d",array[i]);printf("\n");ret=binaray(array,sizeof(array)/sizeof(int),16);printf("查找值%d\n",ret);return0;}chunli@CentOS/tmp/work$gccsort.c-Wall-std=c99&&./a.out399853330115536669833237054034656764776230387011547358856779010511153032333437383940535354586264656667676970737779838598查找值-1chunli@CentOS/tmp/work$
二分查找,递归实现
chunli@CentOS/tmp/work$catsort.c#include<stdio.h>#include<stdlib.h>#include<time.h>intbubble_sort(intarray[],intsize){if(NULL==array||size<2){return-1;}inti=0;intj=0;for(i=0;i<size;i++){for(j=0;j<size;j++){if(array[i]<array[j]){intt=array[i];array[i]=array[j];array[j]=t;}}}return0;}intbinaray(intarray[],intIndexLow,intIndexHigh,intkey){if(NULL==array){return-1;}if(IndexLow<=IndexHigh){intIndexMid=(IndexLow+IndexHigh)/2;if(array[IndexMid]==key){returnIndexMid;}elseif(key>array[IndexMid]){returnbinaray(array,IndexMid+1,IndexHigh,key);}else{returnbinaray(array,IndexLow,IndexMid-1,key);}}return-1;}intmain(){srand((int)time(NULL));intarray[31];intret=0;for(inti=0;i<sizeof(array)/sizeof(int);i++)array[i]=rand()%100;for(inti=0;i<sizeof(array)/sizeof(int);i++)printf("%02d",array[i]);printf("\n");bubble_sort(array,sizeof(array)/sizeof(int));for(inti=0;i<sizeof(array)/sizeof(int);i++)printf("%02d",array[i]);printf("\n");printf("-------\n");ret=binaray(array,0,sizeof(array)/sizeof(int)-1,6);printf("index=%d\n",ret);ret=binaray(array,0,sizeof(array)/sizeof(int)-1,99);printf("index=%d\n",ret);return0;}chunli@CentOS/tmp/work$gcc-gsort.c-Wall-std=c99&&./a.out9009130215376745182899686153709394412114646731257812330352846402030912131415182125283133374145525361646467676870788490939499-------index=-1index=30chunli@CentOS/tmp/work$
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。