0% found this document useful (0 votes)
13 views20 pages

Practical file part 1

This document is a practical file for a course on Design and Analysis of Algorithms, submitted by Ashutosh Kewat Manjhi. It includes various programming assignments (WAP) that implement sorting algorithms, search algorithms, and other algorithmic problems, along with their time complexity analyses. The document features code snippets for algorithms like Insertion Sort, Selection Sort, Quick Sort, and more, along with a detailed time complexity table.

Uploaded by

Ashutosh Kewat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

Practical file part 1

This document is a practical file for a course on Design and Analysis of Algorithms, submitted by Ashutosh Kewat Manjhi. It includes various programming assignments (WAP) that implement sorting algorithms, search algorithms, and other algorithmic problems, along with their time complexity analyses. The document features code snippets for algorithms like Insertion Sort, Selection Sort, Quick Sort, and more, along with a detailed time complexity table.

Uploaded by

Ashutosh Kewat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

[Department of Information Technology]

[Design and Analysis of Algorithm]


(240301)
[Practical file]
Submitted to: - Prof. Vishwas Shrivastava sir

Submitted by: - Ashutosh Kewat Manjhi


(0901AI201014)
INDEX

S.No. Program name Page No. Remarks


1 1. WAP to implement the following using array as 3 - 19
data structure and analyse its time complexity.
a. Insertion sort b. Selection sort c. Bubble sort
d. Quick sort e. Merge sort f. Bucket sort
g. Shell sort h. Radix sort i. Heap sort
2 WAP to implement Linear and Binary Search and 20 - 21
analyze its time complexity.
3 WAP to implement Matrix Chain Multiplication and 21 - 24
analyze its time complexity.
4 WAP to implement Longest Common Subsequence 25 - 26
Problem and analyze its time complexity.
5 WAP to implement Optimal Binary Search Tree 27 - 28
Problem and analyze its time complexity.
6 WAP to implement Huffman Coding and analyze its 29- 35
time complexity.
7 WAP to implement Dijkstra’s Algorithm and analyze 35 - 38
its time complexity
8 WAP to implement Bellman Ford Algorithm and 39 - 42
analyze its time complexity.
9 WAP to implement DFS and BFS and analyze their 43 - 46
time complexities.
10 WAP to Implement 0/1 knapsack using dynamic 47
programming.
1) WAP to implement the following using array as data structure and
analyse its time complexity.
A, Insertion sort
Code-
#include <bits/stdc++.h>
using namespace std;
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {11, 12, 27, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
Output –

B, Selection Sort

Code-
#include <bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr[] = {67, 27, 14, 20, 13};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

Output -

C, Bubble sort
Code-
#include <bits/stdc++.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(&arr[j], &arr[j + 1]);
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
// Driver code
int main()
{
int arr[] = {67, 32, 24, 13, 21, 17, 80};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}

Output-
D, Quick Sort
Code-
#include <bits/stdc++.h>
using namespace std;
int partition(int arr[], int l, int h)
{
int pivot = arr[l];
int i = l;
int j = h;
while (i < j)
{
while (arr[i] <= pivot && i < j)
i++;
while (arr[j] > pivot && i <= j)
j--;
if (i < j)
{
swap(arr[i], arr[j]);
}
}
swap(arr[l], arr[j]);
return j;
}
void quicksort(int arr[], int l, int h)
{
if (l < h)
{
int pivot = partition(arr, l, h);
quicksort(arr, l, pivot - 1);
quicksort(arr, pivot + 1, h);
}
}
int main()
{
int arr[6] = {7, 8, 2, 5, 1, 0};
cout << "array after sorting:" << endl;
quicksort(arr, 0, 5);
for (int i = 0; i < 6; i++)
{
cout << arr[i] << " ";
}
return 0;
}

Output –

E, Bucket sort
#include <stdio.h>
#include <stdlib.h>
#define NARRAY 7 // Array size
#define NBUCKET 6 // Number of buckets
#define INTERVAL 10 // Each bucket capacity
struct Node
{
int data;
struct Node *next;
};
void BucketSort(int arr[]);
struct Node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);
void BucketSort(int arr[])
{
int i, j;
struct Node **buckets;
buckets = (struct Node **)malloc(sizeof(struct Node *)
* NBUCKET);
for (i = 0; i < NBUCKET; ++i)
{
buckets[i] = NULL;
}
for (i = 0; i < NARRAY; ++i)
{
struct Node *current;
int pos = getBucketIndex(arr[i]);
current = (struct Node *)malloc(sizeof(struct
Node));
current->data = arr[i];
current->next = buckets[pos];
buckets[pos] = current;
}
for (i = 0; i < NBUCKET; i++)
{
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
for (i = 0; i < NBUCKET; ++i)
{
buckets[i] = InsertionSort(buckets[i]);
}
printf(" -------------\n");
printf("Bucktets after sorting\n");
for (i = 0; i < NBUCKET; i++)
{
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
for (j = 0, i = 0; i < NBUCKET; ++i)
{
struct Node *node;
node = buckets[i];
while (node)
{
arr[j++] = node->data;
node = node->next;
}
}
return;
}
struct Node *InsertionSort(struct Node *list)
{
struct Node *k, *nodeList;
if (list == 0 || list->next == 0)
{
return list;
}
nodeList = list;
k = list->next;
nodeList->next = 0;
while (k != 0)
{
struct Node *ptr;
if (nodeList->data > k->data)
{
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = nodeList;
nodeList = tmp;
continue;
}
for (ptr = nodeList; ptr->next != 0; ptr = ptr-
>next)
{
if (ptr->next->data > k->data)
break;
}
if (ptr->next != 0)
{
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = ptr->next;
ptr->next = tmp;
continue;
}
else
{
ptr->next = k;
k = k->next;
ptr->next->next = 0;
continue;
}
}
return nodeList;
}
int getBucketIndex(int value)
{
return value / INTERVAL;
}
void print(int ar[])
{
int i;
for (i = 0; i < NARRAY; ++i)
{
printf("%d ", ar[i]);
}
printf("\n");
}

void printBuckets(struct Node *list)


{
struct Node *cur = list;
while (cur)
{
printf("%d", cur->data);
cur = cur->next;
}
}
int main(void)
{
int array[NARRAY] = {42, 32, 33, 52, 37, 47, 51};
printf(" --- \n");

printf("Initial array: ");


print(array);

BucketSort(array);

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

print(array);
return 0;
}
Output –
F, Radix sort
Code –
#include <iostream>
using namespace std;
int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp)
{
int output[n];
int i, count[10] = {0};
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--)
{
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[], int n)
{
int m = getMax(arr, n);
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
void print(int arr[], int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}
int main()
{
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr) / sizeof(arr[0]);
radixsort(arr, n);
print(arr, n);
return 0;
}

Output –
G, Heap sort
Code-
#include <iostream>
using namespace std;
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--)
{
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << "\n";
}
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
cout << "Sorted array is \n";
printArray(arr, n);
}

