1- var array = [ 44 , 2 , 34 , 6 , 7 , 34 , 4 , 4 , 2 , 3 , 6 , 8 ] ;
2-
31/**
42 * Counting sort algorithm. It's with complexity O(n) but it's
53 * correct for specific input.
@@ -8,81 +6,79 @@ var array = [44,2,34,6,7,34,4,4,2,3,6,8];
86 */
97var countingSort = function ( ) {
108
11- /**
12- * Gets the count of the elements into the input array
13- *
14- * @private
15- * @param {array } array The input array
16- * @returns {array } count The count of each element from the input array
17- */
18- function getCount ( array ) {
19- var count = [ ] ,
20- current ;
21- for ( var i = 0 ; i < array . length ; i += 1 ) {
22- current = array [ i ] ;
23- if ( count [ current ] !== undefined ) {
24- count [ current ] += 1 ;
25- } else {
26- count [ current ] = 1 ;
27- }
28- }
29- return count ;
9+ /**
10+ * Gets the count of the elements into the input array
11+ *
12+ * @private
13+ * @param {array } array The input array
14+ * @returns {array } count The count of each element from the input array
15+ */
16+ function getCount ( array ) {
17+ var count = [ ] ,
18+ current ;
19+ for ( var i = 0 ; i < array . length ; i += 1 ) {
20+ current = array [ i ] ;
21+ if ( count [ current ] !== undefined ) {
22+ count [ current ] += 1 ;
23+ } else {
24+ count [ current ] = 1 ;
25+ }
3026 }
27+ return count ;
28+ }
3129
32- /**
33- * Gets the count of the elements which are less than a given
34- *
35- * @private
36- * @param {array } array The input array
37- * @returns {array } less The count of the elements which are less than each element from the input
38- */
39- function getLessCount ( array ) {
40- var less = [ ] ,
41- last ;
42- less [ 0 ] = array [ 0 ] || 0 ;
43- for ( var i = 1 ; i < array . length ; i += 1 ) {
44- last = array [ i - 1 ] || 0 ;
45- less [ i ] = last + less [ i - 1 ] ;
46- }
47- return less ;
30+ /**
31+ * Gets the count of the elements which are less than a given
32+ *
33+ * @private
34+ * @param {array } array The input array
35+ * @returns {array } less The count of the elements which are less than each element from the input
36+ */
37+ function getLessCount ( array ) {
38+ var less = [ ] ,
39+ last ;
40+ less [ 0 ] = array [ 0 ] || 0 ;
41+ for ( var i = 1 ; i < array . length ; i += 1 ) {
42+ last = array [ i - 1 ] || 0 ;
43+ less [ i ] = last + less [ i - 1 ] ;
4844 }
45+ return less ;
46+ }
4947
50- /**
51- * Sorts the input array
52- *
53- * @private
54- * @param {array } array Input which should be sorted
55- * @param {array } less Count of the less elements for each element
56- * @returns {array } result The sorted input
57- */
58- function sort ( array , less ) {
59- var result = [ ] ,
60- currentPositions = [ ] ,
61- current ,
62- position ;
63- for ( var i = 0 ; i < array . length ; i += 1 ) {
64- current = array [ i ] ;
65- position = less [ current ] ;
66- if ( currentPositions [ current ] === undefined ) {
67- currentPositions [ current ] = position ;
68- }
69- result [ currentPositions [ current ] ] = current ;
70- currentPositions [ current ] += 1 ;
71- }
72- return result ;
48+ /**
49+ * Sorts the input array
50+ *
51+ * @private
52+ * @param {array } array Input which should be sorted
53+ * @param {array } less Count of the less elements for each element
54+ * @returns {array } result The sorted input
55+ */
56+ function sort ( array , less ) {
57+ var result = [ ] ,
58+ currentPositions = [ ] ,
59+ current ,
60+ position ;
61+ for ( var i = 0 ; i < array . length ; i += 1 ) {
62+ current = array [ i ] ;
63+ position = less [ current ] ;
64+ if ( currentPositions [ current ] === undefined ) {
65+ currentPositions [ current ] = position ;
66+ }
67+ result [ currentPositions [ current ] ] = current ;
68+ currentPositions [ current ] += 1 ;
7369 }
70+ return result ;
71+ }
7472
75- /**
76- * Sorts a given array
77- *
78- * @public
79- * @param {array } array Array which should be sorted
80- * @returns {array } array Sorted array
81- */
82- return function ( array ) {
83- var less = getLessCount ( getCount ( array ) ) ;
84- return sort ( array , less ) ;
85- } ;
86- } ( ) ;
87-
88- console . log ( countingSort ( array ) ) ;
73+ /**
74+ * Sorts a given array
75+ *
76+ * @public
77+ * @param {array } array Array which should be sorted
78+ * @returns {array } array Sorted array
79+ */
80+ return function ( array ) {
81+ var less = getLessCount ( getCount ( array ) ) ;
82+ return sort ( array , less ) ;
83+ } ;
84+ } ( ) ;
0 commit comments