待 简单C++代码
在线等待 简单C++代码
小弟初学算法设计,想用二分搜索技术编一个简单程序
给定已排好序的N个元素a[0:n-1],现要在这N个元素中找一个特定的元素x
大虾们帮忙了!!
------解决方案--------------------
假设为升序排列,则二分搜索代码如下:
int i = 0;
int j = n - 1;
while (i < j)
{
if (a[i] == x || a[j] == x)
{
return true;
}
int k = (i + j) / 2;
if (a[k] == x)
{
return true;
}
else if (a[k] < x)
{
j = k;
}
else
{
i = k;
}
}
return false;
------解决方案--------------------
有个地方错了。
把else if (a[k] < x)改成else if (a[k] > x)
------解决方案--------------------
void Half_Search(const int *a, int SIZE, int value, int left, int right)
{
if (left > right || value < a[left] || value > a[right] )
{
cout < < "No Such Data Found! " < < endl < < endl ;
return ;
}
int middle = (left + right)/2 ; // middle pointer
if ( value == a[middle] )
{
while (a[--middle] == value) ; // move the pointer to the front position of the leftest certain data
cout < < "_________________Data Found___________________ " < < endl ;
while (a[++middle] == value && middle < SIZE) // print all the data(the count can be one, two, three...)
cout < < "a[ " < < middle < < "] : " < < a[middle] < < endl ;
cout < < "_________________Data Found___________________ " < < endl ;
}
else if (value < a[middle] )
Half_Search(a, SIZE, value, left, middle-1) ;
else
Half_Search(a, SIZE, value, middle+1, right) ;
}
------解决方案--------------------
main就自己写咯
------解决方案--------------------
#include "memory "
#include "vector "
#include "iostream "
using namespace std;
/*
*/
template <typename T> int search(vector <T> & vArray, T t) {
int _r = static_cast <int> (vArray.size()) - 1;
int _l = 0;
while (_l <= _r) {
int _m = (_r + _l) / 2;
if (vArray[_m] == t)
return _m;
if (vArray[_m] > t)
_r = _m - 1;
else
_l = _m + 1;
}
return -1;
}
int main()
{
vector <int> vArray;
for (int i = 0; i < 100; i++)
vArray.push_back(i);
cout < <search(vArray, 80) < <endl;
cout < <search(vArray, 180) < <endl;
cout < <search(vArray, 25) < <endl;
return 0;
}
小弟初学算法设计,想用二分搜索技术编一个简单程序
给定已排好序的N个元素a[0:n-1],现要在这N个元素中找一个特定的元素x
大虾们帮忙了!!
------解决方案--------------------
假设为升序排列,则二分搜索代码如下:
int i = 0;
int j = n - 1;
while (i < j)
{
if (a[i] == x || a[j] == x)
{
return true;
}
int k = (i + j) / 2;
if (a[k] == x)
{
return true;
}
else if (a[k] < x)
{
j = k;
}
else
{
i = k;
}
}
return false;
------解决方案--------------------
有个地方错了。
把else if (a[k] < x)改成else if (a[k] > x)
------解决方案--------------------
void Half_Search(const int *a, int SIZE, int value, int left, int right)
{
if (left > right || value < a[left] || value > a[right] )
{
cout < < "No Such Data Found! " < < endl < < endl ;
return ;
}
int middle = (left + right)/2 ; // middle pointer
if ( value == a[middle] )
{
while (a[--middle] == value) ; // move the pointer to the front position of the leftest certain data
cout < < "_________________Data Found___________________ " < < endl ;
while (a[++middle] == value && middle < SIZE) // print all the data(the count can be one, two, three...)
cout < < "a[ " < < middle < < "] : " < < a[middle] < < endl ;
cout < < "_________________Data Found___________________ " < < endl ;
}
else if (value < a[middle] )
Half_Search(a, SIZE, value, left, middle-1) ;
else
Half_Search(a, SIZE, value, middle+1, right) ;
}
------解决方案--------------------
main就自己写咯
------解决方案--------------------
#include "memory "
#include "vector "
#include "iostream "
using namespace std;
/*
*/
template <typename T> int search(vector <T> & vArray, T t) {
int _r = static_cast <int> (vArray.size()) - 1;
int _l = 0;
while (_l <= _r) {
int _m = (_r + _l) / 2;
if (vArray[_m] == t)
return _m;
if (vArray[_m] > t)
_r = _m - 1;
else
_l = _m + 1;
}
return -1;
}
int main()
{
vector <int> vArray;
for (int i = 0; i < 100; i++)
vArray.push_back(i);
cout < <search(vArray, 80) < <endl;
cout < <search(vArray, 180) < <endl;
cout < <search(vArray, 25) < <endl;
return 0;
}