#include <stdio.h>
#include<stdlib.h>
int BinarySearch( int a[], int X )
{
int low=1,high=10,mid;
while(low<=high){
mid=(high+low)/2;
if(a[mid]>X) high-=1;
else if(a[mid]<X) low+=1;
else return mid;
}
return 0;
}
void printarr(int a[],int n){
for(int i=0;i<n;i++){
printf("%d ",a[i]);
}
printf("\n");//养成好习惯
}
void mergesort(int arr[], int len) {
int* a = arr;
int* b = (int*) malloc(len * sizeof(int));
int seg, start;
for (seg = 1; seg < len; seg += seg) {
for (start = 0; start < len; start += seg + seg) {
int low = start, mid = min(start + seg, len), high = min(start + seg + seg, len);
int k = low;
int start1 = low, end1 = mid;
int start2 = mid, end2 = high;
while (start1 < end1 && start2 < end2)
b[k++] = a[start1] < a[start2] ? a[start1++] : a[start2++];
while (start1 < end1)
b[k++] = a[start1++];
while (start2 < end2)
b[k++] = a[start2++];
}
int* temp = a;
a = b;
b = temp;
}
if (a != arr) {
int i;
for (i = 0; i < len; i++)
b[i] = a[i];
b = a;
}
free(b);
}
int main()
{
int arr[] = { 0, 22, 2, 33, 3, 1, 213, 5, 8, 9 };
int sizeofnum=sizeof(arr)/sizeof(int);
mergesort(arr,sizeofnum);
printarr(arr,sizeofnum);
printf("%d",BinarySearch(arr,33));
}
01-20
942
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-04
518
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)