Lab Manual: C. K. Pithawalla College of Engineering and Technology
Lab Manual: C. K. Pithawalla College of Engineering and Technology
Lab Manual: C. K. Pithawalla College of Engineering and Technology
Lab Manual
Practical Assignment
Solutions
1.1 Bubble sort
BubbleSort (A, n)
//A – array of numbers
//n – number of elements
for i = 1 to n-1
for j = 1 to n-1
if A[j] > A[j+1]
temp = A[j]
A[j] = A[j+1]
A[j+1] = temp
return A
1.2 Selection sort
SelectionSort (A, n)
//A – array of numbers
//n – number of elements
for i = 1 to n-1
for j = i+1 to n-1
if A[j] > A[i]
temp = A[j]
A[j] = A[i]
A[i] = temp
return A
Insertion_Sort(A, n)
//A – array of numbers
//n – number of elements
for j=2 to n do
key = A[j]
i = j-1
While i>0 and A[i] > key
A[i+1] = A[i]
i = i-1
A[i+1] = key
return A
IterativeFactorial(n)
// n – nth factorial number
fact = 1
For I = 1 to n
fact = fact * i
return fact
RecursiveFactorial(n)
// n – nth factorial number
if n <= 1
return n
else
return RecursiveFactorial(n-1) * n
LinearSearch(A, n, key)
// A – Array of elements
// n – size of an array
// key – element to be searched
for i = 1 to n
if A[i] = key then
return i
return -1
IterativeBinSearch(A, n, key)
// Given Array – A[1:n] in increasing order, n>0,
// If key is present in A it returns p such that A[p] = key.
low=1;
high=n;
While( low ≤ high)
{
mid = (low + high)/2 ;
If (key < A[mid]) then
high= mid -1;
Else if If (key > A[mid]) then
low = mid + 1;
Else
Return mid;
}
Return 0;
RecursiveBinSearch(A, low, high, key)
// Given Array – A[low:high] in increasing order, 1 ≤ low ≤ high,
// If key is present in A it returns p such that A[p] = key.
{
mid = (low + high )/2;
If ( key = A[mid]) then
Return mid;
Else if (key < A[mid]) then
RBinSerach(A, low, mid – 1, key);
Else
RBinSerach(A, mid + 1, high, key);
}
2.3 Merge sort
MergeSort(low, high)
// Global Array – A[low:high] to be sorted.
If (low ≤ high) then
{
// Divide P into two SP
mid = (low + high)/2 ;
// Solve SP
MergeSort(low, mid);
MergeSort(mid+1, high);
// Combine The Solutions
Merge(low, mid, high);
}
Merge(low, mid, high)
// A[low:high] is global array containing 2 sorted subsets in A[low:mid] and
A[mid+1:high]
// Combine these 2 sets into a 1 set residing in a A[low:high].
// B[] is an auxiliary global array
h = low;
i = low;
j = mid + 1;
while(( h ≤ mid) and (j ≤ high)) do
if (A[h] ≤ A[j]) then
B[i] = A[h] ;
h = h+1;
else
B[i] = A[j] ;
j = j+1;
end While
if (h>mid) then
for k = j to high do
B[i] = A[k];
i=i+1;
else
for k = h to mid do
B[i] = A[k];
i=i+1;
end if
for k = low to high do
A[k] = B[k];
QuickSort(p, r)
// Sorts the elements A[p],…A[r] which resides in Global Array – A[1:n].
If (p < r) then
{
// Divide P into two SP
q = Partition(A, p, r+1)
// q is the position of the partitioning element
// Solve SP
QuickSort(p, q-1);
QuickSort(q+1, r);
}
Partition(A, l, r)
// Returns correct position of pivot
pivot = A[l]
i = l
j = r
While i < j
While A[i] < pivot && i <= r
i = i + 1
End While
While A[j] > pivot && j >= l
j = j – 1
End While
If (i < j ) then
Interchange(A, i, j)
End While
A[l] = A[j]
A[j] = pivot
Return j
Interchange(A, i, j)
// Swaps values of A[i] and A[j]
x = A[i]
A[i] = A[j]
A[j] = x
Heapify (A, i)
l ← left [i]
r ← right [i]
if l ≤ heap-size [A] and A[l] > A[i]
then largest ← l
else largest ← i
if r ≤ heap-size [A] and A[i] > A[largest]
then largest ← r
if largest ≠ i
then exchange A[i] ↔ A[largest]
Heapify (A, largest)
BuildHeap (A)
heap-size (A) ← length [A]
For i ← floor(length[A]/2) down to 1 do
Heapify (A, i)
Heapsort (A)
BuildHeap (A)
for i ← length (A) down to 2 do
exchange A[1] ↔ A[i]
heap-size [A] ← heap-size [A] - 1
Heapify (A, 1)
KnapsackDP(n, v, w)
For j = 0 to W
V[0, j] = 0
For i = 1 to n
For j = 1 to W
if (wi > j)
V[i, j] = V[i-1, j]
else
V[i, j] = max {V[i-1,j], vi + V[i-1, j-wi}
Return V[n, W]
MatrixChainMultiplocation(p, n)
// p – represents chain of matrices such that matrix Ai has
// the dimension p[i-1] x p[i]
// n – length of p i.e. Total matrices – (n-1)
For i = 1 to n
M[i][i] = 0
For L = 2 to n
For i = 1 to n-L+1
j = i+L-1
m[i][j] = ∞
For k = i to j-1
cost = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]
if (cost < m[i][j])
m[i][j] = cost
s[i][j] = k
Return m[1][n-1]
MakeChange(SetofCoins n)
// Making change for N units using coinages from d[1…n]
d[1…n] = [1,4,6]
for i=1 to n
C[i, 0] = 0
for i=1 to n
for j=1 to N
LongestCommonSequence(X, Y, m, n)
//X – sequence 1 of length m
//Y – sequence 2 of length n
For i = 0 to m
For j = 0 to n
if i=0 or j=0
L[i, j] = 0
else if X[i] = Y[j]
L[i, j] = 1 + L[i-1, j-1]
else
L[i, j] = max {L[i-1, j], L[i, j-1]}
Return L[m, n]
FractionalKanpsack(v,w,W,n)
// v[i] and w[i] represents Value and Weight of an Object i respectively
// W = Weight a knapsack can occupy
For i = 1 to n do
x[i] = 0
Weight = 0
For i = 1 to n do
ValueDensity[i] = v[i] / w[i]
T = Allocate_Memory(n)
IndirectSort(ValueDensity, T, n)
For i = 1 to n And Weight < W
If Weight + w[T[i]] ≤ W then
x[T[i]] = 1
Weight = Weight + w[T[i]]
Else
x[T[i]] = (W – Weight) / w[T[i]]
Weight = W
Return x
MST_Prim(G, w, r)
// G – Graph with vertices set V and edges set E
// w – weight function
// r – source vertex
for each u V[G]
Prof. Yogesh M Kapuriya 8|Page
9 Lab Manual – Algorithms
key[u] =
parent[u] = Ø
key[r] = 0
Q = InitQueue(V[G]) //Maintain set V as min-priority heap in queue
while Q ≠ Ø
u = ExtractMin(Q) // Extract Min
for each v Adj[u]
if v Q and w[u,v] < key[v]
parent[v] = u
key[v] = w[u,v] // Updates Queue
MST_Kruskal(G, w)
// G – Graph with vertices set V and edges set E
// w – weight function
A = Ø
for each vertex v V[G]
MakeSet(v)
sort the edges E into ascending order by weight w
for each edge (u, v) E, taken in ascending order by weight
if FindSet(u) ≠ FindSet(v)
A = A {(u, v)}
Union(u, v)
Breadth-First-Search (G, r)
// G – Graph of set V and E
// r – root vertex
DFS-Iterative(G, r)
// G – Graph of set V and E
// r – root vertex
let S be a stack
Prof. Yogesh M Kapuriya 9|Page
10 Lab Manual – Algorithms
S.push(r)
while S is not empty
v = S.pop()
if v is not labeled as discovered:
label v as discovered
for all edges from v to w in G.adjacentEdges(v) do
S.push(w)