Advertisement
Shailrshah

Bucket Sort

Oct 6th, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <stdio.h>
  2. void bucketSort(int a[], int max, int min, int n){
  3.     int count = 0;
  4.     int bucket[max - min];
  5.     for(int i = 0; i < max-min+1; i++) bucket[i] = -1;
  6.     for(int i = 0; i<n; i++) bucket[a[i]-min] = a[i];
  7.     for(int i = 0; i < max-min+1; i++)
  8.         if(bucket[i]!=-1)a[count++] = bucket[i];
  9. }
  10. int main(){
  11.     int n, max = 0, min = 0;
  12.     printf("How many elements to insert?: ");
  13.     scanf("%d",&n);
  14.     int a[n];
  15.     printf("Enter %d  elements.", n);
  16.     for(int i = 0; i < n; i++){
  17.         up: scanf("%d", &a[i]);
  18.         if(a[i] == -1){
  19.             printf("Enter elements that aren't -1");
  20.             goto up;
  21.         }
  22.         if(i==0 || max < a[i]) max = a[i];
  23.         if(i==0 || min > a[i]) min = a[i];
  24.     }
  25.     bucketSort(a, max, min, n);
  26.     for(int i = 0; i < n; i++)
  27.         printf("%d ", a[i]);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement