Machine-translated by Google. Questions are authored in English.
Top

Discussion

The best/worst case time complexity of Bubble sort is :

  • A.O(n)/ O(\(n^{2}\))
  • B.O(n)/ O(n log n)
  • C.O(n log n)/O(n log n)
  • D.O (n log n)/ O(\(n^{2}\) log n)

Answer: A

Bubble sort is O(n) in the best case and O(\(n^{2}\)) in the worst case.

Bubble sort with the usual 'swapped' flag stops as soon as a pass makes no swaps. On an already sorted array the first pass of n-1 comparisons makes no swaps, so the best case is O(n). On a reverse-sorted array every pass swaps, and about n(n-1)/2 comparisons and swaps are needed, so the worst case is O(\(n^{2}\)).

O(n log n) is the complexity of merge sort and of heap sort, not of bubble sort, which rules out options B, C and D.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.