Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PigeonholeSort.c #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions C/PigeonholeSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>

#define MAX 7

void pigeonhole_sort(int, int, int *);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to pigeonholeSort

void main()
{
int a[MAX], i, min, max;
printf("enter the values into the matrix :");
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];
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove braces for all one line blocks

pigeonhole_sort(min, max, a);
printf("Sorted order is :\n");
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;
}
}
}