DSA Assignemnt
DSA Assignemnt
algorithm
Name: Syed Talha Ur Rehman ID: 12232
Question # 1
Write an algorithm in pseudo-code for converting a roman number into a decimal number (1
– 1000). Recall the following conversion:
I One XX Twenty
II Two XXX Thirty
III Three XL Forty
IV Four L Fifty
V Five XC Ninety
VI Six C One Hundred
VII Seven CC Two Hundred
VIII Eight CD Four Hundred
IX Nine D Five Hundred
X Ten M One Thousand
Pseudo-code:
Question # 2:
Write down algorithm in pseudo-code of any two sorting algorithms with different time
complexities. The algorithm can be of your choice
1. Selection Sort:
Time Complexity: O (n2)
Pseudo-code:
Begin
for i := 0 to size-2 do //find minimum from ith location to size
iMin := i;
for j:= i+1 to size – 1 do
if array[j] < array[iMin] then
iMin := j
done
swap array[i] with array[iMin].
done
End
2. Merge Sort:
Time Complexity: O (n log n) for all cases
Pseudo-code:
Begin
nLeft := m - left+1
nRight := right – m
define arrays leftArr and rightArr of size nLeft and nRight respectively
for i := 0 to nLeft do
leftArr[i] := array[left +1]
done
for j := 0 to nRight do
rightArr[j] := array[middle + j +1]
done
i := 0, j := 0, k := left
while i < nLeft AND j < nRight do
if leftArr[i] <= rightArr[j] then
array[k] = leftArr[i]
i := i+1
else
array[k] = rightArr[j]
j := j+1
k := k+1
done
while i < nLeft do
array[k] := leftArr[i]
i := i+1
k := k+1
done
while j < nRight do
array[k] := rightArr[j]
j := j+1
k := k+1
done
End