C Program to implement bucket sort

Write C program to implement bucket sort.
The idea of Bucket Sort is to divide the interval [0, 1] into n equal-sized sub intervals, or buckets, and then distribute the n input numbers into the buckets. To produce the output, we simply sort the numbers in each bucket and then go through the buckets in order, listing elements in each.
Bucket sort runs in linear time when the input is drawn from a uniform distribution. Read more about C Programming Language .
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#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 < n;i++) 
 {     
  printf("%d ", array[i]);  
 }   
 printf("n");      
 return 0; 
} 

Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link
Or if you can’t find the program you are looking for, you can contact info@c-program-example.com. Thank you for visiting.

(c) www.c-program-example.com

C program to sort a string

C Strings:
Write a C program to sort a string.
In this program we sort the string using bubble sort technique.
Bubble Sort is the simplest and easiest sorting technique. In this technique, the two successive items A[i] and A[i+1] are exchanged whenever A[i]>=A[i+1]. The larger values sink to the bottom of the array and hence it is called sinking sort. The end of each pass smaller values gradually “bubble” their way upward to the top(like air bubbles moving to surface of water) and hence called bubble sort.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<string.h>
int main(){
    int i,j,n;
    char str[50],temp[50];
    printf("Enter a String:nn");
    scanf("%s",str);
    n=strlen(str);
    for(i=0;i<=n;i++)
    for(j=i+1;j<=n;j++){
        if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
        }
    }
    printf("The sorted string is:%sn",str);
    return 0;
}
Read more Similar C Programs
Sorting in C
C Strings
C Aptitude

You can easily select the code by double clicking on the code area above.
To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to implement address calculation sort.

Write a C Program to implement address calculation sort.
Address calculation sort is the sorting method which sorts the given array by using insertion method.
In this algorithm, a hash function is used and applied to the each key. Result of hash function is placed in the linked lists. The hash function must be a order preserving function which places the elements in linked lists are called as sub files. An item is placed into a sub-file in correct sequence by using any sorting method. After all the elements are placed into subfiles, the list is concatenated to produce the sorted list.
 Address calculation sort is used to efficiently store non-contiguous keys (account numbers, part numbers, etc.) that may have wide gaps in their alphabetic and numeric sequences.
Read more about C Programming Language .
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>

#include<malloc.h>

#define MAX 20

struct node

{

int info ;

struct node *link;

};

struct node *head[10];

int n,i,arr[MAX];

main()

{

int i;

printf("Enter the number of elements in the list : ");

scanf("%d", &n);

for(i=0;i<n;i++)

{

printf("Enter element %d : ",i+1);

scanf("%d",&arr[i]);

}/*End of for */

printf("Unsorted list is :n");

for(i=0;i<n;i++)

printf("%d ",arr[i]);

printf("n");

addr_sort();

printf("Sorted list is :n");

for(i=0;i<n;i++)

printf("%d ",arr[i]);

printf("n");

}/*End of main()*/

addr_sort()

{

int i,k,dig;

struct node *p;

int addr;

k=large_dig();

for(i=0;i<=9;i++)

head[i]=NULL;

for(i=0;i<n;i++)

{

addr=hash_fn( arr[i],k );

insert(arr[i],addr);

}

for(i=0; i<=9 ; i++)

{

printf("head(%d) -> ",i);

p=head[i];

while(p!=NULL)

{

printf("%d ",p->info);

p=p->link;

}

printf("n");

}


i=0;

for(k=0;k<=9;k++)

{

p=head[k];

while(p!=NULL)

{

arr[i++]=p->info;

p=p->link;

}

}

}/*End of addr_sort()*/

/*Inserts the number in sorted linked list*/

insert(int num,int addr)

{

struct node *q,*tmp;

tmp= malloc(sizeof(struct node));

tmp->info=num;

/*list empty or item to be added in begining */

if(head[addr] == NULL || num < head[addr]->info)

{

tmp->link=head[addr];

head[addr]=tmp;

return;

}

else

{

q=head[addr];

while(q->link != NULL && q->link->info < num)

q=q->link;

tmp->link=q->link;

q->link=tmp;

}

}/*End of insert()*/

/* Finds number of digits in the largest element of the list */

int large_dig()

{

int large = 0,ndig = 0 ;

for(i=0;i<n;i++)

{

if(arr[i] > large)

large = arr[i];

}

printf("Largest Element is %d , ",large);

while(large != 0)

{

ndig++;

large = large/10 ;

}

printf("Number of digits in it are %dn",ndig);

return(ndig);

} /*End of large_dig()*/

hash_fn(int number,int k)

{

/*Find kth digit of the number*/

int digit,addr,i;

for(i = 1 ; i <=k ; i++)

{

digit = number % 10 ;

number = number /10 ;

}

addr=digit;

return(addr);

}/*End of hash_fn()*/




Read more Similar C Programs

Data Structures


C Sorting

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

K & R C Programs Exercise 5-14.

K and R C, Solution to Exercise 5-14:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program to handle a -r flag which indicates sorting in reverse (decreasing) order. But sure that -r works with -n.Read more about C Programming Language .
/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0

#define MAXLINES 5000
char *lineptr[MAXLINES];

#define MAXLEN 1000

int reverse = FALSE;

int getline(char s[], int lim)
{
int c, i;

for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != 'n'; i++)
s[i] = c;
if (c == 'n') {
s[i++] = c;
}
s[i] = '