Skip to content

Commit 07e714a

Browse files
authored
Merge pull request deutranium#74 from adithyaakrishna/implement-pigeonhole-sort
Implemented Pigeon Hole Sort
2 parents b06772c + 059f68d commit 07e714a

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
3+
class GFG
4+
{
5+
public static void pigeonhole_sort(int []arr, int n)
6+
{
7+
int min = arr[0];
8+
int max = arr[0];
9+
int range, i, j, index;
10+
11+
for(int a = 0; a < n; a++)
12+
{
13+
if(arr[a] > max)
14+
max = arr[a];
15+
if(arr[a] < min)
16+
min = arr[a];
17+
}
18+
19+
range = max - min + 1;
20+
int[] phole = new int[range];
21+
22+
for(i = 0; i < n; i++)
23+
phole[i] = 0;
24+
25+
for(i = 0; i < n; i++)
26+
phole[arr[i] - min]++;
27+
28+
29+
index = 0;
30+
31+
for(j = 0; j < range; j++)
32+
while(phole[j] --> 0)
33+
arr[index++] = j + min;
34+
35+
}
36+
37+
// Driver Code
38+
static void Main()
39+
{
40+
int[] arr = {8, 3, 2, 7,
41+
4, 6, 8};
42+
43+
Console.Write("Sorted order is : ");
44+
45+
pigeonhole_sort(arr,arr.Length);
46+
47+
for(int i = 0 ; i < arr.Length ; i++)
48+
Console.Write(arr[i] + " ");
49+
}
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.lang.*;
2+
import java.util.*;
3+
4+
public class MySort
5+
{
6+
public static void pigeonholeSort(int arr[], int n)
7+
{
8+
int min = arr[0];
9+
int max = arr[0];
10+
int range, i, j, index;
11+
12+
for(int a=0; a<n; a++)
13+
{
14+
if(arr[a] > max)
15+
max = arr[a];
16+
if(arr[a] < min)
17+
min = arr[a];
18+
}
19+
20+
range = max - min + 1;
21+
int[] phole = new int[range];
22+
Arrays.fill(phole, 0);
23+
24+
for(i = 0; i<n; i++)
25+
phole[arr[i] - min]++;
26+
27+
28+
index = 0;
29+
30+
for(j = 0; j<range; j++)
31+
while(phole[j]-->0)
32+
arr[index++]=j+min;
33+
34+
}
35+
36+
public static void main(String[] args)
37+
{
38+
MySort sort = new MySort();
39+
int[] arr = {8, 3, 2, 7, 4, 6, 8};
40+
41+
System.out.print("Sorted order is : ");
42+
43+
sort.pigeonholeSort(arr,arr.length);
44+
45+
for(int i=0 ; i<arr.length ; i++)
46+
System.out.print(arr[i] + " ");
47+
}
48+
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def pigeonholeSort(a):
2+
3+
my_min = min(a)
4+
my_max = max(a)
5+
size = my_max - my_min + 1
6+
7+
# our list of pigeonholes
8+
holes = [0] * size
9+
10+
# Populate the pigeonholes.
11+
for x in a:
12+
assert type(x) is int, "Input Only Integers"
13+
holes[x - my_min] += 1
14+
15+
# Put the elements back into the array in order.
16+
i = 0
17+
for count in range(size):
18+
while holes[count] > 0:
19+
holes[count] -= 1
20+
a[i] = count + my_min
21+
i += 1
22+
#Get The Size of The Array
23+
print("Enter the Size of the Array :")
24+
n=int(input())
25+
a=[]
26+
#Get The Elements of The Array
27+
for i in range(0,n):
28+
ele=int(input())
29+
a.append(ele)
30+
31+
print("Pigeon Hole Sorted order is : ", end = ' ')
32+
33+
pigeonholeSort(a)
34+
35+
for i in range(0, len(a)):
36+
print(a[i], end = ' ')

0 commit comments

Comments
 (0)