binary:
1 #include <stdio.h>
2 int Bsearch(int arr[],int low,int high,int key)
3 {
4 int mid;
5 while(low<=high)
6 {
7 mid=(low+high)/2;
8 if(key>arr[mid])
9 {
10 low=mid+1;
11 }
12 else if(key<arr[mid])
13 {
14 high=mid-1;
15 }
16 else if(key==arr[mid])
17 {
18 return mid;
19 }
20
21 }
22 return -1;
23 }
24
25 int main(int argc, const char *argv[])
26 {
27 int arr[10]={1,2,3,4,5,6,7,8,9,11};
28 int num=8;
29 int low=0,high=9;
30 int target=Bsearch(arr,low,high,num);
31 printf("%d\n",target);
32 return 0;
33 }
bubble:
1 #include <stdio.h>
2 int main(int argc, const char *argv[])
3 {
4 int arr[5]={2,1,3,5,4};
5 for(int i=0;i<5;i++){
6 if(arr[i]>arr[i+1])
7 {
8 int temp=arr[i+1];
9 arr[i+1]=arr[i];
10 arr[i]=temp;
11 }
12 printf("at this moment the arry is->%d\n",arr[i]);
13 }
14
15 return 0;
16 }
~
select:
1 #include <stdio.h>
2
3
4 int main(int argc, const char *argv[]){
5 int arr[6]={2,4,1,89,43,12};
6 int m=0;
7 for(int i=0;i<5;i++)
8 {
9 m=i;
10 for(int j=i+1;j<6;j++)
11 {
12 if(arr[m]<arr[j])
13 {
14 m=j;
15 }
16 }
17 if(m!=i)
18 {
19 int temp=arr[i];
20 arr[i]=arr[m];
21 arr[m]=temp;
22 }
23 }
24 for(int i=0;i<6;i++)
25 {
26
27 printf("%d\n",arr[i]);
28 }
29 return 0;
30 }