关于快速排序参考之前的文章:https://blog.csdn.net/hpu2022/article/details/83069041
关于二分查找的参考之前的文章:https://blog.csdn.net/hpu2022/article/details/79845630
code:
#include <iostream>
using namespace std;
int BinarySearch(int *a , int low, int high, int key)
{
if(low > high)
return -1;
int mid = (low + high) / 2;
if(key == a[mid])
return mid;
else if(key < a[mid])
return BinarySearch(a, low, mid-1, key);
else
return BinarySearch(a, mid+1, high, key);
}
void QuickSort(int *a, int L, int R)
{
if(L < R)
{
int i = L;
int j = R;
int num = a[L];
while(i < j)
{
while(i < j && a[j] >= num)
--j;
if(i < j)
a[i++] = a[j];
while(i < j && a[i] < num)
++i;
if(i < j)
a[j--] = a[i];
}
a[i] = num;
QuickSort(a, L, i-1);
QuickSort(a, i+1, R);
}
}
int main()
{
int a[100];
for(int i=0; i<6; i++)
cin >> a[i];
QuickSort(a, 0, 5);
int number;
cin >> number;
int index = BinarySearch(a, 0, 5, number);
if(index != -1)
cout << a[index] << endl;
else
cout << "-1" << endl;
return 0;
}