0% found this document useful (0 votes)
67 views

DSA Assignemnt

The document contains an algorithm to convert a Roman numeral to a decimal number using a getValue function, and an algorithm called romanToInteger that iterates through the Roman numeral string and sums the values of each character. It also contains pseudocode for two sorting algorithms - selection sort with O(n^2) time complexity that finds the minimum element and swaps it into place iteratively, and merge sort with O(n log n) time complexity that divides the array, recursively sorts halves and merges them.

Uploaded by

Talha Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

DSA Assignemnt

The document contains an algorithm to convert a Roman numeral to a decimal number using a getValue function, and an algorithm called romanToInteger that iterates through the Roman numeral string and sums the values of each character. It also contains pseudocode for two sorting algorithms - selection sort with O(n^2) time complexity that finds the minimum element and swaps it into place iteratively, and merge sort with O(n log n) time complexity that divides the array, recursively sorts halves and merges them.

Uploaded by

Talha Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENT Data Structures and

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:

Letter Value Letter Value

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:

int getValue(char symbol)


{
if(symbol=='I' || symbol=='i')
return 1
if(symbol=='II' || symbol=='ii')
return 2
if(symbol=='III' || symbol=='iii')
return 3
if(symbol=='IV' || symbol=='iv')
return 4
if(symbol=='V' || symbol=='v')
return 5
if(symbol=='VI' || symbol=='vi')
return 6
if(symbol=='VII' || symbol=='vii')
return 7
if(symbol=='VIII' || symbol=='viii')
return 8
if(symbol=='IX' || symbol=='ix')
return 9
if(symbol=='X' || symbol=='x')
return 10
if(symbol=='XX' || symbol=='xx')
return 20
if(symbol=='XXX' || symbol=='xxx')
return 30
if(symbol=='XL' || symbol=='xl')
return 40
if(symbol=='L' || symbol=='l')
return 50
if(symbol=='XC' || symbol=='xc')
return 90
if(symbol=='C' || symbol=='c')
return 100
if(symbol=='CC' || symbol=='cc')
return 200
if(symbol=='CD' || symbol=='cd')
return 400
if(symbol=='D' || symbol=='d')
return 500
if(symbol=='M' || symbol=='m')
return 1000
return -1
}

int romanToInteger(string Roman, int size)


{
int resultInt = 0
for(i = 0 to size - 1, i = i+1)
{
int val = getValue(Roman[i])
if(i < size - 2)
{
int valNext = getValue(Roman[i+1])
if(val >= valNext)
resultInt = resultInt + val
else
{
int afterSub = valNext - val
resultInt = resultInt + afterSub
i=i+1
}
}
else
resultInt = resultInt + val
}
return resultInt
}

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

You might also like