C Program to Implement Pigeonhole Sort using Function

By | 27.11.2016

Pigeonhole Sort using Function


Write a C Program to Implement Pigeonhole Sort using Function. Here’s simple C Program to Implement Pigeonhole Sort using Function in C Programming Language.


Pigeonhole sorting, also known as count sort, is a sorting algorithm that is suitable for sorting lists of elements where the number of elements (n) and the number of possible key values (N) are approximately the same.[1] It requires O(n + N) time


Below is the source code for C Program to Implement Pigeonhole Sort using Function which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to Implement Pigeonhole Sort using Function  */

#include <stdio.h>

#define MAX 7

void pigeonhole_sort(int, int, int *);
void main()
{
    int a[MAX], i, min, max;
    printf("\nEnter the values into the matrix ::\n");
    for (i = 0; i < MAX; i++)
    {
        scanf("%d", &a[i]);
    }
    min = a[0];
    max = a[0];
    for (i = 1; i < MAX; i++)
    {
        if (a[i] < min)
        {
            min = a[i];
        }
        if (a[i] > max)
        {
            max = a[i];
        }
    }
    pigeonhole_sort(min, max, a);
    printf("\nSorted order is :: ");
    for (i = 0; i < MAX; i++)
    {
        printf(" %d ", a[i]);
    }
}

/* sorts the array using pigeonhole algorithm */
void pigeonhole_sort(int mi, int ma, int * a)
{

    int size, count = 0, i;
    int *current;
    current = a;
    size = ma - mi + 1;
    int holes[size];
    for (i = 0; i < size; i++)
    {
        holes[i] = 0;
    }
    for (i = 0; i < size; i++, current++)
    {
        holes[*current-mi] += 1;
    }
    for (count = 0, current = &a[0]; count < size; count++)
    {
        while (holes[count]--> 0)
        {
            *current++ = count + mi;
        }
    }
}

Output :


/*  C Program to Implement Pigeonhole Sort using Function  */

Enter the values into the matrix ::
6
9
2
4
3
5
6

Sorted order is ::  2  3  4  5  6  6  9
Process returned 3

Above is the source code for C Program to Implement Pigeonhole Sort using Function which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Category: C Programming Sorting Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments