Awesome q2a theme
0 votes
25 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 (641 points) | 25 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.

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
Top Users Jan 2020
  1. shashin

    1416 Points

  2. Vimal Patel

    377 Points

  3. Deepakk Poonia (Dee)

    350 Points

  4. Debapaul

    276 Points

  5. Satbir

    196 Points

  6. Pratyush Priyam Kuan

    160 Points

  7. tp21

    151 Points

  8. SuvasishDutta

    151 Points

  9. pranay562

    145 Points

  10. DukeThunders

    97 Points

Monthly Top User and those within 60% of his/her points will get a share of monthly revenue of GO subject to a minimum payout of Rs. 500. Current monthly budget for Top Users is Rs. 75.
3,138 questions
1,550 answers
9,401 comments
89,835 users