From Algorithm wiki
Related content:
|
|
|
[edit] Pseudocode
BucketSort(InputArray)
n <-- length[InputArray]
for i <-- 1 to n
do insert InputArray[i] in the list OutputArray[n * InputArray[i]]
for i <-- 0 to n - 1
do sort the list OutputArray[i] with e.g. Quicksort, Insertionsort, etc.
concat the lists OutputArray[0], OutputArray[1], ..., OutputArray[n - 1]
[edit] C# Code
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter array size");
int size = int.Parse(Console.ReadLine());
int[] a = new int[size];
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine("Enter element");
a[i] = int.Parse(Console.ReadLine());
}
BucketSort(a);
}
public static void BucketSort(int[] a)
{
int m = a.Length + 1;
int[] buckets = new int[m];
for (int j = 0; j < m; ++j)
buckets[j] = 0;
for (int i = 0; i < a.Length; ++i)
++buckets[a[i]];
for (int i = 0, j = 0; j < m; ++j)
for (int k = buckets[j]; k > 0; --k)
a[i++] = j;
for (int i = 0; i < a.Length; i++)
Console.Write(a[i] + " ");
}
}
[edit] Java Code
public class BucketSort {
public static void main(String[] args) {
int a[] = new int[]{2,5,7,1,8,5,2,9};
bucketSort(a);
}
public static void bucketSort(int a[]){
int m = a.length;
int[] buckets = new int[m];
for(int j = 0; j < m; j++)
buckets[j] = 0;
for(int i = 0; i < a.length; i++)
buckets[a[i]]++;
for(int i = 0, j=0; j < m; j++)
for(int k = buckets[j]; k > 0; k--)
a[i++] = j;
for(int i = 0; i < a.length; i++)
System.out.print(" " + a[i]);
}
}
[edit] C Code
#include <stdio.h>
void bucketSort(int array[], int n) {
int i, j;
int count[n];
for(i=0; i < n; i++) {
count[i] = 0;
}
for(i=0; i < n; i++) {
(count[array[i]])++;
}
for(i=0,j=0; i < n; i++) {
for(; count[i]>0; (count[i])--) {
array[j++] = i;
}
}
}
int main() {
int array[] = {1,3,4,6,4,2,9,1,2,9};
int n = 10;
int i;
for (i = 0;i < n;i++) {
printf("%d ", array[i]);
}
printf("\n");
bucketSort(array, n);
for (i = 0;i < n;i++) {
printf("%d ", array[i]);
}
printf("\n");
return 0;
}
[edit] C++ Code
#include <iostream>
using namespace std;
void bucketSort(int a[],int n, int max)
{
int* buckets = new int[max];
int SIZE = n;
for(int j = 0 ; j <= max ; j++)
buckets[j] = 0;
for(int i = 0 ; i < SIZE ; i++)
++buckets[a[i]];
for(int i = 0 , j = 0 ; j <= max ; ++j)
for(int k = buckets[j] ; k > 0 ; k--)
a[i++] = j;
for(int i = 0 ; i < SIZE ; i++)
cout << a[i] << " ";
cout << "\n";
}
int main()
{
int a[] = {25,54,73,11,83,52,23,91};
int elem = sizeof(a)/sizeof(int);
int max = a[0];
for(int i = 0 ; i < elem ; i++)
if(a[i] > max)
max = a[i];
bucketSort(a, elem, max);
system("pause>nul");
}
[edit] PHP Code
<?php
function bucketSort($arr) {
$count = array_count_values($arr);
ksort($count);
$arr = array();
foreach ($count as $nr => $amount) {
for ($i=0; $i < $amount;$i++) {
array_push($arr, $nr);
}
}
return $arr;
}
$arr = array(1,3,4,6,4,2,9,1,2,9);
echo implode(" ", $arr), "\n";
$arr = bucketSort($arr);
echo implode(" ", $arr), "\n";
?>
[edit] VB Code
Public Function BucketSort(ByVal Buckets As Integer, ByVal z() As Integer) As String 'Iterativ
'Für das Array z() muss gelten 0<=z(i)<=Bucktes
'Buckets sind die benötigten "Eimer" - in dem Falle Auslagerungen, die Anzahl der Buckets muss also
'laut Definition um sinnvoll zu arbeiten größer oder gleich der größten Zahl sein.
'Sinnvoller Einsatz: sortieren von Telefonnummern, PLZ, BLZ, Kontonummern
'Start der Zeitmessung
Dim start As DateTime
Dim ende As DateTime
start = DateTime.Now
'Histogramm erstellen
Dim n As Integer = z.Length
Dim k(Buckets) As Integer
Dim a As Integer
For a = 0 To Buckets - 1 'alles auf 0 setzen
k(a) = 0
Next a
For a = 0 To n - 1
k(z(a)) = k(z(a)) + 1
Next a
'sortieren
Dim x As Integer = 0
For a = 0 To Buckets - 1
While k(a) > 0
z(x) = a
x = x + 1
k(a) = k(a) - 1
End While
Next a
'Ende der Zeitmessung
ende = DateTime.Now
'umrechnen in MilliSekunden und zurückgeben
BucketSort = (ende.Hour * 60 * 60 * 1000) + (ende.Minute * 60 * 1000) + (ende.Second * 1000) + ende.Millisecond
BucketSort = BucketSort - ((start.Hour * 60 * 60 * 1000) + (start.Minute * 60 * 1000) + (start.Second * 1000) + start.Millisecond)
End Function
[edit] Perl Code
This section needs your Code!
[edit] Prolog Code
This section needs your Code!
[edit] Python Code
def stel(n,m):
a= (n/(10**(m-1)))%10
return a
liste = [5,678768,65546,2,34,999,1,34,77,3435,765,1234,65467,6,45,65]
def maxstelle(liste):
a = max(liste)
n= 1
while stel(a,n) != 0:
n += 1
return n-1
def bucketsort(liste):
a = 1
while a != maxstelle(liste):
l=[[] for i in range(10)]
for x in liste:
l[stel(x,a)].append(x)
liste = sum(l,[])
a +=1
return liste
[edit] Ruby Code
def bucket_sort(array)
bucket = []
array.each do |i|
bucket[i] ||= 0
bucket[i] += 1
end
sorted = []
bucket.size.times do |i|
next unless bucket[i]
bucket[i].times do
sorted << i
end
end
sorted
end
[edit] Delphi
procedure BuckSort;
var
Index : LongInt;
BuckIndex : LongInt;
begin
for BuckIndex := 0 to 255 do Bucket[BuckIndex] := 0;
for Index := 1 to TableSize do
Inc(Bucket[Table[Index]]);
Index := 1;
BuckIndex := 0;
while Index <= TableSize do
begin
while Bucket[BuckIndex] = 0 do
Inc(BuckIndex);
Table[Index] := BuckIndex;
Dec(Bucket[BuckIndex]);
Inc(Index);
end;
end;