资源简介
二分查找_测试
代码片段和文件信息
#if 1
#include
using namespace std;
#include
#define SIZE(a) sizeof(a) / sizeof(a[0])
#if 0
int search(int array[] int n int v)
{
int left right middle;
left = 0 right = n;
while (left <= right) {
middle = (left + right) / 2;
if (array[middle] > v) {
right = middle - 1;
}
else if (array[middle] < v) {
left = middle + 1;
}
else {
return middle;
}
}
return -1;
}
#endif
#if 1
int search(int array[] int n int v)
{
int left right middle;
left = 0 right = n;
cout << “left: “ << left << ‘\t‘ << ‘\t‘ << “right: “ << right << endl;
while (left < right) {
middle = (left + right) / 2;
cout << ‘\t‘ << “mid: “ << middle;
if (array[middle] > v) {
cout << ‘\t‘ << ‘\t‘ << “array[“ << middle << “]:“ << array[middle] << “ > “ << v << endl;
right = middle;
}
else if (array[middle] < v) {
cout << ‘\t‘ << ‘\t‘ << “array[“ << middle << “]:“ << array[middle] << “ < “ << v << endl;
left = middle + 1;
}
else {
return middle;
}
cout << “left: “ << left << ‘\t‘ << ‘\t‘ << “right: “ << right << endl;
}
return -1;
}
#endif
#if 0
int search(int array[] int n int v)
{
int left right middle;
left = 0 right = n - 1;
cout << “left: “ << left << ‘\t‘ << ‘\t‘ << “right: “ << right << endl;
while (left <= right) {
middle = (left + right) / 2;
cout << ‘\t‘ << “mid: “ << middle;
if (array[middle] > v) {
cout << ‘\t‘ << ‘\t‘ << “array[“ << middle << “]:“ << array[middle] << “ > “ << v << endl;
right = middle;
}
else if (array[middle] < v) {
cout << ‘\t‘ << ‘\t‘ << “array[“ << middle << “]:“ << array[middle] << “ < “ << v << endl;
left = middle;
}
else {
return middle;
}
cout << “left: “ << left << ‘\t‘ << ‘\t‘ << “right: “ << right << endl;
}
return -1;
}
#endif
int main()
{
int array[] = { 0 1 2 3 4 5 6 7 13 19 };
#if 1
cout << endl << search(array SIZE(array) -1) << endl;
cout << endl << search(array SIZE(array) 20) << endl;
#endif
#if 0
for (int i = 0; i < SIZE(array); ++i) {
cout << endl << search(array SIZE(array) array[i]) << endl << endl;
}
#endif
#if 0
for (int i = 0; i < SIZE(array); ++i) {
cout << array[i] << ‘\t‘;
cout << search(array SIZE(array) array[i]) << endl;
}
#endif
return 0;
}
#endif
/*
int binary_search1(int array[] int n int value) {
int left = 0 right = n mid;
cout << “left: “ << left << ‘\t‘ << ‘\t‘ << “right: “ << right << endl;
while (left < right) {
mid = (left + right) / 2;
cout << ‘\t‘ << “mid: “ << mid << endl;
if (array[mid] > value) {
right = mid - 1;
}
else if (array[mid] < value) {
left = mid;
}
else
return mid;
cout << “left: “ << left << ‘\t‘ << ‘\t‘ << “right: “ << right << endl;
}
return -1;
}
int main()
{
int a[8] = { 2 4 5 7 8 11 14 17 };
//cout << endl << binary_search1(a 8 17) << endl;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3018 2016-12-12 15:38 backup.cpp
目录 0 2016-12-12 15:38 Debug\
文件 56213 2016-12-12 15:38 Debug\backup.obj
文件 2543 2016-12-12 15:28 Debug\main.obj
文件 429056 2016-12-12 15:38 Debug\vc140.idb
文件 356352 2016-12-12 15:38 Debug\vc140.pdb
文件 238 2016-12-12 15:38 Debug\二分查找.log
目录 0 2016-12-12 15:38 Debug\二分查找.tlog\
文件 1230 2016-12-12 15:38 Debug\二分查找.tlog\CL.command.1.tlog
文件 31280 2016-12-12 15:38 Debug\二分查找.tlog\CL.read.1.tlog
文件 1442 2016-12-12 15:38 Debug\二分查找.tlog\CL.write.1.tlog
文件 2300 2016-12-12 15:38 Debug\二分查找.tlog\li
文件 3048 2016-12-12 15:38 Debug\二分查找.tlog\li
文件 496 2016-12-12 15:38 Debug\二分查找.tlog\li
文件 221 2016-12-12 15:38 Debug\二分查找.tlog\二分查找.lastbuildstate
文件 3063 2016-12-12 15:28 main.cpp
文件 7290 2016-12-10 12:57 二分查找.vcxproj
文件 1035 2016-12-10 12:57 二分查找.vcxproj.filters
评论
共有 条评论