Bucket Sort
Bucket Sort
Bucket Sort
Aim: Consider John has recorded weight of 10,000 people and wants to
arrange them in descending order. Write a program to implement
the situation using Bucket Sort.
Algorithm:
bucketSort()
create N buckets each of which can hold a range of values
for all the buckets
initialize each bucket with 0 values
for all the buckets
put elements into buckets matching the range
for all the buckets
sort elements in each bucket
gather elements from each bucketend bucketSort
Code:
#include <stdio.h>
#include <stdlib.h>
// Move elements of arr[0..i-1], that are greater than key, to one position ahead
of their current position
while (j >= 0 && arr[j] < key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
int main() {
int n = 10000; // Number of people
int weights[n];
// Generate random weights for the people (you can replace this with your own
data)
for (int i = 0; i < n; i++) {
weights[i] = rand() % 1000; // Generating random weights between 0 and 999
}
return 0;
}