From 3da83115a303abca9b2cd1df0f6083bba46f909b Mon Sep 17 00:00:00 2001 From: archie94 Date: Wed, 29 Jun 2016 00:50:53 +0530 Subject: [PATCH 1/2] Implemented Pigeonhole Sort --- algorithm/category.json | 3 +- algorithm/sorting/pigeonhole/basic/code.js | 42 ++++++++++++++++++++++ algorithm/sorting/pigeonhole/basic/data.js | 6 ++++ algorithm/sorting/pigeonhole/desc.json | 13 +++++++ gulpfile.babel.js | 1 + 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 algorithm/sorting/pigeonhole/basic/code.js create mode 100644 algorithm/sorting/pigeonhole/basic/data.js create mode 100644 algorithm/sorting/pigeonhole/desc.json diff --git a/algorithm/category.json b/algorithm/category.json index 980c8470..f0e3dbf9 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -73,11 +73,12 @@ "bucket": "Bucket Sort", "bubble": "Bubble Sort", "comb": "Comb Sort", - "counting": "Counting Sort", + "counting": "Counting Sort", "cycle": "Cycle Sort", "heap": "Heapsort", "insertion": "Insertion Sort", "merge": "Merge Sort", + "pigeonhole": "Pigeonhole Sort", "quick": "Quicksort", "radix": "Radix Sort", "selection": "Selection Sort", diff --git a/algorithm/sorting/pigeonhole/basic/code.js b/algorithm/sorting/pigeonhole/basic/code.js new file mode 100644 index 00000000..385f8d8a --- /dev/null +++ b/algorithm/sorting/pigeonhole/basic/code.js @@ -0,0 +1,42 @@ +var min = A[0]; +var max = A[0]; + +for( var i = 1; i < N; i++ ) { + if( A[i] < min ) { + min = A[i]; + } + if( A[i] > max ) { + max = A[i]; + } +} +var range = max - min + 1; + +var holes = new Array ( range ); +for ( var i = 0; i < range; i++ ) { + holes[i] = []; +} +tracer2._setData( holes ); + +logTracer._print ( 'Filling up holes' ); +for ( var i = 0; i < N ; i++ ) { + tracer1._select ( i )._wait (); + + holes[ A[i] - min ].push( A[i] ); + + tracer2._setData( holes ); + tracer1._deselect ( i ); +} + +logTracer._print ( 'Building sorted array' ); +var k = 0; +for ( var i = 0; i < range ; i++ ) { + for (var j = 0; j < holes[i].length; j++ ) { + tracer2._select ( i, j )._wait (); + A[k++] = holes[i][j]; + tracer1._notify ( k-1, A[k-1] )._wait (); + tracer2._deselect ( i, j ); + tracer1._denotify ( k-1 ); + } +} + +logTracer._print ( 'Sorted array is ' + A ); \ No newline at end of file diff --git a/algorithm/sorting/pigeonhole/basic/data.js b/algorithm/sorting/pigeonhole/basic/data.js new file mode 100644 index 00000000..69384fe6 --- /dev/null +++ b/algorithm/sorting/pigeonhole/basic/data.js @@ -0,0 +1,6 @@ +var A = Array1D.random(7); +var N = A.length; + +var tracer1 = new Array1DTracer ( 'Array' )._setData ( A ); +var tracer2 = new Array2DTracer ( 'Holes' ); +var logTracer = new LogTracer ( 'Console' ); \ No newline at end of file diff --git a/algorithm/sorting/pigeonhole/desc.json b/algorithm/sorting/pigeonhole/desc.json new file mode 100644 index 00000000..f5b6a8d1 --- /dev/null +++ b/algorithm/sorting/pigeonhole/desc.json @@ -0,0 +1,13 @@ +{ + "Pigeonhole Sort": "Pigeonhole sorting is a sorting algorithm that is suitable for sorting lists of elements where the number of elements (n) and the number of possible key values (N) are approximately the same.", + "Complexity": { + "time": " O(n + N)", + "space": "O(n)" + }, + "References": [ + "Wikipedia" + ], + "files": { + "basic": "Pigeonhole Sort" + } +} diff --git a/gulpfile.babel.js b/gulpfile.babel.js index da390bde..c46544ba 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -1,4 +1,5 @@ 'use strict'; +var Promise = require ('es6-promise').Promise; import path from "path"; import gulp from "gulp"; From 29ddfdfc04ad5220b3999fce8454a9d2bcfdaa73 Mon Sep 17 00:00:00 2001 From: archie94 Date: Wed, 29 Jun 2016 00:53:25 +0530 Subject: [PATCH 2/2] Undo gulpfile.babel.js change --- gulpfile.babel.js | 1 - 1 file changed, 1 deletion(-) diff --git a/gulpfile.babel.js b/gulpfile.babel.js index c46544ba..da390bde 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -1,5 +1,4 @@ 'use strict'; -var Promise = require ('es6-promise').Promise; import path from "path"; import gulp from "gulp"; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy