Professional Documents
Culture Documents
Data Structures and Algorithms Assignment-3
Data Structures and Algorithms Assignment-3
Assignment
Make groups of four and answer the following questions accordingly.
Part – I Workout
1. Calculate both the time and space complexity of the following program. Show all the necessary
steps.
int main() {
int num, x, y, sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; i++) {
sum += i;
}
cout <<sum;
if(num%2 == 0){
x = num * num;
y = num + sum;
cout <<x;
cout <<y;
}
else {
x = num + num;
cout <<x;
}
return 0;
}
2. Given the following array, how does the binary search algorithm work to find element 16
form the array? Show all the necessary steps that binary search algorithm follows.
Array = {2, 3, 5, 6, 7, 10, 11, 12, 14, 15, 16, 18, 19, 20, 21}
3. Given the following unsorted array, how does the Selection, Insertion sort, Heap sort, Quick
sort and Merge sort algorithms work to sort elements. Show all the necessary steps that
selection and insertion sort algorithms follow.
Array = {6, 20, 5, 2, 7, 10, 1, 12, 4, 15, 8, 9, 19, 3, 13, 16}
4. Write the prefix and postfix form of the following infix expression.
(3+5) - (2*6) + ((4+8)/5) -9
5. Delete node 14 from the following binary tree and re-structure the tree.
Part – II Programming
1. Given the following linked list and write a program that