Describe Θ (N*Logn) -Time Algorithm That, Given A Set S Of N Integers And Another Integer X, Determines Whether Or Not There Exist Two Elements In S Whose Sum Is Exactly X
Describe Θ (N*Logn) -Time Algorithm That, Given A Set S Of N Integers And Another Integer X, Determines Whether Or Not There Exist Two Elements In S Whose Sum Is Exactly X
Sum-Search (S,x)
Merge-Sort(S,1,S.length) //sorts the set with time complexity of nlogn
left=1
right=S.length
while (left<right)
if S[left]+S[right]==x
return true
else if S[left]+S[right]<x
left=left+1
else
right=right−1
return false
As we can see the search part of the code has atmost n-1
comparisions which means in worst case the time complexity will
be theta of n hence total program complexity can be written as
nlogn+n which can be further simplified into n*logn hence we have
found a algorithm with order of theta n*logn time complexity to
find if the given number x is present in the set in form of sum of two
numbers.
question
• Consider a modification to merge sort in
which n/k sublists of length k are sorted using insertion
sort and then merged using the standard merging
mechanism, where k is a value to be determined.
1.Show that insertion sort can sort the n/k sublists, each of
length k, in Θ(n* lg(n/k)) worst-case time.
2.Show how to merge the sublists in Θ(n lg(n /k)) worst-
case time.
3.Given that the modified algorithm runs in Θ(n*k +n*
lg(n/k)) worst-case time, what is the largest value of k as a
function of n for which the modified algorithm has the
same running time as standard merge sort, in terms
of Θ notation?
• For input of size k, insertion sort runs on Θ(k^2) worst-case time. So, worst-
case time to sort n/k sublists, each of length k will be
• n|k *Θ(k^2) =Θ(n*k) hence proved