fast search algoritm

I wrote a c++ function that uses sequential search to search an array of floats for all all the elements > the key. The function works fine but the problem is it is ver very slow when an array of say 10000 elements is searched since the sequential search algorithm is O(n) .Could any one suggest a fast search algorithm to search a very large array?

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <ctype.h>
#include <cmath>
#include <time.h>
#include <complex>
#include <fstream>
#include <string>
using namespace std;

int find_pos_b(float array[],int size,float key,int *pointer)
int main()
{

float b = 5;//key
float *X;

ifstream file;
file.open("X.txt");
if(file.fail())
{
cout<<"\nError in file opening";
exit(1);
}
int sizex
file>>sizex;
X = new float[++sizex];
for(int i = 0;i<sizex;i++)
{
file>> X[i];
}
X[sizex -1] = 0.0;

file.close();

int matches = 0;
int *pos_b = NULL;

matches = find_pos_b(X,sizex,b,pos_b);//find all the elements >b
pos_b = new int[matches];
if(pos_b == NULL)
{
cout<<"error in memeory allocation. Program exiting.."<<endl;
exit(1);
}
//print result of search
for(int g = 0;g<matches;g++)
{
pos_b[g] = pos_b[g] + 1;
cout<<"pos_b["<<g<<"]="<<pos_b[g]<<endl;
}

delete [] X;
delete [] pos_b;
return 0;
}
//-------------------
//array[]:the array to be searched
//size: the size of the array to be searched
//element:the function returns the indexes of all the elements >= key
//pointer:points to the array holding the result of the search
//the function returns the number of matches found
//--------------------
int find_pos_b(float array[],int size,float key,int *pointer)
{

int i = 0;
int j = 0;
int *p = NULL;//to point to the old array
int *t = NULL;//;= new float[j];
//start the search
while(i<size)
{

if(array[i] >= key)//found
{

p = t;
j++;

t = new int[j];
if(j ==1)
{

t[0] = i;

}
else
{
for(int l = 0;l<j-1;l++)
{
t[l] = p[l];
}
t[j-1] = i;
}

delete [] p;


}
i++ ;
}
pointer = t;

// for(int f = 0;f<j;f++)
// {
// cout<<pointer[f]<<endl;
// }

delete [] t;
t = NULL;
return j;//return the number of matches found
}



thanks
[3152 byte] By [dala] at [2007-11-18 22:16:07]
# 1 Re: fast search algoritm
I wrote a c++ function that uses sequential search to search an array of floats for all all the elements > the key. The function works fine but the problem is it is ver very slow when an array of say 10000 elements is searched since the sequential search algorithm is O(n) .Could any one suggest a fast search algorithm to search a very large array?
1) Did you profile your code? Be sure that you know what is slowing it down (see #2 below). Profiling programs will show you the function call or line that is taking the most time.

2) Just by looking at it, one reason why it's so slow is that you are calling new[] and delete[] in the loop. Take all the extra junk out of the loop such as operator new[] and delete[] and then really time how long it takes. It is a biased test if while you're in the loop, you're doing all kinds of things that have nothing to do with searching.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 0:34:52 >
# 2 Re: fast search algoritm
The following version uses the find_if algorithm and vector to store the results.

#include <algorithm>
#include <vector>
#include <functional>
#include <iostream>

struct IsGreater
{
IsGreater(float nKey) : key(nKey) {}
bool operator()(float keyTest)
{ return keyTest > key; }

float key;
};

int find_pos_b(float array[],int size,float key, std::vector<int>& results)
{
results.reserve(100);
bool moretodo = true;
float *pStart = array;
float *pVal;
while ( moretodo )
{
pVal = std::find_if(pStart, array + size, IsGreater(key));
if ( pVal != array + size )
{
results.push_back(pVal - array);
pStart = pVal + 1;
if ( pStart >= array+size)
moretodo = false;
else
if ( results.size() == 100 )
results.reserve(results.size() * 2 );
}
else
moretodo = false;
}
return results.size();
}

using namespace std;

int main()
{
float vals[10000];
std::vector<int> data;
int i;
// set up some values
for ( i = 0; i < 10000; ++i )
vals[i] = rand();

// now search for all items > 30000
find_pos_b(vals, 10000, 30000.0, data);

// output the items found and their data
cout << "Number of items > 30000 is " << data.size() << "\n";
for ( i = 0; i < data.size(); ++i )
cout << vals[data[i]] << " " << "Position: " << data[i] << "\n";
}

Note that there are no loops in the find function except for one that continuosly calls find_if. Handwritten loops will almost always be slower than the equivalent algorithm function that does the same job.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 0:35:54 >
# 3 Re: fast search algoritm
hi ,Paul.canu give some useful advice on this far-sort algorithm i know that it isnot efficient ,but anyway check it pls.

//this is the seach function.
#include<iostream>
void sort(int a[],int n)
{
int pt,temp;
for (int i=0;i<n;i++)
pt=i;
for(int j=i+1;j<n;j++)
if(a[pt]<a[j]) pt=i;
if(pt!=i)
{
temp=a[pt];
a[pt]=a[j];
a[j]=temp;

}

//this is the main function.

int main()

{
int a[10];
for(int i=0;i<10;i++)
std::cin>>a[i];
std::cout<<"these are the original data"<<std::endl;
for(int j=0;j<10;j++)
std::cout<<"\t"<<a[i]<<std::endl;
std::cout<<"these are the sorted data"<<std::endl;
sort(a[],10);
for(int k=0;k<10;k++)
std::cout<<"\t"<<a[i]<<std::endl;

}

I know that if it is finished by a stl vector.and the compexity is O(n^2).that is more convient ,but unluckily i have not get a lot about it,so i want u to comment on this CRUDE code.i will thank u for that!
jolley at 2007-11-9 0:36:55 >
# 4 Re: fast search algoritm
hi ,Paul.canu give some useful advice on this far-sort algorithm i know that it isnot efficient ,but anyway check it pls.

//this is the sort function.
#include<iostream>
void sort(int a[],int n)
{
int pt,temp;
for (int i=0;i<n;i++)
pt=i;
for(int j=i+1;j<n;j++)
if(a[pt]<a[j]) pt=i;
if(pt!=i)
{
temp=a[pt];
a[pt]=a[j];
a[j]=temp;

}

//this is the main function.

int main()

{
int a[10];
for(int i=0;i<10;i++)
std::cin>>a[i];
std::cout<<"these are the original data"<<std::endl;
for(int j=0;j<10;j++)
std::cout<<"\t"<<a[i]<<std::endl;
std::cout<<"these are the sorted data"<<std::endl;
sort(a[],10);
for(int k=0;k<10;k++)
std::cout<<"\t"<<a[i]<<std::endl;

}

I know that if it is finished by a stl vector.that is more convient ,but unluckily i have not get a lot about it,so i want u to comment on this CRUDE code.i will thank u for that!(and the compexity is O(n^2).)
jolley at 2007-11-9 0:37:53 >
# 5 Re: fast search algoritm
thanks alot Paul.
dala at 2007-11-9 0:38:53 >
# 6 Re: fast search algoritm
If you have a big array and it's already sorted, then you can use the binary search algorithm which is O(log n). It works much the same way that you would look for someone in the phone book:

#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

int binary_search(const vector<int>& vect, int val, int lowerbound, int upperbound);

int main()
{ const int RANDNUMS_SIZE = 1000;
srand(static_cast<int>(time(NULL)));
vector<int> randnums(RANDNUMS_SIZE);

for (int count = 0; count < RANDNUMS_SIZE; count++)
{ randnums[count] = rand() % RANDNUMS_SIZE; }

sort(randnums.begin(), randnums.end());

cout << "Enter a number to find: ";
int numToFind;
cin >> numToFind;

int pos = binary_search(randnums, numToFind, 0, RANDNUMS_SIZE - 1);

if (pos > 0)
{ cout << "Value found at position: " << pos << endl; }
else
{ cout << "Value not in vector." << endl; }

system("pause");
}

int binary_search(const vector<int>& vect, int val, int lowerbound, int upperbound)
{ if (lowerbound > upperbound)
{ return -1; }

int mid = (lowerbound + upperbound) / 2;

if (vect[mid] == val)
{ return mid; }
else if (vect[mid] > val)
{ return binary_search(vect, val, lowerbound, mid - 1); }
else
{ return binary_search(vect, val, mid + 1, upperbound); }
}
cma at 2007-11-9 0:39:52 >