Thursday, April 18, 2019

Bucket Sort in C

Here is a program that I wrote to ask the user to give a series of numbers and then the program will sort the series of a number using bucket sort algorithm using C language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting.

work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com



Sample Program Output


Program Listing

/* bucket_sort.c
   Author   : Mr. Jake Rodriguez Pomperada,BSCS,MAED-IT
   Tool     : Dev C++ 5.11
   Date     : April 18, 2019  Thursday   12:56 AM
   Website  : www.jakerpomperada.com
   Email    : jake_pomperada@tup.edu.ph and jakerpomperada@gmail.com
   Location : Bacolod City, Negros Occidental
*/

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

struct bucket 
{
    int count;
    int* value;
};

int compareIntegers(const void* first, const void* second)
{
    int x = *((int*)first), y =  *((int*)second);
    if (x == y)
    {
        return 0;
    }
    else if (x < y)
    {
        return -1;
    }
    else
    {
        return 1;
    }
}

void bucket_sort(int array[],int n)
{
    struct bucket buckets[3];
    int i, j, k;
    for (i = 0; i < 3; i++)
    {
        buckets[i].count = 0;
        buckets[i].value = (int*)malloc(sizeof(int) * n);
    }
    
    for (i = 0; i < n; i++)
    {
        if (array[i] < 0)
        {
            buckets[0].value[buckets[0].count++] = array[i];
        }
        else if (array[i] > 10)
        {
            buckets[2].value[buckets[2].count++] = array[i];
        }
        else
        {
            buckets[1].value[buckets[1].count++] = array[i];
        }
    }
    for (k = 0, i = 0; i < 3; i++)
    {
        qsort(buckets[i].value, buckets[i].count, sizeof(int), &compareIntegers);
        for (j = 0; j < buckets[i].count; j++)
        {
            array[k + j] = buckets[i].value[j];
        }
        k += buckets[i].count;
        free(buckets[i].value);
    }
}



int main()
{
 int items[1000], num=0,min=0,loc=0;
 int c=0, b=0,change=0,j=0,temp=0;
 system("cls");
 printf("\n\n");
 printf("\tBucket Sort Program in C");
 printf("\n\n");
 printf("\tHow many items? : ");
 scanf("%d", &num);
 printf("\n\n");
 for (c= 0; c < num; c++) {
    printf("\tEnter item no. %d: ", c+1);
    scanf("%d", &items[c]);
 }
printf("\n\n");
printf("\tOriginal Arrangement of Numbers");
printf("\n\n");
 for ( c = 0 ; c < num ; c++ ) {
   printf("\t%d ",items[c]);
 }
  bucket_sort(items,num);

 printf("\n\n");
 printf("\tAsceding Order of Numbers");
 printf("\n\n");
 for ( c = 0 ; c < num ; c++ ) {
 printf("\t%d ", items[c]);
 }  
printf("\n\n");
printf("\tEnd of Program");
printf("\n\n");
system("pause");
}


No comments:

Post a Comment