冒泡排序+二分法查找

 1 #include <stdio.h>

 2 

 3 void bubble(int *arr, int length)

 4 {

 5     int i, j, tmp;

 6     for(i=0;i<length-1;i++)

 7     {

 8         for(j=0;j<length-1-i;j++)

 9         {

10             if(arr[j]>arr[j+1])

11             {

12                 tmp = arr[j];

13                 arr[j] = arr[j+1];

14                 arr[j+1] = tmp;

15             }

16         }

17     }

18 

19 }

20 

21 int search(int *arr, int key, int high, int low)

22 {

23     int middle = 0;

24     if(high<low)

25         return -1; 

26     middle = (low+high)/2;

27     printf("hello\n");

28     if(arr[middle] == key)

29     {   

30         return middle;

31     }   

32     else if(arr[middle]<key)

33         return search(arr, key, high, middle+1);

34     }

35     else

36     {

37         return search(arr, key, middle-1, low);

38     }

39 }

40 

41 int main()

42 {

43     int i = 0;

44     int a[] = {1, 32, 5, 56, 39, 4, 67, 83, 25, 98};

45     

46     for(i=0;i<10;i++)

47     {   

48         printf("%d\t", a[i]);

49     }

50     printf("\n");

51     bubble(a, 10);

52     i = search(a, 32, 9, 0);

53     printf("i=%d\n", i);

54     for(i=0;i<10;i++)

55     {   

56         printf("%d\t", a[i]);

57     }

58     printf("\n");

59     return 0;

60 }

 

你可能感兴趣的:(冒泡排序)