You are on page 1of 8

Ex.

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

How nany Elenents are there in an array?5


Enter the elenents :
12
63
25
19
42

Enter the elenent which is to be searched:42


The elenent is present at locat ion :5
Process exited after 15.78 seconds with return value
Press any key to continue

RESULT:
The program was executed and the output was verified.
Ex.No :2
Binary Search
ATE :09lcla
AIM:

To write a c++ program to implement Binary Search


ALGORITHM:
Start the program
Initialize the low and high indexes to o and n-1, respectively, where nis the number of
elements in the array.
Set the middle index to the average of the low and high indexes.
Compare the target value to the middle element of the array.
If the target value is equal to the middle element, return the middle index.
If the target value is less than the middle element, set the high index to the middle index - 1
and go tostep 2.
If thetarget value is greater than the middle element, set the low index tothe middle index +
1and go to step 2.
" If the low index is greater than or equal to the high index, the target value is not in the array.
End the program.

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 BINSEARCH binsearch(int x,int low,int high)


int mid.
if }ow >high)
return -1,
mid=(ow high)/2;
if(x =amid])
return(mid):
else if(x<a<mid])
binsearch(x,low,mid-1):
else
binsearch(N,mid+1,high);
int main(void)

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

DATE :16los lo Quick Sort

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

Enter ho many Numbers:4

The Random Numbers are:41 18467 6334 26500


Sorted Numbers are:
6334 13467 26500
The t i e taken is 0.0eee0Be+000
- - -

Process exited after 2.091 seconds with return value 33


Press any key to continue

RESULT:
The program was executed and the output was verified.
Ex.No :4
Insertion Sort
DATE :23hala82
AIM:

Towrite a Cprogram to implement Insertion Sort.


ALGORITHM;
Start with the secondelement of the array.
Compare the second element with the first element. If the second element is smaller, swap
them.
" Move on to the third element. Compare it with the second element and then the first element.
If it is smaller than either,swap it with the appropriate element(s).
" Repeat this process for all elements in the array.
PROGRAM:
#include<stdio.h>
=include< conio.h>
void main)
int A[10],n,i;
void Insert_sort(int A[10],int n);
printf("\n\t\t Insertion sort");
printf("\nHow many elements are there?");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
Insert_sort(A,n);
getch0;
void Insert_sort(int A[10],int n)
int ij,temp;
for(i=1;i<=n-1;i++)
temp=A[i];
j=i-1;
while(ý> =o)&&(A(j]> temp)
A[j+1]=A[jl;
j=j-1;
Alj+i]=temp:
printf("\nThe sorted list of elements is... n");
for(i=0;i<n;i++)
printf("\n%d",A[i)

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.

You might also like