Search here

Pages

Saturday, August 13, 2011

Binary Search

In computer science, a binary search or half-interval search algorithm finds the position of a specified value (the input "key") within a sorted array. At each stage, the algorithm compares the input key value with the key value of the middle element of the array. If the keys match, then a matching element has been found so its index, or position, is returned. Otherwise, if the sought key is less than the middle element's key, then the algorithm repeats its action on the sub-array to the left of the middle element or, if the input key is greater, on the sub-array to the right. If the remaining array to be searched is reduced to zero, then the key cannot be found in the array and a special "Not found" indication is returned.
          A binary search halves the number of items to check with each iteration, so locating the an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquer search algorithm.
           Searching a sorted collection is a common task. A dictionary is a sorted list of word definitions. Given a word, one can find its definition. A telephone book is a sorted list of people's names, addresses, and telephone numbers. Knowing someone's name allows one to quickly find their telephone number and address.
If the list to be searched contains more than a few items (a dozen, say) a binary search will require far fewer comparisons than a linear search, but it imposes the requirement that the list be sorted. Similarly, a hash search can be faster than a binary search but imposes still greater requirements. If the contents of the array are modified between searches, maintaining these requirements may take more time than the searches! And if it is known that some items will be searched for much more often than others, and it can be arranged that these items are at the start of the list, then a linear search may be the best.
Program for Binary Search 
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
int bsearch(int a[],int low,int high,int ele);
void main()
{
int a[10],i,n,ele,found=0;
clrscr();
cout<<"enter the no of elements in to array";
cin>>n;
cout<<"enter elements in sorted order";
for (i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"enter the element to be searched";
cin>>ele;
cout.setf(ios::fixed|ios::left);
cout<<setw(10)<<"LOW"<<setw(10)<<"MID"<<setw(10)<<"HIGH";
cout<<endl;
found=bsearch(a,0,n-1,ele);
if(found==-1)
cout<<"element not found";
else
cout<<"element found at location "<<found;
getch();
}
int bsearch(int a[],int low, int high, int ele)
{
if(low<=high)
{
int mid =(low+high)/2;
cout<<setw(10)<<low;
cout<<setw(10)<<mid;
cout<<setw(10)<<high;
cout<<endl;
if(ele==a[mid])
return mid;
else if(ele<a[mid])
high=mid-1;
else if(ele>a[mid])
low=mid+1;
bsearch(a,low,high,ele);
}
else
return -1;
}

Read Full

Linear Search

In computer science, linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.            Linear search is the simplest search algorithm; it is a special case of brute-force search. Its worst case cost is proportional to the...
Read Full

Fibonacci Series printing using Functions

#include<iostream.h> #include<conio.h> #include<iomanip.h> void fibonacci(int ); void main() { int n; clrscr(); cout<<"Enter a digit to findout fibonacci around it's range"; cin>>n; fibonacci(n); getch(); } void fibonacci(int x) { int a=0,b=1,c; cout<<a<<"\t"<<b; while(c<x) { c=a+b; a=b; b=c; cout<<"\t"<<c; } } ...
Read Full

Factorial Using Recursion

Proram for Factorial using recursion #include<iostream.h> #include<conio.h> #include<iomanip.h> long int factorial(int ); void main() { long int f; int n; cout<<"Enter a digit to findout it's factorial"; cin>>n; f=factorial(n); cout<<"Factorial is"<<f; getch(); } long int factorial(int x) { while(x>=0) { if(x==0) return 1; else { int n=x*factorial(x-1); return n; } ...
Read Full

Pass by Value(swapping)

Program for Passby Value-PARAMETER PASSING #include<iostream.h> #include<conio.h> void swap(int,int); void main() { int a,b; cout<<"Enter any two numbers"; cin>>a>>b; swap(a,b); cout<<"\n the swapped valuesin the main funtion are"<<a<<"and"<<b; getche(); } void swap(int x,int y) { int t; t=x; x=y; y=t; cout<<"\n the  swapped values in the function are"...
Read Full

Pass by Address(swapping)

Program for Passby Address-PARAMETER PASSING #include<iostream.h> #include<conio.h> void swap(int &,int &); void main() { int a,b; cout<<"Enter any two numbers"; cin>>a>>b; swap(a,b); cout<<"\n the swapped valuesin the main funtion are"<<a<<"and"<<b; getche(); } void swap(int &x,int &y) { int t; t=x; x=y; y=t; cout<<"\n the swapped values in...
Read Full

Pass by reference(swapping)

 Program for Passby reference-PARAMETER PASSING#include<iostream.h> #include<conio.h> void swap(int *,int *); void main() { int a,b; cout<<"Enter any two numbers"; cin>>a>>b; swap(&a,&b); cout<<"\n the swapped valuesin the main funtion are"<<a<<"and"<<b; getche(); } void swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; cout<<"\n the swapped values...
Read Full

Pyramid of Numbers

Have you ever tried doing some pyramid of numbers as in the given     1    123     12345     123     1The program here works for at most 7 digitsProgram compiled and executed in turboc++ #include<iostream.h> #include<conio.h> void main() { long int i=0,n,j=1,k; clrscr(); cout<<"Enter any Odd digit number\t"; R:cin>>n; if((n%2)==0) { cout<<"The...
Read Full

Insertion Sort Using Functions

Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages: Simple implementation Efficient for (quite) small data sets Adaptive (i.e., efficient) for data sets that are already substantially...
Read Full

Bubble Sorting

Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list....
Read Full

Best, worst and average cases of Performance

In computer science, best, worst and average cases of a given algorithm express what the resource usage is at least, at most and on average, respectively. Usually the resource being considered is running time, but it could also be memory or other resources. In real-time computing, the worst-case execution time is often of particular concern since it is important to know how much time might be needed in the worst case...
Read Full

Thursday, August 11, 2011

Program using recursion to List all sub directories starting at a given path

#include <iostream> #include <string> #include <io.h> using namespace std; #define DIR "c:" int rec_dir_list(string path) { static int count = 0; struct _finddata_t c_file; long fh; string t_path = path; t_path += "\\*.*"; if((fh=_findfirst(t_path.c_str(),&c_file)) != -1) while(_findnext(fh, &c_file) == 0) // ignore '.' and '..' if(strncmp(c_file.name,...
Read Full

Reading of file dynamically

#include <iostream> #include <fstream.h> int main()  { char inLine[128]; char fname[24]; ifstream inFile; cout << "Enter a file that contains text: "; cin >> fname; /* open the file */ inFile.open(fname, ios::in); /* if opening didn't fail */ if(!inFile.fail()) { while(!inFile.eof()) { inFile >> inLine; ...
Read Full