Professional Documents
Culture Documents
No :1
Linear Search
DATE
AIM:
Towrite a C++ program to implement Linear Search.
ALGORITHM:
Start the program
The user is prompted to enter the numnber of elements in the
Theuser is prompted to enter the elementsof the array. array.
The user isprompted to enter the element to be searched for.
The element is searched for in the array using a
If the element is found, the index of the linear search algorithm.
If the element is not found, a element is printed to the console.
End the program. message printed to the console.
is
PROGRAM:
#include<iostream.h>
using namespace std;
class search
private:
int a[10],n,key;
public:
void get_data();
void Seq_Search(int);
void search:Seq_Search(int key)
{
int flag=o,mark;
for(int i=o;i<n;i++)
if(a(i]==key)
flag=1;
mark=i;
} break;
if(flag==1)
else cout<<"\nThe element is present at
cout<<"\n The element is not presentloincation:"<<mark+1;
the array:";
}
void search::get_data()
{
cout<<"\nHow many Elements are there in an array?";
Cin>>n;
cout<<"\n Enter the elements:":
for(int i-0:i<n;i+ +)
cin>>ali]:
int main()
int k;
search obj:
obj.get_data():
cout<<"n Enter the element which is to be searched:":
cin>>k:
obj.Seq_Search(k);
QUTPUT:
F\programs\c++ programsWinear.exe
RESULT:
The program was executed and the output was verified.
Ex.No :2
Binary Search
ATE :09lcla
AIM:
PROGRAM:
#includexiostream.h>
#define size 10
using namespace std;
class BINSEARCH
{
private:
int a[size];
public:
int low,high;
BINSEARCH);
int get_value);
int binsearch(int x,int low,int high);
}:
BINSEARCH::BINSEARCH)
low = 0;
}
int BINSEARCH::get_value0
{
int n;
cout<<"\nEnter the total number of elements";
cin>>n;
cout<<"\nEnter the list of elements \n";
for(int i-0;i<ni++)
cin>>ali].
return n.
int kev.ans,n;
BINSEARCH obj:
cout<<"\n\t\t\t Binary Search Method";
n=obj get_value();
cout<<"\n Enter the element which you want to search";
cin>>key;
obj.high=n-1;
ans=obj.binsearch(key, obj.low,obj.high);
if(ans!= -1)
cout<<"|n number"<<key<<"is present in the list at
else location"<<ans+1;
cout<<"\n The number is not present in the list";
QLTPUL:
Output
Cp/jr 3l Jv6s VA.0
Binary Starch Method
Enter the total number of elements4
Enter (he !1St of elementS
33
44
55
67
Enter (he elenent which you nant
co searchs5
The nuber 1S fhot
present ln (he list
RESULT:
The program was
executed and the output was verified.
Ex.No :3
AIM:
Sort agiven set of elements using the Quick sort method and determine the time required to sort
the elements. Repeat the experiment for different values of n, the number of elements in the list to
be sortedand plot a graph of the time taken versus n. The elements can be read from afile or can be
generated using the random number generator.
ALGORITHM:
Start the program
The user enters the number of numbers to be sorted.
Generates a random number for each element in an array.
Prints the random numbers.
Calls the Quick Sort function to sort the numbers in the array.
Prints the sorted numbers.
The program prints the time taken to sort the numbers.
End the program.
PROGRAM:
#include<stdio.h>
#include<time.h>
void Exch(int "p,int *){
int temp= "p;
*p=*q;
*q= temp;
void QuickSort(int a[),int low, int high){
int ij,key, k;
if(low> =high)
return;
key=low;
i=low+1;
j=high;
while(i<=j){
while(a[i]<=a[key])
i=i+1;
while(a[j]>a[key])
j=j-1;
if(i<j)
Exch(&a[i],&a[j]);
Exch(&ajl,ka[key]):
QuickSort(a,low.j-1);
QuickSort(aj+1,high),
void main(){
int n,a[10o0),k;
int clock st,et;
double ts;
printf("\n Enter how many Numbers:"):
scanf("%d", &n):
printf("\n The Random Numbers are:");
for(k=1;kK=n;k++){
a[k]=rand);
printf("%d\t". a[k);
ts=clock();
QuickSort(a, 1,n);
et=clock):
ts=(double) (et-ts)/CLOCKS_PER_ SEC;
printf("\n SOrted Numbers are:\n");
for(k=1;k<=n;k++)
printf("%d\t",a[k]);
printf(" n The time taken is %e",ts);
QUTPUT:
Dc programs\Quick.exe
RESULT:
The program was executed and the output was verified.
Ex.No :4
Insertion Sort
DATE :23hala82
AIM:
QUTPUT:
FAinsertionexe
Insertion sort
How nany elenents are there?5
Enter the elenents
37
18
73
54
10
Ihe sorted list of elements is...
18
37
54
73
RESULT:
The program was executed and the output was verified.