Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views25 pages

Design and Analysis of Algorithms

Uploaded by

ninjadevil139
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)
5 views25 pages

Design and Analysis of Algorithms

Uploaded by

ninjadevil139
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/ 25

Design And Analysis of

Algorithms
LAB Report

Submitted by Submitted to
Name: Shreyansh Gupta Mr. Rahul Moriwal
(Assistant Professor)
Section: AI
GLA University Mathura
Class Roll No.: 21
Batch: 2024 – 2025
University Roll No. 2215001707
Index
Exp. Page
No Title of the Experiment No Signature

1 Write a program implementing Binary Search 1

2 Write a program implementing Bubble Sort 3

3 Write a program implementing Selection Sort 5

4 Write a program implementing Merge Sort 7

5 Write a program implementing Quick Sort 9

6 Write a program implementing Strassen’s Matrix 11


Multiplication

7 Write a program to implement minimum spanning 14


tree using Kruskal's algorithm

8 Write a program to implement minimum spanning 16


tree using Prim’s algorithm

9 Write a program to implement single source 19


shortest path using Dijkstra’s problem

10 Write a program to implement Floyd - Warshall 22


algorithm
Experiment – 01
Objective:

• Write a program implementing Binary Search.

Theory:
Binary Search is an efficient searching algorithm used on sorted
arrays. It works by repeatedly dividing the search interval in half. If the
target value is less than the middle element, the search continues in the
left half; otherwise, it continues in the right half. Its time complexity is
O(log n), making it much faster than linear search for large datasets.

Implementation:
public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int low = 0, high = arr.length - 1;
while (low <= high) {
int mid = low + (high - low) / 2;

if (arr[mid] == target)
return mid;
else if (arr[mid] < target)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}

public static void main(String[] args) {


int[] arr = { 10, 20, 30, 40, 50 };
int target = 40;
int result = binarySearch(arr, target);
System.out.println("Original Array !!");
for (int i : arr)
System.out.print(i + " ");
System.out.println("\nTarget element: " + target);
if (result != -1)
System.out.println("Element found at index: " + result);
else

1
System.out.println("Element not found.");
}
}

Output:

2
Experiment – 02
Objective:

• Write a program implementing Bubble Sort.

Theory:
Bubble Sort is one of the simplest sorting algorithms. It works by
repeatedly stepping through the list, comparing adjacent elements, and
swapping them if they are in the wrong order. This process is repeated
until the list is sorted. Though inefficient for large datasets, it's useful for
educational purposes. The time complexity is O(n²) in the worst and
average case, and O(n) in the best case when the list is already sorted.

Implementation:

public class BubbleSort {


public static void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped)
break;
}
}

public static void main(String[] args) {


int[] arr = { 5, 1, 4, 2, 8 };
System.out.println("Original Array !!");
for (int i : arr)
System.out.print(i + " ");

System.out.println("\nAfter Bubble Sort !!");


bubbleSort(arr);

3
for (int i : arr)
System.out.print(i + "
");
}
}

Output:

4
Experiment – 03
Objective:

• Write a program implementing Selection Sort.

Theory:
Selection Sort is a simple comparison-based sorting algorithm. It works
by repeatedly selecting the smallest (or largest) element from the unsorted
portion and moving it to the sorted portion. It has a time complexity of
O(n²) for all cases. Though inefficient on large lists, it is easy to
understand and implement.

Implementation:
public class SelectionSort {
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

public static void main(String[] args) {


int[] arr = { 64, 25, 12, 22, 11 };
System.out.println("Original Array !!");
for (int i : arr)
System.out.print(i + " ");

System.out.println("\nAfter Selection Sort !!");


selectionSort(arr);
for (int i : arr)
System.out.print(i + " ");
}
}

5
Output:

6
Experiment – 04
Objective:

• Write a program implementing Merge Sort.

Theory:
Merge Sort is a recursive sorting algorithm that splits the array into
halves, sorts each half and then merges them. It is stable and has a consistent
time complexity of O(n log n) in all cases.

Implementation:

public class MergeSort {


public static void mergeSort(int[] arr, int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

private static void merge(int[] arr, int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;
int[] L = new int[n1];
int[] R = new int[n2];

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


L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

int i = 0, j = 0, k = l;
while (i < n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}

7
public static void main(String[] args)
{ int[] arr = { 12, 11, 13, 5, 6, 7
};

System.out.println("Original Array
!!"); for (int i : arr)
System.out.print(i + " ");

System.out.println("\nAfter Merge Sort !!");


mergeSort(arr, 0, arr.length - 1);
for (int i : arr)
System.out.print(i + "
");
}
}
Output:

8
Experiment – 05
Objective:

• Write a program implementing Quick Sort.

Theory:
Quick Sort is a divide-and-conquer sorting algorithm. It selects a pivot
element and partitions the array so that elements smaller than the pivot are
moved to the left, and larger to the right. Then it recursively sorts the
subarrays. The average time complexity is O(n log n), and the worst-case
is O(n²).

Implementation
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

private static int partition(int[] arr, int low, int high) {


int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}

9
public static void main(String[] args)
{ int[] arr = { 10, 7, 8, 9, 1, 5
};

System.out.println("Original Array
!!"); for (int i : arr)
System.out.print(i + " ");

System.out.println("\nAfter Quick Sort !!");


quickSort(arr, 0, arr.length - 1);
for (int i : arr)
System.out.print(i + "
");
}
}

public static void main(String[] args)


{ int[] arr = { 10, 7, 8, 9, 1, 5
};

System.out.println("Original Array
!!"); for (int i : arr)
System.out.print(i + " ");

System.out.println("\nAfter Quick Sort !!");


quickSort(arr, 0, arr.length - 1);
for (int i : arr)
System.out.print(i + "
Output: ");
}
}

10
Experiment – 06
Objective:

• Write a program implementing Strassen’s Matrix Multiplication.

Theory:
Strassen's Matrix Multiplication is the divide and conquer approach to
solve the matrix multiplication problems. The usual matrix multiplication
method multiplies each row with each column to achieve the product
matrix. The time complexity taken by this approach is O(n3), since it
takes two loops to multiply. Strassens method was introduced to reduce
the time complexity from O(n3) to O(nlog 7).

Implementation:

11
12
Output:

13
Experiment – 07
Objective:

• Write a program to implement minimum spanning tree using Kruskal's


algorithm.

Theory:
Kruskal's minimal spanning tree algorithm is one of the efficient methods
to find the minimum spanning tree of a graph. A minimum spanning tree
is a subgraph that connects all the vertices present in the main graph with
the least possible edges and minimum cost (sum of the weights assigned to
each edge). The inputs taken by the kruskals algorithm are the graph G {V,
E}, where V is the set of vertices and E is the set of edges, and the source
vertex S and the minimum spanning tree of graph G is obtained as an
output.

Implementation:

14
Output:

15
Experiment – 08
Objective:

• Write a program to implement minimum spanning tree using Prim’s


algorithm.

Theory:
Prim's minimal spanning tree algorithm is one of the efficient methods to
find the minimum spanning tree of a graph. The algorithm, similar to any
shortest path algorithm, begins from a vertex that is set as a root and walks
through all the vertices in the graph by determining the least cost adjacent
edges. To execute the prim's algorithm, the inputs taken by the algorithm
are the graph G {V, E}, where V is the set of vertices and E is the set of
edges, and the source vertex S. A minimum spanning tree of graph G is
obtained as an output.

Implementation:

16
17
Output:

18
Experiment – 09
Objective:

• Write a program to implement single source shortest path using


Dijkstra’s problem.

Theory:
Dijkstra’s algorithm is a popular algorithm for solving single-source
shortest path problems having non-negative edge weight in the graphs i.e.,
it is to find the shortest distance between two vertices on a graph. The
algorithm maintains a set of visited vertices and a set of unvisited vertices.
It starts at the source vertex and iteratively selects the unvisited vertex
with the smallest tentative distance from the source. It then visits the
neighbors of this vertex and updates their tentative distances if a shorter
path is found. This process continues until the destination vertex is
reached, or all reachable vertices have been visited.

Implementation:

19
20
Output:

21
Experiment – 10
Objective:

• Write a program to implement Floyd - Warshall algorithm.

Theory:
The Floyd-Warshall algorithm is a graph algorithm that is deployed to
find the shortest path between all the vertices present in a weighted graph.
This algorithm is different from other shortest path algorithms; to describe
it simply, this algorithm uses each vertex in the graph as a pivot to check
if it provides the shortest way to travel from one point to another.

Floyd-Warshall algorithm works on both directed and undirected


weighted graphs unless these graphs do not contain any negative cycles
in them. By negative cycles, it is meant that the sum of all the edges in the
graph must not lead to a negative number.

Implementation:

22
Output:

23

You might also like