0% found this document useful (0 votes)
109 views5 pages

Analysis of Algorithms (CS-318) Assignment: Instructions

The document provides instructions for an analysis of algorithms assignment. It includes 5 questions asking the student to determine time complexities of algorithms and functions in terms of Big-O notation, identify possible pivot elements in a quicksort partition, calculate space complexity of a tree traversal, and analyze the time complexity of insertion sort on a partially sorted array. The student is asked to provide logical explanations and calculations to support their answers.

Uploaded by

Hina jamil
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)
109 views5 pages

Analysis of Algorithms (CS-318) Assignment: Instructions

The document provides instructions for an analysis of algorithms assignment. It includes 5 questions asking the student to determine time complexities of algorithms and functions in terms of Big-O notation, identify possible pivot elements in a quicksort partition, calculate space complexity of a tree traversal, and analyze the time complexity of insertion sort on a partially sorted array. The student is asked to provide logical explanations and calculations to support their answers.

Uploaded by

Hina jamil
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/ 5

Analysis of Algorithms (CS-318)

Assignment
Instructions:
1. I don’t like stories so please stay focussed and give logics to support your theory
2. Anything without logic is illogical, hence it will carry zero marks.
3. Try to put your answers in this document. If you are doing it on paper, attach a picture of
your answer under the respective question.

Name: _Bisma Ali

Roll No: 181370150

Question 1: (4 Marks)

Find out worst-case complexity in terms of Big-Oh for the following function. [Note:
here n is always greater than 10]

void my_function(int n){ O(1)


for(int i = 1; i < n; i = i * 2){ O(log n)
for(int j = n; j > 1; j--){ O(n)
cout<<”print something”<<endl; O(1)
if(j == n-3){ O(1)
j = 3; O(1)
}
}
}
}

O(log n ) Outer loop complexity , O(n) Inner loop complexity

T(n) = O(n log n) + O(4)

Time Complexity :- O(n log n)

Question 2: (8 Marks)

Find Big-Oh for the following relations.


1. T(n) = 4T(n/2) + n (using substitution method)
2. T(n) = T(n/4) + T(n/2) + n 3
(using recursion tree method)
Question 3: (2 Marks)

Here is an array which has been partitioned by the first step of quicksort. Which of
these elements could be the pivot? (There may be more than one possibility)

4 2 7 10 9 15 36 26 27 21 46 17 19 50 58

7,15,50,58 could be the pivot of this given array.

Question 4: (2 Marks)

Every node in a tree has 4 children except leaf nodes and the depth of the tree is 8.
Each node occupies 20 bytes of memory, How much maximum memory BFS will
occupy to traverse the whole tree.

To find Space Complexity in BFS use O(bd ).


So space complexity of tree is: O(bd) = O(48)
To find maximum memory :
=(O(40)+ O(41)+ O(42)+ O(43)+ O(44)+ O(45)+ O(46)+ O(47)+ O(48)) * 20
=(1+4+16+64+256+ 1024+4096+16384+65536)*20
=87381*20= 1747620

Question 5: (4 Marks)

You have an array of n elements. The first half of its elements are already sorted.
Compute the worst-case complexity of implementing insertion sort on the provided
array. Prove your answer with mathematical calculations.

Insertion sort uses comparison complexity of insertion sort.

No. of elements | No. of comparisons


1 | 0
2 | 1
3 | 2
4 | 3
……. |
Nth element | n-1

After comparisons array will be sorted. It makes arithmetic


series. 1+2+3+4+. .n = n(n+1)/2
Big oh will be : O(n2)
In this case if we have half array is sorted and half array is
unsorted. e.g. 1 3 5 6 2 7 9 4
> <> <
Sorted
Unsorted
Then our times complexity will be half in wort case. Worst case Time complexity will
be as:(n(n-1)/2)/2
After dropping constants worst case complexity of this array will be O(n2)

You might also like