From 38760ff5c49db8dc2c49aef715c8aa16bdbc6643 Mon Sep 17 00:00:00 2001 From: Chris Kolonas Date: Wed, 25 Nov 2020 21:10:50 +0200 Subject: [PATCH] added PigeonHoleSort algorithm --- Sorts/PigeonHoleSort.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Sorts/PigeonHoleSort.js diff --git a/Sorts/PigeonHoleSort.js b/Sorts/PigeonHoleSort.js new file mode 100644 index 000000000..8af87c882 --- /dev/null +++ b/Sorts/PigeonHoleSort.js @@ -0,0 +1,41 @@ +/* +*Pigeonhole sorting is a sorting algorithm that is suitable +* for sorting lists of elements where the number of elements +* (n) and the length of the range of possible key values (N) +* are approximately the same. + */ +function pigeonhole_sort(arr){ + var min = arr[0]; + var max = arr[0]; + + for(let i=0; i max) + max = arr[i]; + if(arr[i] < min) + min = arr[i]; + } + console.log(max); + console.log(min); + + var range = max - min + 1; + let pigeonhole = Array(range).fill(0); + + for(let i = 0; i0){ + arr[index++]=j+min; + } + + } +} +//Driver code +var arr=Array(8, 3, 2, 7, 4, 6, 8); +pigeonhole_sort(arr); +console.log(arr); +