0% found this document useful (0 votes)
87 views8 pages

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

The document describes an algorithm to determine if two elements in a set S of N integers sum to a given integer X, running in Θ(NlogN) time. It first sorts the set using merge sort in Θ(NlogN) time. It then performs a two-way search in Θ(N) time to check if any pair sums to X. The overall time is Θ(NlogN) + Θ(N) = Θ(NlogN). The document also discusses a modification of merge sort that sorts sublists of length k using insertion sort, and shows the largest k allowing the same Θ(NlogN) runtime is Θ(

Uploaded by

Gurkirat Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views8 pages

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

The document describes an algorithm to determine if two elements in a set S of N integers sum to a given integer X, running in Θ(NlogN) time. It first sorts the set using merge sort in Θ(NlogN) time. It then performs a two-way search in Θ(N) time to check if any pair sums to X. The overall time is Θ(NlogN) + Θ(N) = Θ(NlogN). The document also discusses a modification of merge sort that sorts sublists of length k using insertion sort, and shows the largest k allowing the same Θ(NlogN) runtime is Θ(

Uploaded by

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

Question

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.
Every time we see lgn, we should think of divide-and-
conquer algorithms. It inherently means how many
times n can be divided by 2, that is repeated division
of n elements in two groups.

APPROACH Hence we use merge sort first to sort the set of n


: elements. Time complexity of merge sort is n*logn
hence we are in the specified range till now , now we
have to check whether the given element is present in
the set as sum of two distinct integers . Using a
technique known as two way search whose
complexity comes out to be theta of n lets move on to
the algorithm to explain in detail .
SUPPOSED ALGORITHM

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

• We have n elements divided into n/k sorted sublists each of length k. To merge


these n/k sorted sublists to get a single sorted list of length n, we have to take 2
sublists at a time and continue to merge them.
• then it will take log(n/k) steps to merge upto full n sized array and also each
level will have n comparisions therefore complexity will be theta of n*log(n/k).
• For the modified algorithm to have the same asymptotic running time as standard
merge sort,  Θ(nk + nlg(n/k)) must be same as Θ(n lg n)Θ
• To satisfy this condition, k cannot grow faster than lgn asymptotically, if it does
then because of the nk term, the algorithm will run at worse asymptotic time than 
Θ (n lg n).
• So, let’s assume, k = Θ(lgn) and see if we can meet the criteria …
Θ(nk +nlg(n/k)) = Θ(nk +nlgn-nlgk)
= Θ(nlgn +nlgn –nlg(lgn))
= Θ(2nlgn – nlgn(lgn))
= Θ(nlgn)
• Hence we can tell that k can have maximum value theta of logn . To satisfy
this condition, k cannot grow faster than lgn asymptotically, if it does then
because of the nk term, the algorithm will run at worse asymptotic time than 
Θ (n lg n).

You might also like