1
- var array = [ 0.21 , 0.44 , 0.221 , 0.01 , 0.88 ] ;
2
-
3
1
/**
4
2
* Bucketsort. This algorithm has complexity O(n) but it's not
5
3
* correct for every input.
@@ -8,100 +6,99 @@ var array = [0.21, 0.44, 0.221, 0.01, 0.88];
8
6
*/
9
7
var bucketSort = ( function ( ) {
10
8
11
- /**
12
- * Insertionsort.
13
- *
14
- * @private
15
- * @param {array } array Input array
16
- * @returns {array } array Sorted input array
17
- */
18
- function insertionSort ( array ) {
19
- var current ,
20
- j ;
21
- for ( var i = 1 ; i < array . length ; i += 1 ) {
22
- current = array [ i ] ;
23
- j = i - 1 ;
24
- while ( j >= 0 && current < array [ j ] ) {
25
- array [ j + 1 ] = array [ j ] ;
26
- j -= 1 ;
27
- }
28
- array [ j + 1 ] = current ;
29
- }
30
- return array ;
9
+ /**
10
+ * Insertionsort.
11
+ *
12
+ * @private
13
+ * @param {array } array Input array
14
+ * @returns {array } array Sorted input array
15
+ */
16
+ function insertionSort ( array ) {
17
+ var current ,
18
+ j ;
19
+ for ( var i = 1 ; i < array . length ; i += 1 ) {
20
+ current = array [ i ] ;
21
+ j = i - 1 ;
22
+ while ( j >= 0 && current < array [ j ] ) {
23
+ array [ j + 1 ] = array [ j ] ;
24
+ j -= 1 ;
25
+ }
26
+ array [ j + 1 ] = current ;
31
27
}
28
+ return array ;
29
+ }
32
30
33
- /**
34
- * Creates buckets for given array
35
- *
36
- * @private
37
- * @param {array } array Input array
38
- * @returns {array } buckets Array whith array for each bucket.
39
- * Each bucket contains an array with all elements from the input which are with suitable size.
40
- */
41
- function createBuckets ( array ) {
42
- var buckets = [ ] ,
43
- currentBucket ,
44
- current ,
45
- sectorSize = 1 / array . length ;
46
- for ( var i = 0 ; i < array . length ; i += 1 ) {
47
- current = array [ i ] ;
48
- currentBucket = Math . floor ( current / sectorSize ) ;
49
- if ( buckets [ currentBucket ] === undefined ) {
50
- buckets [ currentBucket ] = [ ] ;
51
- }
52
- buckets [ currentBucket ] . push ( current ) ;
53
- }
54
- return buckets ;
31
+ /**
32
+ * Creates buckets for given array
33
+ *
34
+ * @private
35
+ * @param {array } array Input array
36
+ * @returns {array } buckets Array whith array for each bucket.
37
+ * Each bucket contains an array with all elements from the input which are with suitable size.
38
+ */
39
+ function createBuckets ( array ) {
40
+ var buckets = [ ] ,
41
+ currentBucket ,
42
+ current ,
43
+ sectorSize = 1 / array . length ;
44
+ for ( var i = 0 ; i < array . length ; i += 1 ) {
45
+ current = array [ i ] ;
46
+ currentBucket = Math . floor ( current / sectorSize ) ;
47
+ if ( buckets [ currentBucket ] === undefined ) {
48
+ buckets [ currentBucket ] = [ ] ;
49
+ }
50
+ buckets [ currentBucket ] . push ( current ) ;
55
51
}
52
+ return buckets ;
53
+ }
56
54
57
- /**
58
- * Sorts the arrays from each bucket.
59
- *
60
- * @private
61
- * @param {array } buckets Given buckets
62
- * @returns {array } buckets Buckets with sorted arrays for each bucket
63
- */
64
- function sortBuckets ( buckets ) {
65
- for ( var i = 0 ; i < buckets . length ; i += 1 ) {
66
- if ( buckets [ i ] !== undefined )
67
- insertionSort ( buckets [ i ] ) ;
68
- }
69
- return buckets ;
55
+ /**
56
+ * Sorts the arrays from each bucket.
57
+ *
58
+ * @private
59
+ * @param {array } buckets Given buckets
60
+ * @returns {array } buckets Buckets with sorted arrays for each bucket
61
+ */
62
+ function sortBuckets ( buckets ) {
63
+ for ( var i = 0 ; i < buckets . length ; i += 1 ) {
64
+ if ( buckets [ i ] !== undefined ) {
65
+ insertionSort ( buckets [ i ] ) ;
66
+ }
70
67
}
68
+ return buckets ;
69
+ }
71
70
72
- /**
73
- * Unions all buckets' arrays
74
- *
75
- * @private
76
- * @param {array } buckets Input buckets
77
- * @returns {array } result Sorted array which contains all elements form each bucket
78
- */
79
- function unionBuckets ( buckets ) {
80
- var result = [ ] ,
81
- currentBucket ;
82
- for ( var i = 0 ; i < buckets . length ; i += 1 ) {
83
- currentBucket = buckets [ i ] ;
84
- if ( currentBucket !== undefined ) {
85
- for ( var j = 0 ; j < currentBucket . length ; j += 1 ) {
86
- result . push ( currentBucket [ j ] ) ;
87
- }
88
- }
71
+ /**
72
+ * Unions all buckets' arrays
73
+ *
74
+ * @private
75
+ * @param {array } buckets Input buckets
76
+ * @returns {array } result Sorted array which contains all elements form each bucket
77
+ */
78
+ function unionBuckets ( buckets ) {
79
+ var result = [ ] ,
80
+ currentBucket ;
81
+ for ( var i = 0 ; i < buckets . length ; i += 1 ) {
82
+ currentBucket = buckets [ i ] ;
83
+ if ( currentBucket !== undefined ) {
84
+ for ( var j = 0 ; j < currentBucket . length ; j += 1 ) {
85
+ result . push ( currentBucket [ j ] ) ;
89
86
}
90
- return result ;
87
+ }
91
88
}
89
+ return result ;
90
+ }
92
91
93
- /**
94
- * Sorts given array with bucketsort
95
- *
96
- * @public
97
- * @param {array } array Input array which should be sorted
98
- * @returns {array } Sorted array
99
- */
100
- return function ( array ) {
101
- var buckets = createBuckets ( array ) ;
102
- sortBuckets ( buckets ) ;
103
- return unionBuckets ( buckets ) ;
104
- } ;
105
- } ( ) ) ;
106
-
107
- console . log ( bucketSort ( array ) ) ;
92
+ /**
93
+ * Sorts given array with bucketsort
94
+ *
95
+ * @public
96
+ * @param {array } array Input array which should be sorted
97
+ * @returns {array } Sorted array
98
+ */
99
+ return function ( array ) {
100
+ var buckets = createBuckets ( array ) ;
101
+ sortBuckets ( buckets ) ;
102
+ return unionBuckets ( buckets ) ;
103
+ } ;
104
+ } ( ) ) ;
0 commit comments