You are on page 1of 23

ALGORITHMS ANALYSIS AND DESIGN

(ETCS-351)

PRACTICAL RECORD FILE


(2021-22)
Submitted by :-
Name :- AMAN KUMAR MAHTO
Roll No. :- 04910102719
Branch :- CSE
Sem : V

Submitted to:- Prof. Audithan Sivaraman

Netaji Subhas University of


Technology (East Campus)
EXPERIMENT - 1
AIM : To implement selection sort algorithm using an array as a data structure.

THEORY :
The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from the unsorted part and putting it at
the beginning.
The algorithm maintains two subarrays in a given array :
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending
order) from the unsorted subarray is picked and moved to the sorted subarray.

CODE :

#include <stdio.h>

int main()

int arr[100], n, i, j, pos, sw;

printf("Enter number of elements : ");

scanf("%d", &n);

printf("Enter %d Numbers.\n", n);

for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

for(i = 0; i < n - 1; i++)

pos=i;

for(j = i + 1; j < n; j++)

if(arr[pos] > arr[j])

pos=j;
}

if(pos != i)

sw=arr[i];

arr[i]=arr[pos];

arr[pos]=sw;

printf("Sorted Array\n");

for(i = 0; i < n; i++)

printf("%d ", arr[i]);

return 0;

OUTPUT :
EXPERIMENT - 2
AIM : To implement bubble sort algorithm using an array as a data structure.

THEORY :
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping
the adjacent elements if they are in the wrong order.

CODE :

int main()

int arr[100], n, i, j, sw;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d Numbers.\n", n);

for(i = 0; i < n; i++)

scanf("%d", &arr[i]);

for(i = 0 ; i < n - 1; i++)

for(j = 0 ; j < n-i-1; j++)

if(arr[j] > arr[j+1])

sw=arr[j];

arr[j]=arr[j+1];

arr[j+1]=sw;

}
}

printf("Sorted Array:n\n");

for(i = 0; i < n; i++)

printf("%d ", arr[i]);

return 0;

OUTPUT :
EXPERIMENT - 3
AIM : To implement merge sort algorithm using an array as a data structure.

THEORY :
Merge Sort is a Divide and Conquer algorithm. It divides the input array into two
halves, calls itself for the two halves, and then merges the two sorted halves. The
merge() function is used for merging two halves. The merge(arr, l, m, r) is a key
process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the
two sorted subarrays into one.

CODE :

#include<iostream>
using namespace std;

void merge(int arr[],int s,int e){

int i=s;
int j=s;

int mid=(s+e)/2;
int k=mid+1;

int out[100];

while(i<=mid&&k<=e){

if(arr[i]<arr[k]){

out[j++]=arr[i++];

}
else{

out[j++]=arr[k++];
}
}
while(i<=mid){

out[j++]=arr[i++];
}
while(k<=e){

out[j++]=arr[k++];
}
for(int i=s;i<=e;i++){

arr[i]=out[i];
}

void merge_sort(int arr[],int s,int e){

if(s>=e){

return;
}

int mid=(s+e)/2;

merge_sort(arr,s,mid);
merge_sort(arr,mid+1,e);
merge(arr,s,e);

int main(){

int n;
cin>>n;

int arr[100];

for(int i=0;i<n;i++){

cin>>arr[i];
}

merge_sort(arr,0,n-1);

cout<<"sorted array :";


for(int i=0;i<n;i++){

cout<<arr[i]<<" ";
}

return 0;
}

OUTPUT :
EXPERIMENT - 4
AIM : To implement quick sort algorithm using an array as a data structure.

THEORY :
QuickSort is a Divide and Conquer algorithm. It picks an element as pivot
and partitions the given array around the picked pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and
an element x of array as pivot, put x at its correct position in sorted array and put all
smaller elements (smaller than x) before x, and put all greater elements (greater
than x) after x.

CODE :

#include <iostream>
using namespace std;

int parti_on(int arr[],int s,int e){

int piv=arr[e];

int i=s-1;
int j=s;

for( ;j<=e-1;j++){

if(arr[j]<=piv){

i=i+1;

swap(arr[i],arr[j]);
}

swap(arr[e],arr[i+1]);

return i+1;
}

void quick_sort(int arr[],int s,int e){

if(s>=e){

return;
}

int p=parti_on(arr,s,e);
quick_sort(arr,s,p-1);

quick_sort(arr,p+1,e);

int main() {
int n;
cin>>n;

int arr[100];

for(int i=0;i<n;i++){

cin>>arr[i];
}

quick_sort(arr,0,n-1);

for(int i=0;i<n;i++){

cout<<arr[i]<<" ";
}

return 0;
}

OUTPUT :
EXPERIMENT - 5
AIM : To implement heap sort algorithm using an array as a data structure.

THEORY :
Heap sort is a comparison-based sorting technique based on Binary Heap data
structure. It is similar to selection sort where we first find the minimum element and
place the minimum element at the beginning. We repeat the same process for the
remaining elements.

CODE :

#include <stdio.h>
/* function to heapify a subtree. Here 'i' is the

index of root node in array a[], and 'n' is the size of heap. */

void heapify(int a[], int n, int i)

{
int largest = i; // Initialize largest as root

int left = 2 * i + 1; // left child

int right = 2 * i + 2; // right child


// If left child is larger than root

if (left < n && a[left] > a[largest])

largest = left;
// If right child is larger than root

if (right < n && a[right] > a[largest])

largest = right;
// If root is not largest

if (largest != i) {
// swap a[i] with a[largest]

int temp = a[i];

a[i] = a[largest];

a[largest] = temp;

heapify(a, n, largest);
}
}

/*Function to implement the heap sort*/

void heapSort(int a[], int n)


{

for (int i = n / 2 - 1; i >= 0; i--)

heapify(a, n, i);
// One by one extract an element from heap

for (int i = n - 1; i >= 0; i--) {


/* Move current root element to end*/
// swap a[0] with a[i]

int temp = a[0];

a[0] = a[i];

a[i] = temp;

heapify(a, i, 0);
}
}

/* function to print the array elements */

void printArr(int arr[], int n)


{

for (int i = 0; i < n; ++i)


{
printf("%d", arr[i]);

printf(" ");
}

int main()
{

int a[] = {48, 10, 23, 43, 28, 26, 1};

int n = sizeof(a) / sizeof(a[0]);

printf("Before sorting array elements are - \n");


printArr(a, n);

heapSort(a, n);

printf("\nAfter sorting array elements are - \n");

printArr(a, n);

return 0;
}

OUTPUT :
EXPERIMENT - 6
AIM : To implement Linear search and Binary search algorithms.

THEORY :
Linear Search :
Iterate through every element in an array, check if it matches with the given element.

Binary Search :
Search a sorted array by repeatedly dividing the search interval in half. Begin with an
interval covering the whole array. If the value of the search key is less than the item in
the middle of the interval, narrow the interval to the lower half. Otherwise, narrow it to
the upper half. Repeatedly check until the value is found or the interval is empty.

CODE :

Linear Search :

#include<stdio.h>

int main()

int arr[20],i,x,n;

printf("Enter No. of elements : "); scanf("%d",&n);

printf("Enter array elements\n");

for(i=0;i<n;++i)

scanf("%d",&arr[i]);

printf("Enter element to search : ");

scanf("%d",&x);

for(i=0;i<n;++i)

if(arr[i]==x)

break;

if(i<n)

printf("Element found at index : %d",i);


else

printf("Element not found");

return 0;

OUTPUT :

2. Binary Search :

Code:

#include<iostream>
using namespace std;

int main()
{

// clrscr();
int n,i,arr[50],num,first,last,middle;

cout<<"Enter total no. of elemets :"<<" ";

cin>>n;

cout<<"Enter the elements :"<<endl;

for(i=0;i<n;i++)

{
cin>>arr[i];

cout<<"Enter the no. to be found :"<<" ";

cin>>num;

first=0;

last=(n-1);

do

{
middle=(first+last)/2;

if(arr[middle]==num)

cout<<num<<" "<<"found at position no. "<<" "<<middle+1<<endl;

break;

else if(arr[middle]<num)
{

first=middle+1;

else

last=middle-1;

} while(first<=last);

if(first>last)

cout<<"Entered no."<<" "<<num<<" "<< "is not present in list\n";

return 0;

OUTPUT :
EXPERIMENT - 7
AIM : To implement Rabin-Karp Algorithm for Pattern Searching

THEORY :
Rabin-Karp algorithm is an algorithm used for searching/matching patterns in the text
using a hash function. Unlike Naive string matching algorithm, it does not travel
through every character in the initial phase rather it filters the characters that do not
match and then performs the comparison.

CODE :

#include<stdio.h>
#include<string.h>
int main (){

char txt[80], pat[80];

int q;

printf ("Enter the container string \n");

scanf ("%s", &txt);

printf ("Enter the pattern to be searched \n");

scanf ("%s", &pat);

int d = 256;

printf ("Enter a prime number \n");

scanf ("%d", &q);

int M = strlen (pat);

int N = strlen (txt);

int i, j;

int p = 0;

int t = 0;

int h = 1;

for (i = 0; i < M - 1; i++)


h = (h * d) % q;

for (i = 0; i < M; i++){

p = (d * p + pat[i]) % q;

t = (d * t + txt[i]) % q;
}

for (i = 0; i <= N - M; i++){

if (p == t){

for (j = 0; j < M; j++){

if (txt[i + j] != pat[j])

break;
}

if (j == M)

printf ("Pattern found at index %d \n", i);


}

if (i < N - M){

t = (d * (t - txt[i] * h) + txt[i + M]) % q;

if (t < 0)

t = (t + q);

}
}

return 0;
}

OUTPUT :
EXPERIMENT - 8
AIM : To implement Knuth, Morris, and Pratt string searching algorithm.

THEORY :
The KMP Algorithm (or Knuth, Morris, and Pratt string searching algorithm) cleverly
uses the previous comparison data. It can search for a pattern in O(n) time as it
never re-compares a text symbol that has matched a pattern symbol. However, it
uses a partial match table to analyze the pattern structure. Construction of a partial
match table takes O(m) time. Therefore, the overall time complexity of the KMP
algorithm is O(m + n).

CODE :
#include <iostream>
using namespace std;

// Function to implement the KMP algorithm


void KMP(string text, string pattern)
{
int m = text.length();
int n = pattern.length();

// if pattern is an empty string


if (n == 0)
{
cout << "The pattern occurs with shift 0";
return;
}

// if text's length is less than that of pattern's


if (m < n)
{
cout << "Pattern not found";
return;
}

// next[i] stores the index of the next best partial match


int next[n + 1];

for (int i = 0; i < n + 1; i++) {


next[i] = 0;
}

for (int i = 1; i < n; i++)


{
int j = next[i + 1];
while (j > 0 && pattern[j] != pattern[i]) {
j = next[j];
}

if (j > 0 || pattern[j] == pattern[i]) {


next[i + 1] = j + 1;
}
}

for (int i = 0, j = 0; i < m; i++)


{
if (text[i] == pattern[j])
{
if (++j == n) {
cout << "The pattern occurs with shift " << i - j + 1 << endl;
}
}
else if (j > 0)
{
j = next[j];
i--; // since `i` will be incremented in the next iteration
}
}
}

// Program to implement the KMP algorithm in C++


int main()
{
string text = "ABCABAABCABAC";
string pattern = "CAB";

KMP(text, pattern);

return 0;
}

OUTPUT :

You might also like