#include <iostream>
using namespace std;
template <class Type>
int BinarySearch(Type a[],const Type&x,int n);
int main()
{
?????? int x = 6;
?????? int a[10];
?????? for(int i=0; i<10; i++)
?????? {
????????????? a[i] = i + 1;
?????? }
?????? cout<<BinarySearch(a,x,10)<<endl;
?????? return 0;
}
?????? template<class Type>
?????? int BinarySearch(Type a[],const Type&x,int n)
{
?????? int left = 0; int right = n-1;
?????? while(left <= right)
?????? {
?????? int mid = (left + right)/2;
?????? if(x == a[mid])
????????????? {
???????????????????? return mid;
????????????? }
????????????? if(x > a[mid])
????????????? {
???????????????????? left = mid + 1;
????????????? }
???????????????????? else
????????????? {
???????????????????? right = mid-1;
????????????? }
?????? }
?????? return -1;
}
#include <iostream>
using namespace std;??
int a[] = {10,5,9,4,3,7,8};?
int b[7];??
template<class Type>
void Merge(Type c[],Type d[],int l,int m,int r);?
template<class Type>
void MergeSort(Type a[],int left,int right);
int main()
{?
for(int i=0; i<7; i++)
{??
cout<<a[i]<<"" ;
}??
cout<<endl;??
MergeSort(a,0,6);??
for(int i=0; i<7; i++)
{??
cout<<a[i]<<"" ;
}??
cout<<endl;
}?
template<class Type>
void Merge(Type c[],Type d[],int l,int m,int r)
?{??
int i = l,j = m + 1,k = l;? while((i<=m)&&(j<=r))??
{??
if(c[i]<=c[j])?
? {??
d[k++] = c[i++];
? }?
else
? {??
d[k++] = c[j++];
?????? }
}
if(i>m)?
{??
for(int q=j; q<=r; q++)?
? {??
d[k++] = c[q];
}
}?
else
{?
for(int q=i; q<=m; q++)?
? {??
d[k++] = c[q];
?????? }
?????? }
}
template<class Type>
void MergeSort(Type a[],int left,int right)?
{??
if(left<right)?
{??
int i = (left + right)/2;??
MergeSort(a,left,i);??
MergeSort(a,i+1,right);??
Merge(a,b,left,i,right);?
for(int g=left; g<=right; g++)
? {??
a[g] = b[g];
?????? }
?????? }
}