Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- void bucketSort(int a[], int max, int min, int n){
- int count = 0;
- int bucket[max - min];
- for(int i = 0; i < max-min+1; i++) bucket[i] = -1;
- for(int i = 0; i<n; i++) bucket[a[i]-min] = a[i];
- for(int i = 0; i < max-min+1; i++)
- if(bucket[i]!=-1)a[count++] = bucket[i];
- }
- int main(){
- int n, max = 0, min = 0;
- printf("How many elements to insert?: ");
- scanf("%d",&n);
- int a[n];
- printf("Enter %d elements.", n);
- for(int i = 0; i < n; i++){
- up: scanf("%d", &a[i]);
- if(a[i] == -1){
- printf("Enter elements that aren't -1");
- goto up;
- }
- if(i==0 || max < a[i]) max = a[i];
- if(i==0 || min > a[i]) min = a[i];
- }
- bucketSort(a, max, min, n);
- for(int i = 0; i < n; i++)
- printf("%d ", a[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement