Search results for 'Bubble Sort'. 1 post(s) found.

  1. 2007/09/08 Binary Search Sample Code
2007/09/08 18:10

Binary Search Sample Code


This is Binary Search that returns a negative one if the number is not found.
This code does a Bubble Sort and then proforms the Binary Search.

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
const c=10;
void inputa(int a[ ]);
void outputa(int a[ ]);
void dosort(int a[ ]);
void swap(int &x, int &y);
void search(int a[ ]);
int dosearch(int a[ ], int b);
int main()
{
int a[c];
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
inputa(a);
outputa(a);
cout<<endl;
dosort(a);
outputa(a);
search(a);
return 0;
}
void inputa(int a[ ])
{
int i;
for(i=0; i<c;i++)
{
a[i]=rand()%101;
}
}
void outputa(int a[ ])
{
int pig;
for(pig=0; pig<c;pig++)
{
cout<<a[pig]<<endl;
}
}
void dosort(int a[ ])
{
int j, pig;
for(pig=0;pig<c;pig++)
{
for(j=0;j<c;j++)
{
if (a[j]>a[pig])
{
swap(a[j],a[pig]);
}
}
}
}
void swap(int &a, int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
void search(int a[ ])
{
int b;
cout<<"Enter Number to Search for: ";
cin>>b;
dosearch(a,b);
cout<<"The number was found in line number "<<dosearch(a,b)<<endl;
}
int dosearch(int a[ ], int b)
{
int high=c;
int mid=c/2;
int low=-1;
int d=0;
int line=0;
do
{
if(b>a[mid])
{
low=mid+1;
mid=(high+low)/2;
}
else if (b<a[mid])
{
high=mid+1;
mid=(high+low)/2;
}
else if(a[mid]==b)
{
return mid+1;
}
}while(low<high);
return -1;
}
Trackback 13 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Free rape videos.

    Tracked from Rape in the movies. 2009/02/12 12:33 delete

    Rape porn. Sex rape. Free rape stories.

  2. Subject Meridia coupon.

    Tracked from Meridia. 2009/02/13 08:34 delete

    Monument to the flag in meridia. Meridia meridia. Order meridia pill.

  3. Subject Cialis best price buy online.

    Tracked from Online pharmacy buy cialis. 2009/03/07 08:37 delete

    Buy cialis phentermine. Cheapest cialis. Cialis.

  4. Subject Viagra side effects.

    Tracked from Generic viagra. 2009/03/09 01:11 delete

    Viagra. Viagra canada. Viagra side effects. Generic viagra. Viagra for women. Free viagra. Free sample viagra.

  5. Subject Medical fetish.

    Tracked from Fetish wear. 2009/03/13 17:55 delete

    Sneeze fetish. Panty fetish. Foot fetish. Foot fetish tgp.

  6. Subject How do you snort percocet.

    Tracked from Percocet. 2009/03/17 11:58 delete

    Buy percocet online. Percocet withdrawal symptoms. Percocet aspirin. Generic percocet. Percocet.

  7. Subject Valtrex side effects.

    Tracked from Valtrex. 2009/04/03 18:44 delete

    Valtrex. Buy valtrex com. Valtrex information. Buy valtrex without prescription. Symptoms of valtrex overdose.

  8. Subject Codeine.

    Tracked from How codeine effects the brain. 2009/04/29 11:20 delete

    Cocaine and codeine. Buy codeine. Tylenol with codeine. Tylenol with codeine elixir dosing. What is apap codeine. Source of codeine. Buy fiorinal with codeine.

  9. Subject Codeine.

    Tracked from Buy fiorinal with codeine. 2009/05/02 20:40 delete

    Codeine us pharmacy. Robitusin with codeine. Codeine. Codeine antitussive action. Side effects of codeine.

  10. Subject Antidote soma valium.

    Tracked from No prescription generic valium. 2009/06/01 08:58 delete

    Valium side effects. Valium generic. Buy valium. Side effects of valium. Valium liquid form. Valium.

  11. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 04:09 delete

    moneyideas

  12. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 13:10 delete

    moneyideas

  13. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:38 delete

    moneyideas