一.折半查找的原理:
折半查找的前提是:数据是有序的;大概原理就是:对区间二分,找到数据所在的区间,然后对所在区间继续二分,不断对数据所在的区间二分,直至找到数据或者只剩一个数为止;
二.程序部分:
#include<iostream>
using namespace std;
int Search_Two(int a[],int x,int low, int high);
void Bubble_sort(int a[], int n);
void swap(int &a, int&b);
int main()
{
int a[10] = { 1,2,3,4,5,6,7,8,9,10};
int index = Search_Two(a,6,0,9);
cout << a[index] << endl;
system("pause");
}
void swap(int &a, int&b)
{
int temp;
temp = a;
a = b;
b = temp;
}
void Bubble_sort(int a[], int n)
{
for(int i =0;i<n-1;i++)
for (int j = 0; j < n - 1-i; j++)
{
if (a[j] > a[j + 1])
swap(a[j], a[j + 1]);
}
}
int Search_Two(int a[],int x, int low,int high)
{
int mid;
int index;
while (low <= high)
{
m