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

Assignment-1

The document contains solutions to four exercises related to algorithms and their complexities. Exercise 1 derives a recurrence relation for T(n) showing that T(n) = nlogn for powers of 2. Exercises 2, 3, and 4 discuss the worst-case running time of recursive insertion sort, an in-place rearrangement algorithm, and the average number of comparisons in binary search, respectively.

Uploaded by

chihuahua522861
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)
8 views

Assignment-1

The document contains solutions to four exercises related to algorithms and their complexities. Exercise 1 derives a recurrence relation for T(n) showing that T(n) = nlogn for powers of 2. Exercises 2, 3, and 4 discuss the worst-case running time of recursive insertion sort, an in-place rearrangement algorithm, and the average number of comparisons in binary search, respectively.

Uploaded by

chihuahua522861
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/ 1

Student: Nguyen Nhu Hoang Phuong

Troy ID: 1607900

Assignment 1
Exercise 1:
With n = 2 => T(2) = 2 = 2log2 (1)
Assume that there exists a k>1, such that T(2k) = 2klog2k
Consider: T(2k+1) = 2T(2k+1/2) + 2k+1
= 2T(2k.2/2) + 2k.2
= 2T(2k) + 2k.2
= 2. 2k.log2k + 2k.2
= 2k.2.(log2k + 1)
= 2k+1(log2k + log2)
= 2k+1log2k+1 (2)
From (1) and (2) => When n is an exact power of 2, the solution of the recurrent is T(n) = nlogn.

Exercise 2:
There are 2 steps in this recursive sorting algorithm:
- Step 1: Recursive sorting of A[1….n-1]
 The worst-case running time for this step is T(n-1)
- Step 2: Inserting A[n] into the sorted array A[1…n-1]
 A[n] need to be compared with all elements in the sub-array in the worst time before
finding its correct position.
 This insertion step takes O(n) time in the worst case.
 A recurrence for the worst-case running time of this recursive version of insertion sort is T(n) = T(n-1) +
O(n).

Exercise 3:
RearrangeArray(A)
left = 0
right = A.length – 1
while left <= right
while A[left] < 0
left = left + 1
while A[right] >= 0
right = right – 1
if left <= right
temp = A[left]
A[left] = A[right]
A[right] = temp
 This algorithm has a time complexity of O(n) and does not require any extra space

Exercise 4:
To calculate the average number of comparisons in binary search, we can use the concept of the binary search
tree.
With the array that has the size n, a binary search tree has a height of log2n.
 Each level of the binary search tree = one comparison.
 The average number of comparisons in binary search is the average height of the binary
search tree.
 The average number of comparisons in binary search in a successful search is log2n..

You might also like