You are on page 1of 38

ANALYSIS OF DESIGN ALGORITHM BTIT-305

SHRI VAISHNAV VIDHYAPEETH


VISHWAVIDYALAYA

Department of Computer Science and


Engineering

PRACTICAL LAB-FILE
Subject Name:-ANALYSIS AND DESIGN OF
ALGORITHMS.
Subject Code:-BTIT-305.
Class:-B-Tech CSE 2NDYEAR 3RD
ENROLLMENT NO.: 20100BTCSES07307
Section:- f red hat

Submitted To:- POOJA JAIN MAM


Submitted By:- sudit patel

PREPAIRED BY: SUDIT PATEL P a g e 1 | 38


ANALYSIS OF DESIGN ALGORITHM BTIT-305

Index
Practical Program Code No.

1 To implement binary search algorithm and compare 1


its running time in searching an element in arrays of
different sizes.

2 To implement Sum of Array algorithm Recursively 2


and compare its running time.

3 To implement Matrix Addition algorithm and 3


compare its running time.

4 To implement Matrix Multiplication algorithm and 4


compare its running time.

5 To implement Linear search algorithm and compare 5


its running time in searching an element in arrays of
different sizes.

6 To implement binary search algorithm in arrays of 6


different sizes.

7 To implement binary search algorithm Recursively in 7


arrays of different sizes.

8 To implement Modified binary search algorithm in 8


arrays of different sizes.

9 To implement Merge sort algorithm on the given list 9


of keys.

PREPAIRED BY: SUDIT PATEL P a g e 2 | 38


ANALYSIS OF DESIGN ALGORITHM BTIT-305

10 To implement Max-Min algorithm on the given list of 10


keys.

11 To implement Quick sort algorithm on the given list 11


of keys. (Hoare Partition)

12 To implement Quick sort algorithm on the given list 12


of keys. (Lomuto Partition)

13 To implement Heap sort algorithm on the given list 13


of keys

14 To implement insertion sort algorithm for the given 14


keys.

15 To implement selection sort algorithm for the given 15


keys.

16 To implement greedy knapsack methods by prim’s 16


and krushkal’s algorithm.

17 To implement Floyd’s Warshall’s Algorithm. 17

18 To implement Strassen’s Matrix Multiplication. 18

19 To implement N-Queen’s Problem. 19

PREPAIRED BY: SUDIT PATEL P a g e 3 | 38


ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 1
Objective: To implement binary search algorithm and compare its running time in searching
an element in arrays of different sizes.

Description:-
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.

Program:-

Output:-

PREPAIRED BY: SUDIT PATEL P a g e 4 | 38


ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 2
Objective:- To implement Sum of Array algorithm Recursively and compare its running time.

Description:- Given an array A[], we need to find the sum of its elements using Tail Recursion
Method. We generally want to achieve tail recursion (a recursive function where recursive
call is the last thing that function does) so that compilers can optimize the code. Basically,
if recursive call is the last statement, the compiler does not need to save the state of parent
call.

Program:-

Output:-

PREPAIRED BY: SUDIT PATEL P a g e 5 | 38


ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 3
Objective:- To implement Matrix Addition algorithm and compare its running time.

Description:- Matrix addition in C language to add two matrices, i.e., compute their sum and
print it. A user inputs their orders (number of rows and columns) and the matrices.

Program:-

Output:-
PREPAIRED BY: SUDIT PATEL P a g e 6 | 38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 4

Objective:- To implement Matrix Multiplication algorithm and compare its running time.

Description:- Matrix Multiplication in C language to multiply two matrices, i.e., compute


their multiplication and print it. A user inputs their orders (number of rows and columns)
and the matrices.

Program:-

PREPAIRED BY: SUDIT PATEL P a g e 7 | 38


ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

PREPAIRED BY: SUDIT PATEL P a g e 8 | 38


ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 5
Objective:-To implement Linear search algorithm and compare its running time in searching
an element in arrays of different sizes in c.

Description:- Linear search in C to find whether a number is present in an array. If it's


present, then at what location it occurs. It is also known as a sequential search. It is
straightforward and works as follows: we compare each element with the element to search
until we find it or the list ends. Linear search for multiple occurrences and using a function.
Program:-

Output:-
PREPAIRED BY: SUDIT PATEL P a g e 9 | 38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 6
Objective:- To implement binary search algorithm in arrays of different sizes.
Description:-
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.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 10 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Experiment 7
Objective:- To implement binary search algorithm Recursively in arrays of different sizes.

Description:- 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.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 11 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Experiment 8
Objective:- To implement Modified binary search algorithm in arrays of different sizes.
Description:- It is basically performing a binary search on a list which first increases then
decreases. Even though it's obvious that we can achieve O (log n) I couldn't figure out what
is wrong the code below that I've written:-
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 12 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Experiment 9
Objective:-To implement Merge sort algorithm on the given list of keys.
Description:- Merge sort is similar to the quick sort algorithm as it uses the divide and
conquer approach to sort the elements. It is one of the most popular and efficient sorting
algorithm. It divides the given list into two equal halves, calls itself for the two halves and
then merges the two sorted halves. We have to define the merge() function to perform the
merging.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 13 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

PREPAIRED BY: SUDIT PATEL P a g e 14 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

