From 38760ff5c49db8dc2c49aef715c8aa16bdbc6643 Mon Sep 17 00:00:00 2001 From: Chris Kolonas Date: Wed, 25 Nov 2020 21:10:50 +0200 Subject: [PATCH 1/7] 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); + From d4ede60e3cc7a7b2fa498a6a852d9caaf38cf4e2 Mon Sep 17 00:00:00 2001 From: Chris Kolonas Date: Wed, 25 Nov 2020 21:13:41 +0200 Subject: [PATCH 2/7] added PigeonHoleSort algorithm --- Sorts/PigeonHoleSort.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sorts/PigeonHoleSort.js b/Sorts/PigeonHoleSort.js index 8af87c882..b5871ed35 100644 --- a/Sorts/PigeonHoleSort.js +++ b/Sorts/PigeonHoleSort.js @@ -1,4 +1,6 @@ /* +https://en.wikipedia.org/wiki/Pigeonhole_sort + *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) From 4a040c38acacd6d560639d35c225662e70bffcaa Mon Sep 17 00:00:00 2001 From: Chris Kolonas Date: Wed, 25 Nov 2020 21:18:10 +0200 Subject: [PATCH 3/7] added PigeonHoleSort algorithm --- Sorts/PigeonHoleSort.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sorts/PigeonHoleSort.js b/Sorts/PigeonHoleSort.js index b5871ed35..3ac695d2d 100644 --- a/Sorts/PigeonHoleSort.js +++ b/Sorts/PigeonHoleSort.js @@ -39,5 +39,4 @@ function pigeonhole_sort(arr){ //Driver code var arr=Array(8, 3, 2, 7, 4, 6, 8); pigeonhole_sort(arr); -console.log(arr); - +console.log(arr); \ No newline at end of file From 9b8bd417d4062011ee02ed093747074769c84ec8 Mon Sep 17 00:00:00 2001 From: Chris Kolonas Date: Wed, 25 Nov 2020 21:25:36 +0200 Subject: [PATCH 4/7] added PigeonHoleSort algorithm --- Sorts/PigeonHoleSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/PigeonHoleSort.js b/Sorts/PigeonHoleSort.js index 3ac695d2d..98999a6db 100644 --- a/Sorts/PigeonHoleSort.js +++ b/Sorts/PigeonHoleSort.js @@ -39,4 +39,4 @@ function pigeonhole_sort(arr){ //Driver code var arr=Array(8, 3, 2, 7, 4, 6, 8); pigeonhole_sort(arr); -console.log(arr); \ No newline at end of file +console.log(arr); From 8c928ad565214757eee13a6c7efeebb446d8a866 Mon Sep 17 00:00:00 2001 From: Chris Kolonas Date: Wed, 25 Nov 2020 21:31:27 +0200 Subject: [PATCH 5/7] added PigeonHoleSort algorithm --- Sorts/PigeonHoleSort.js | 58 +++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/Sorts/PigeonHoleSort.js b/Sorts/PigeonHoleSort.js index 98999a6db..820cd9748 100644 --- a/Sorts/PigeonHoleSort.js +++ b/Sorts/PigeonHoleSort.js @@ -6,37 +6,33 @@ https://en.wikipedia.org/wiki/Pigeonhole_sort * (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; - } +function pigeonhole_sort (arr) { + let min = arr[0] + let max = arr[0] + for (let i = 0; i < arr.length; i++) { + if (arr[i] > max) { max = arr[i] } + if (arr[i] < min) { min = arr[i] } + } + console.log(max) + console.log(min) + + const range = max - min + 1 + const pigeonhole = Array(range).fill(0) + + for (let i = 0; i < arr.length; i++) { + pigeonhole[arr[i] - min]++ + } + + let index = 0 + + for (let j = 0; j < range; j++) { + while (pigeonhole[j]-- > 0) { + arr[index++] = j + min } + } } -//Driver code -var arr=Array(8, 3, 2, 7, 4, 6, 8); -pigeonhole_sort(arr); -console.log(arr); +// Driver code +const arr = Array(8, 3, 2, 7, 4, 6, 8) +pigeonhole_sort(arr) +console.log(arr) From 7aa0a66ea5c0b2868930396dd5cb05449231490e Mon Sep 17 00:00:00 2001 From: Chris Kolonas Date: Wed, 25 Nov 2020 21:36:55 +0200 Subject: [PATCH 6/7] changes --- Sorts/PigeonHoleSort.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sorts/PigeonHoleSort.js b/Sorts/PigeonHoleSort.js index 820cd9748..f8eed47ed 100644 --- a/Sorts/PigeonHoleSort.js +++ b/Sorts/PigeonHoleSort.js @@ -6,7 +6,7 @@ https://en.wikipedia.org/wiki/Pigeonhole_sort * (n) and the length of the range of possible key values (N) * are approximately the same. */ -function pigeonhole_sort (arr) { +function pigeonHoleSort(arr) { let min = arr[0] let max = arr[0] @@ -33,6 +33,6 @@ function pigeonhole_sort (arr) { } } // Driver code -const arr = Array(8, 3, 2, 7, 4, 6, 8) -pigeonhole_sort(arr) +const arr = [8, 3, 2, 7, 4, 6, 8] +pigeonHoleSort(arr) console.log(arr) From 2f5504ed64f045777e5118c0f5cbb6c8f55de04a Mon Sep 17 00:00:00 2001 From: Chris Kolonas Date: Wed, 25 Nov 2020 21:41:30 +0200 Subject: [PATCH 7/7] more changes --- Sorts/PigeonHoleSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/PigeonHoleSort.js b/Sorts/PigeonHoleSort.js index f8eed47ed..fc2265049 100644 --- a/Sorts/PigeonHoleSort.js +++ b/Sorts/PigeonHoleSort.js @@ -6,7 +6,7 @@ https://en.wikipedia.org/wiki/Pigeonhole_sort * (n) and the length of the range of possible key values (N) * are approximately the same. */ -function pigeonHoleSort(arr) { +function pigeonHoleSort (arr) { let min = arr[0] let max = arr[0]