成人快手

Bucket sort

A bucket sort separates a list of into different collections of data, called 鈥榖uckets鈥. Empty buckets are set up at the start of the sort and are filled with the relevant data. Each bucket is then sorted, and the data is finally gathered back into a list.

Example

Imagine that you have a list of people who you want to sort by age, from youngest to oldest. A bucket sort can do this.

The list of ages is:

41, 15, 17, 32, 18, 28, 77 and 54

1. Set up a series of empty buckets.

First we set up a series of buckets into equal sub intervals such as 1-10, 11-20, 21-30.

2. Put the data into the correct buckets.

The next part of the bucket sort process is to place our input numbers into the relevant buckets. 17, 15 and 18 will go into our 11-20 bucket.

3. Buckets that have more than one item of data in them will be sorted (eg using bubble sort).

The final part of the bucket sort  process is to sort the numbers in each bucket and go through them in order.

4. The data will then be gathered from each bucket and put back into a list.

The final list will be 15, 17, 18, 28, 32, 41, 54, 77.