Академический Документы
Профессиональный Документы
Культура Документы
We can.
can
By using Bucket
Sort
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
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
.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
.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
.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
.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
.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
.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
.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
.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
.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
.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
.
.
.
.
.
.
.
.
.
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.