简体   繁体   中英

How is the time complexity of Bucket Sort O(n+k) if it uses insertion sort to sort each bucket?

Since time complexity of insertion sort is O(n^2), how is the average case time complexity of bucket sort O(n+k) as it uses insertion sort at each bucket? Here k is the number of buckets.

You can have a look at thedetailed calculations of the average time-complexity. Although, here is an intuition.

First, in the worst-case, bucket sort is O(n^2) . This happens whenever all elements end up in the same buckets.

Although, bucket sort relies on elements being uniformly distributed across buckets. Given that assumption, and given a number of bucket proportional to the size of the input, then the average bucket should contain O(1) elements. In other words, by choosing enough buckets, you keep their size small.

This means the sorting of buckets has no effect toward the overall time-complexity. Choosing insertion sort then becomes a sensible choice as it has smaller overhead and requires no extra space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM