Вы находитесь на странице: 1из 24

Bucket Sort

Why Bucket Sort?


Complexity:
Selection Sort, Bubble Sort, Insertion Sort: O(n2).
Heap sort and Merge sort: O( n log n ),
Quicksort : O( n log n) on average, O( n2 ) worst
case.
.

Can we sort in linear


time?

We can.
can
By using Bucket

Sort

Key Ideas of Bucket


Sort:
1) Bucketing : Set up an array of initially empty
"buckets".
2) Scattering : Go over the original array, putting
each object in its bucket.
3) Sorting: Sort each non-empty bucket (by another
algorithm or bucket sort recursively)
4) Gathering: Visit the buckets in order and put all
elements back into the original array.

Bucket Sort Algorithm

1.
2.
3.
4.
5.
6.
7.
8.
9.

Bucket-Sort(A)
Let B[0.n-1] be a new array
n = length[A]
for i = 0 to n-1
make B[i] an empty list
for i = 1 to n
do insert A[i] into list B[ n A[i] ]
for i = 0 to n-1
do sort list B[i] with Insertion-Sort
Concatenate lists B[0], B[1],,B[n-1]
together in order

We have an array A. We will perform Bucket Sort on i

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
7

10

Bucketing
n=10, i=1

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

10

Scattering:1

n=10, i=1

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X..87 ]
=B[ 8.7 ]=B[8]

10

Scattering:2
n=10, i=2

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X..27 ]
=B[ 2.7 ]=B[2]

.87

.27

10

Scattering:3
n=10, i=3

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X.49 ]=B[ 4.9 ]


=B[4]

.27

.87

.49

10

Scattering:4

n=10, i=4

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X .36 ]=B[ 3.6 ]


=B[3]

.27

.36

.49

.87

10

Scattering: 5

n=10, i=5

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X .82 ]


=B[ 8.2]=B[8]

.82
.27

.36

.49

.87

10

Scattering: 6
n=10, i=6

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X 1.4 ]


=B[ 10.4]=B[10]

.82
.27

.36

.49

1.4

.87

10

Scattering:7

n=10, i=7

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X .31 ]


=B[ 3.1]=B[3]

.31
.27

.82

.36

.49

.31

.87

1.4

10

Scattering:8
n=10, i=8

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X .22 ]


=B[ 2.2]=B[2]

.22

.31

.27

.36

.49

.82

.31

.87

1.4

10

Scattering:9

n=10, i=9

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X .33 ]


=B[ 3.3]=B[3]

.33

.82

.22

.31

.27

.36

.49

.31

.87

1.4

10

Scattering:10
n=10, i=9

A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

B[ n A[i] ] = B[ 10X .78 ]


=B[ 7.8]=B[7]

.33

.82

.22

.31

.27

.36

.49

.78

.87

1.4

10

Sorting
A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

Apply insertion sort on each bucket

.33

.82

.22

.31

.27

.36

.49

.78

.87

1.4

10

Sorting
A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
8

10

Buckets are sorted from bottom to top by


ascending order :
.36

.87

.27

.33

.22

.31

.49

.78

.82

1.4

s10

Gathering
A .

.
.
.
.
1.
87 27 49 36 82 4
1

.
.
.
.
31 22 33 78
7

10

Concatenate the buckets in


order

.
.
.
.
.
.
.
.
.
1.
Y 22 27 31 33 36 49 78 82 87 4
1

2
.36

10

Sorted output
.87

.27

.33

.22

.31

.49

.78

.82

1.4

Best case
In best case every element
belongs to different buckets so
the complexity is O(n+k).
O(n+k)
n=number
of
elements,
k=number of buckets.

Worst case
In worst case complexity is O(n^2), as
every element belongs to one bucket
so we have to use insertion sort on n
elements.
Instead of insertion sort we could use
merge sort or heap sort because their
worst case running time is O( n log n ).
But we use insertion sort because we
expect the buckets to be small and
insertion sort works much faster for
small array.

Applications
Nearest- neighbour search in particle
based simulation.
For median filtering of images.

Вам также может понравиться