PREPAIRED BY: SUDIT PATEL P a g e 15 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Experiment 10
Objective:- To implement Max-Min algorithm on the given list of keys.
Description:- C program to find the maximum and minimum element in an array – In this
article, we will brief in on the many ways to find the maximum and minimum element in an
array in C programming.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 16 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Experiment 11
Objective:-To implement Quick sort algorithm on the given list of Keys (Hoare Partition).
Description:- Hoare uses two indices that start at the ends of the array being partitioned,
then move toward each other until they detect an inversion: a pair of elements, one greater
than the pivot, one smaller, in the wrong order relative to each other. The inverted
elements are then swapped. When the indices meet, the algorithm stops and returns the
final index.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 17 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Experiment 12
Objective:- To implement Quick sort algorithm on the given list of keys (Lomuto Partition).
Description:-
Program:-
Output:-

PREPAIRED BY: SUDIT PATEL P a g e 18 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

PREPAIRED BY: SUDIT PATEL P a g e 19 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 13
Objective:- To implement Heap sort algorithm on the given list of keys.
Description:- Heap Sort is a popular and efficient sorting algorithm in computer
programming. Learning how to write the heap sort algorithm requires knowledge of two
types of data structures - arrays and trees.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 20 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

PREPAIRED BY: SUDIT PATEL P a g e 21 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 14
Objective:- To implement Insertion sort algorithm on the given list of keys.
Description:- Insertion sort works similar to the sorting of playing cards in hands. It is
assumed that the first card is already sorted in the card game, and then we select an
unsorted card. If the selected unsorted card is greater than the first card, it will be placed at
the right side; otherwise, it will be placed at the left side.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 22 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Experiment 15
Objective:- To implement Selection sort algorithm on the given list of keys.
Description:- In selection sort, the smallest value among the unsorted elements of the array
is selected in every pass and inserted to its appropriate position into the array. It is also the
simplest algorithm. It is an in-place comparison sorting algorithm. In this algorithm, the
array is divided into two parts, first is sorted part, and another one is the unsorted part.
Initially, the sorted part of the array is empty, and unsorted part is the given array. Sorted
part is placed at the left, while the unsorted part is placed at the right.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 23 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Experiment 16

Objective:-To implement greedy knapsack methods by prim’s and krushkal’s algorithm.

Description:-
The knapsack problem is a problem in combinatorial optimization: Given a set of items,
each with a weight and a value, determine the number of each item to include in a
collection so that the total weight is less than or equal to a given limit and the total value is
as large as possible.

Prim’s Algorithm:-
Prim's algorithm is a minimum spanning tree algorithm that takes a graph as input and
finds the subset of the edges of that graph which. form a tree that includes every vertex. has
the minimum sum of weights among all the trees that can be formed from the graph.

Krushkal’s Algorithm:-
Krushkal's algorithm finds a minimum spanning forest of an undirected edge-weighted
graph. ... It is a greedy algorithm in graph theory as in each step it adds the next lowest-
weight edge that will not form a cycle to the minimum spanning forest.

PREPAIRED BY: SUDIT PATEL P a g e 24 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Program:-
Prim’s Algorithm Program:-

PREPAIRED BY: SUDIT PATEL P a g e 25 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

PREPAIRED BY: SUDIT PATEL P a g e 26 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

Krushkal’s Algorithm Program:-

PREPAIRED BY: SUDIT PATEL P a g e 27 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

PREPAIRED BY: SUDIT PATEL P a g e 28 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

PREPAIRED BY: SUDIT PATEL P a g e 29 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

PREPAIRED BY: SUDIT PATEL P a g e 30 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 17
Objective:-To implement Floyd’s Warshall’s Algorithm.
Description:-
Floyd–Warshall algorithm is an algorithm for finding shortest paths in a directed weighted
graph with positive or negative edge weights. A single execution of the algorithm will find
the lengths of shortest paths between all pairs of vertices.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 31 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

PREPAIRED BY: SUDIT PATEL P a g e 32 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 18
Objective:-To implement Strassen’s Matrix Multiplication.
Description:-
Strassen's Algorithm is an algorithm for matrix multiplication. Strassen’s Algorithm is a
recursive method for matrix multiplication where we divide the matrix into 4 sub-matrices
of dimensions n/2 x n/2 in each recursive step. For example, consider two 4 x 4 matrices A
and B that we need to multiply.
Program:-

PREPAIRED BY: SUDIT PATEL P a g e 33 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

PREPAIRED BY: SUDIT PATEL P a g e 34 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

PREPAIRED BY: SUDIT PATEL P a g e 35 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Experiment 19
Objective:-To implement N-Queen’s Problem.
Description:-
N – Queen’s Problem is to place n - queens in such a manner on an n x n chessboard that no
queens attack each other by being in the same row, column or diagonal. It can be seen that
for n =1, the problem has a trivial solution, and no solution exists for n =2 and n =3. So first
we will consider the 4 queens problem and then generate it to N – Queen’s problem.

Program:-

PREPAIRED BY: SUDIT PATEL P a g e 36 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

PREPAIRED BY: SUDIT PATEL P a g e 37 |


38
ANALYSIS OF DESIGN ALGORITHM BTIT-305

Output:-

PREPAIRED BY: SUDIT PATEL P a g e 38 |


38

You might also like