Awesome q2a theme
0 votes
20 views
#include<stdio.h>
void Bucket_Sort(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[100];   
 int num;   
 int i;  
 printf("Enter How many Numbers : ");    
 scanf("%d",&num);    
 printf("Enter the %d elements to be sorted:n",num);  
 for(i = 0; i < num; i++ )
 {   
  scanf("%d",&array[i]);  
 }   
 printf("nThe array of elements before sorting : n"); 
 for (i = 0;i < num;i++) 
 {    
  printf("%d ", array[i]);   
 }    
 printf("nThe array of elements after sorting : n");  
 Bucket_Sort(array, num);  
 for (i=0;i<num;i++) 
 {     
  printf("%d ", array[i]);  
 }   
 printf("n");      
 return 0; 
} 
 

Why in this bucket sort , getting runtime error?? Plz. check

in Programming by (247 points) | 20 views
+2
 (count[array[i]])++; 

let the array size is 5 ( so count array size also 5 ) and the array contain value 7.

what is the line returns ?

 

So, your program correct if the elements of the array < size of the array !

0
@Shaik

Is bucket sort algo ,same as hashing??
+1
As per my knowledge, No mam !

Bucket sort uses counting sort :)

but there is no relation between bucket sort and hashing.
0

@Shaik

(count[array[i]])++;

Can u suggest me some code replace this line, for more generalizing output.

0
I think you need to create an array of pointers (say bucket array)  having 10 elements (index 0 to 9). Now, you can do the following-

Each element (i.e. pointer) of bucket array will point to a linked list where you will add elements (of the array to be sorted) depending upon the digit at one’s place (which must match the index of the bucket) in ascending order.

Now, you can go through the buckets (elements of the bucket array) and insert the elements in the original array as you encounter them in the different linked list.  As a result, you will get a sorted array.

Please log in or register to answer this question.

Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
932 questions
596 answers
1,885 comments
81,474 users