Algorithm Design and Analysis GGSIPU Complete Lab File
Algorithm Design and Analysis GGSIPU Complete Lab File
(ETCS-351)
Submitted By :-
NAME :- Ashwini Anand Ojha
Enroll. No. :- 03310102719
BATCH :- B.Tech(Computer Science and Engineering), 2019-23
SEMESTER :- 5th
Serial No. Experiment Page No. Faculty
Sign.
To implement selection
1. sort algorithm using an
1-2
array as a data structure.
To implement bubble
2. sort algorithm using an
3-4
array as a data structure.
To implement Linear
6. search and Binary search
15-17
algorithms.
To implement Rabin-Karp
7. Algorithm for Pattern
18-20
Searching.
To implement Knuth,
8. Morris, and Pratt string
21-23
searching algorithm.
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 a[100], n, i, j, position, swap;
printf("Enter number of elements");
scanf("%d", &n);
printf("Enter %d Numbers", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%d", a[i]);
return 0;
}
OUTPUT
EXPERIMENT - 2
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 array[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d Numbers:n", n);
printf("Sorted Array:n");
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 <stdio.h>
#include <stdlib.h>
// Second subarray is
arr[m+1..r]
merge(arr, l, m, r);
}
}
/* UTILITY FUNCTIONS */
/* Function to print an array */
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
/* Driver code */
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
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 <stdio.h>
int main()
{
int list[50];
int size, i;
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("After applying quick sort\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");
return 0;
}
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>
// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
// Print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}
// Driver
code int
main() {
int arr[] = {1, 12, 9, 5, 6, 10};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
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 :
1. Linear Search :
#include<stdio.h>
int main()
{
int a[20],i,x,n;
printf("How many elements?");
scanf("%d",&n);
for(i=0;i<n;++i)
if(a[i]==x)
break;
if(i<n)
printf("Element found at index %d",i);
else
printf("Element not found");
return 0;
}
OUTPUT :
2. Binary Search :
#include <stdio.h>
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
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>
// if p == t and pat[0...M-1] =
txt[i, i+1, ...i+M-1] if (j == M)
printf("Pattern found at index %d \n", i);
}
// A prime
number int q =
101;
// function call
search(pat,
txt, 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
int n = strlen(text);
int m = strlen(pattern);
return 0;
}
OUTPUT