From e5f2ac138f4187e116d7d40ae713bd30d7908489 Mon Sep 17 00:00:00 2001 From: Abhishek Anand Date: Sat, 3 Oct 2020 22:08:17 +0530 Subject: [PATCH] Create PigeonholeSort.c --- C/PigeonholeSort.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 C/PigeonholeSort.c diff --git a/C/PigeonholeSort.c b/C/PigeonholeSort.c new file mode 100644 index 0000000..ce00912 --- /dev/null +++ b/C/PigeonholeSort.c @@ -0,0 +1,59 @@ +#include + +#define MAX 7 + +void pigeonhole_sort(int, int, int *); +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]; + } + } + 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; + } + } +}