Answers
#include <iostream>
#include <stdio.h>
using namespace std;
void insertion_sort(int,int);
void insert(int,int);
int search(int a,int,int,int);
void insertion_sort(int a[],int n)
{
if(n>1)
{
insertion_sort(a,n-1);
}
insert(a[],n);
}
void insert(int a[],int n)
{
int key=a[n];
int i=n-1;
for (i;i>=0;--i)
{
if (key<=a[i])
{
a[i+1]=a[i];
}
else
break;
}
a[i+1]=key;
}
int search(int a[], int key, int low, int high)
{
int mid;
if (low>high)
return -1;
mid=(high+low) /2 ;
if (a[mid]==key)
return mid;
if(key<a[mid])
return search(a, key, low, mid-1);
else
return search(a, key, mid+1,high);
}
int main()
{
int key,a[10];
for(int i=0;i<10;i++)
{
cout<<"ÊäÈëÊý×éÔªËØ£º"<<endl;
cin>>a[i];
}
insertion_sort(int a[10],int n);
cout<<"ÊäÈë²éÕÒµÄÔªËØ£º"<<endl;
cin>>key;
cout<<search(a[10],key, 0, 9);
return 0;
}
山田大魔王
answered 12 years ago