//顺序查找
#include<stdio.h>
#include<iostream>
using namespace std;
#define max 100
int a[max];
int n;
int key;
int sq_search()
{
for (int i = 1; i <= n; i++)
{
if (a[i] == key)
return i;
}
return 0;
}
int main()
{
cin >> n >> key;
for (int i = 1; i <= n; i++)
cin >> a[i];
int temp=sq_search();
if (temp == 0)
cout << "查找不成功";
else
cout << "查找成功";
}
//折半查找,查找前进行归并排序
#include<stdio.h>
#include<iostream>
using namespace std;
#define max 100
int a[max];
int b[max];
int n;
int key;
void merge_sort(int low, int mid, int high)
{
int i = low, j = mid+1, k = low;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
b[k++] = a[i++];
else
b[k++] = a[j++];
}
while (i <= mid)
b[k++] = a[i++];
while (j <= high)
b[k++] = a[j++];
for (int i = low; i <= high; i++)
a[i] = b[i];
}
void merge(int low, int high)
{
if (low < high)
{
int mid = (low + high) / 2;
merge(low, mid);
merge(mid + 1, high);
merge_sort(low, mid, high);
}
}
int binary_search(int low, int high)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (key > a[mid])
low = mid + 1;
else if (key < a[mid])
high = mid - 1;
else
return mid;
}
return 0;
}
int main()
{
cin >> n >> key;
for (int i = 1; i <= n; i++)
cin >> a[i];
merge(1,n);
int temp=binary_search(1, n);
if (temp)
cout << "查找成功";
else
cout << "查找失败";
}