DAA Lab File
DAA Lab File
Insertion sort:-> Insertion sort is a simple sorting algorithm that works similar
to the way you sort playing cards in your hands. The array is virtually split into a
sorted and an unsorted part. Values from the unsorted part are picked and placed
at the correct position in the sorted part.
CODE:->
def insertionSort(a): # Function to implement insertion sort
for i in range(1, len(a)):
temp = a[i]
# Move the elements greater than temp to one position
#ahead from their current position
j = i-1
while j >= 0 and temp < a[j] :
a[j + 1] = a[j]
j = j-1
a[j + 1] = temp
for i in range(len(a)):
print (a[i], end = " ")
2
PROGRAM:->(02)
AIM:-> Write a program to implement Selection Sort on an unsorted array.
Selection Sort:->
Selection sort is an effective and efficient sort algorithm based on comparison
operations. It adds one element in each iteration. You need to select the smallest
element in the array and move it to the beginning of the array by swapping with the
front element.
CODE:->
for i in range(len(a)):
print (a[i], end = " ")
a = [69, 14, 1, 50, 59]
print("Before sorting array elements are - ")
printArr(a)
selection(a)
print("\nAfter sorting array elements are - ")
selection(a)
printArr(a)
3
OUTPUT:->
4
PROGRAM:->(03)
AIM:-> Write a program to implement Quick Sort on an unsorted array.
Quick Sort:->
Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' element
from the array and partitioning the other elements into two sub-arrays, according to
whether they are less than or greater than the pivot.
CODE:->
#function that consider last element as pivot,
#place the pivot at its exact position, and place
#smaller elements to left of pivot and greater
#elements to right of pivot.
return (i + 1)
5
quick(a, p + 1, end)
OUTPUT:->
6
PROGRAM:->(04)
AIM:-> Write a program to implement Counting Sort on an unsorted array.
Counting Sort:->
Counting sort is an integer sorting algorithm used in computer science to collect
objects according to keys that are small positive integers.
It works by determining the positions of each key value in the output sequence by
counting the number of objects with distinct key values and applying prefix sum to
those counts.
CODE:->
# python program for counting sort
def countingSort(arr):
size = len(arr)
output = [0] * size
# place the elements in output array after finding the index of each element of
original array in count array
m = size - 1
while m >= 0:
output[count[arr[m]] - 1] = arr[m]
7
count[arr[m]] -= 1
m -= 1
data = [3,5,1,6,7,8,3]
countingSort(data)
print("Sorted Array: ")
print(data)
OUTPUT:->
8
PROGRAM:->(05)
AIM:-> Write a program to Fractional Knapsack Problem.
Fractional Knapsack Problem:->
In the fractional knapsack problem, we are given a set of items, each with a weight
and a value, and we want to find the most valuable subset of items that we can fit
into a knapsack with capacity W.
CODE:->
# A naive recursive implementation
# of 0-1 Knapsack Problem
# Base Case
if n == 0 or W == 0:
return 0
# Driver Code
if __name__ == '__main__':
profit = [60, 100, 120]
weight = [10, 20, 30]
W = 50
n = len(profit)
print knapSack(W, weight, profit, n)
OUTPUT:->
10
PROGRAM:->(06)
AIM:-> Write a program to 0-1 Knapsack Problem.
Knapsack Problem:->
Given both weights and profits of N items, we want to put these items in a
Knapsack which has a capacity C. The goal is to get the maximum profit from the
items in the Knapsack. Each item can only be selected once, as we don't have
multiple quantities of any item.
CODE:->
#Returns the maximum value that can be stored by the bag
def knapSack(W, wt, val, n):
# initial conditions
if n == 0 or W == 0 :
return 0
# If weight is higher than capacity then it is not included
if (wt[n-1] > W):
return knapSack(W, wt, val, n-1)
# return either nth item being included or not
else:
return max(val[n-1] + knapSack(W-wt[n-1], wt, val, n-1),
knapSack(W, wt, val, n-1))
# To test above function
val = [50,100,150,200]
wt = [8,16,32,40]
W = 64
n = len(val)
print (knapSack(W, wt, val, n))
OUTPUT:->
11
PROGRAM:->(07)
AIM:-> Write a program to implement Job Sequencing.
Job Sequencing:->
Job sequencing with deadlines is a problem that involves scheduling a set of jobs
to maximize profit while adhering to their respective deadlines. This approach
assumes that each job can be completed in exactly one unit of time. If jobs have
different durations, a more advanced scheduling algorithm might be necessary.
Also, if the deadlines are represented as relative time.
CODE:->
n = len(arr)
for i in range(n):
result = [False] * t
job = ['-1'] * t
for i in range(len(arr)):
if result[j] is False:
result[j] = True
12
job[j] = arr[i][0]
break
print(job)
OUTPUT:->
13
PROGRAM:->(08)
AIM:-> Write a program to implement Naive Based String Matching.
Naive Based String Matching:->
It is the simplest method among all to look for patterns in an input text. It checks
all the characters one by one in the given string of characters. It finds the exact
string matches – be it more or more exact occurrences of the pattern. It is more
used when there is small text.
CODE:->
def naive(txt,wrd):
for i in range(lt-lw+1):
j=0
while(j<lw):
if txt[i+j]==wrd[j]:
j+=1
else:
break
else:
print('found at position',i)
14
OUTPUT:->
15