gagan
gagan

Reputation: 11

worst case time complexity

An unsorted list of n numbers, find any two numbers among the list having minimum difference. If i'll have to write an algorithm for this, with worst case-time O(nlogn). Can the following algorithm work:

  1. sort list using merge sort
  2. traverse the whole list once, to find difference between consecutive numbers.
  3. return numbers having minimum difference.

Is the time complexity for such algorithm will be: O(nlogn + n) which I can say O(nlogn)?

Upvotes: 1

Views: 237

Answers (1)

jbsu32
jbsu32

Reputation: 1064

Yes. O(nlogn + n) is equivalent to O(nlogn)

Upvotes: 1

Related Questions