ADA Practical File

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Index

Sr. No Name of the Practical Date Signature


Practical No.: 1
Write a program to do a linear search on an array.
PSEUDO CODE:
LinearSearch(array, key)
for each item in the array
if item == value
return its index
CODE:
#include<iostream.h>
#include<conio.h>
#define MAX 100
int linear_search(int arr[MAX],int num, int element)
{
for(int i=0;i<num;i++)
if(arr[i]==element)
return i;
}
void main()
{
clrscr();
int num,arr[MAX],element,pos;
cout<<"\n\tName: Kshitij Goyal"<<endl;
cout<<"\tEnrollment No.: A023116120009"<<endl;
cout<<"\tPlease enter number of elements in array:";
cin>>num;
cout<<"\n\tPlease enter the elemets in array:\n";
for(int i=0;i<num;i++)
{
cout<<"\t\t";
cin>>arr[i];
}
cout<<"\tPlease enter the number to be searched:";
cin>>element;
pos=linear_search(arr,num,element);
if(pos<num)
{
cout<<"\n\tElement found at position "<<pos+1<<"."<<endl;
}
else
{
cout<<"\n\tElement not found.\n";
}
getch();
}
OUTPUT:
COMPLEXITY: O(n)
Practical No.: 2
Write a program to do selection sort on an array.
PSEUDO CODE:
selectionSort(array, size)
repeat (size - 1) times
set the first unsorted element as the minimum
for each of the unsorted elements
if element < currentMinimum
set element as new minimum
swap minimum with first unsorted position
end selectionSort
CODE:
#include<iostream.h>
#include<conio.h>
#define MAX 1000
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}

void selection_sort(int arr[MAX], int sizeofarr)


{
int min,temp;
for(int i=0;i<sizeofarr-1;i++)
{
min=i;
for(int j=i+1;j<sizeofarr;j++)
{
if(arr[j]<arr[min])
min=j;
}
swap(&arr[min],&arr[i]);
}
}
void main()
{
clrscr();
int n,arr[MAX];
cout<<"\n\n\tName : Kshitij Goyal";
cout<<"\n\tEnrollment No. : A023116120009."<<endl;
cout<<"\n\tPlease enter the size of array:";
cin>>n;
cout<<"\tPlease enter the elements of the array:"<<endl;
for(int i=0;i<n;i++)
{
cout<<"\t\t";
cin>>arr[i];
}
selection_sort(arr,n);
cout<<"\n\tSorted Array is(Ascending Order):"<<endl<<"\t";
for(int j=0;j<n;j++)
cout<<arr[j]<<" ";
getch();
}

OUTPUT:

COMPLEXITY: O(n2)

You might also like