Output –
H, Merge sort
Code –
#include <stdio.h>
int main()
{
int n1, n2, n3, i, j, k;
printf("\nEnter the size of first array : ");
scanf("%d", &n1);
printf("\nEnter the size of second array : ");
scanf("%d", &n2);
n3 = n1 + n2;
printf("\nEnter the sorted array 1 elements :
");
int A[n1], B[n2], C[n3];
for (int i = 0; i < n1; i++)
{
scanf("%d", &A[i]);
C[k] = A[i];
k++;
i++;
}
printf("\nEnter the sorted array 2 elements :
");
for (int j = 0; j < n2; j++)
{
scanf("%d", &B[j]);
C[k] = B[j];
k++;
j++;
}
printf("Merged Array is : ");
for (k = 0; k < (n1 + n2); k++)
{
printf("%d ", C[k]);
}
return 0;
}

TIME COMPLEXITY:

Sorting Best Average Worst


Algorithm
Selection sort Ω(n^2) θ(n^2) O(n^2)

Bubble sort Ω(n) θ(n^2) O(n^2)

Insertion Sort Ω(n) θ(n^2) O(n^2)

Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n))

Quick sort Ω(n log(n)) θ(n log(n)) O(n^2)

Merge sort Ω(n log(n)) θ(n log(n)) O(n log(n))

Bucket Sort Ω(n+k) θ(n+k) O(n^2)

Radix Sort Ω(nk) θ(nk) O(nk)


2, WAP to implement Linear and Binary Search and analyze its time Complexity.
Code-
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " <<
result;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy