By Samar Qureshi; ICS4U

What is pigeonhole sorting?

<aside> 📌 A non-comparison sorting algorithm that takes elements of a given array and temporarily stores them in a separate array of initially empty "pigeonholes". After being systematically ordered in the pigeonhole array, the elements are then transferred back to the original array in increasing order.

</aside>


Step by step explanation

<aside> 💡 Given the array: 13 2 21 19 6 13 15 2 15 16, let's sort it in increasing order.

</aside>


Step 1: Find the minimum and maximum values in the array

//max and min values start off as the first element in the array
int min = arr[0];
int max = arr[0];

for(int h=0; h<arr.length; h++) { 
	    	 //finds the maximum and minimum values
	    	  
	if(arr[h] > max) { //finds the maximum value
			max = arr[h];
	}
	         
	else if(arr[h] < min) { //finds the minimum value
			min = arr[h];
	}

}

Step 2: Calculate the range of the elements using 'max-min+1'

int range = max - min + 1; //calculates the range