11import { Animation } from "../../Animation/Animation" ;
2-
3-
42// SG 07/10/2022 22:34 https://en.wikipedia.org/wiki/Radix_sort
53
64class RadixSortLSD {
7-
85 getMaxValue ( array ) {
96 let max = array [ 0 ] ;
10- for ( const num of array ) {
11- if ( max < num ) max = num ;
7+ for ( const num of array ) {
8+ if ( max < num ) max = num ;
129 }
1310 return max ;
1411 }
1512
1613 /**
17- * @param {Integer } element
18- * @param {[] } DOM
19- * @returns the index of the element in the DOM (Array converted from NodeList of DOM tree)
14+ * @param {Integer } element
15+ * @param {[] } DOM
16+ * @returns the index of the element in the DOM (Array converted from NodeList of DOM tree)
2017 */
2118 getIndexDOM ( element , DOM ) {
22- for ( const e of DOM ) {
23- if ( element === parseInt ( e . id ) ) return DOM . indexOf ( e ) ;
19+ for ( const e of DOM ) {
20+ if ( element === parseInt ( e . id ) ) return DOM . indexOf ( e ) ;
2421 }
2522 }
2623
@@ -36,15 +33,17 @@ class RadixSortLSD {
3633 let i = 0 ;
3734 let counts = new Array ( 100 ) ;
3835
39- for ( let i = 0 ; i < 100 ; i ++ ) {
36+ for ( let i = 0 ; i < 100 ; i ++ ) {
4037 counts [ i ] = 0 ;
4138 }
4239
43- for ( let i = 0 ; i < k ; i ++ ) { // SG 07/11/2022 09:37 animating iterator
40+ for ( let i = 0 ; i < k ; i ++ ) {
41+ // SG 07/11/2022 09:37 animating iterator
4442 counts [ Math . floor ( array [ i ] / exp ) % 100 ] ++ ;
45- if ( ! options . skipJ ) {
46- if ( DOM . length > 1000 ) { // for more than 1000 bars, only animate in every 3 bars
47- if ( i % 3 === 0 ) {
43+ if ( ! options . skipJ ) {
44+ if ( DOM . length > 1000 ) {
45+ // for more than 1000 bars, only animate in every 3 bars
46+ if ( i % 3 === 0 ) {
4847 await Animation . getAnimation ( DELAY + 2 ) ;
4948 DOM [ i ] . style . backgroundColor = PROCESSING_COLOR ;
5049 await Animation . getAnimation ( DELAY + 2 ) ;
@@ -59,42 +58,46 @@ class RadixSortLSD {
5958 }
6059 }
6160
62-
6361 for ( i = 1 ; i < 100 ; i ++ ) {
6462 counts [ i ] += counts [ i - 1 ] ;
6563 }
6664
67-
68-
6965 for ( i = k - 1 ; i >= 0 ; i -- ) {
7066 await Animation . getAnimation ( DELAY ) ;
7167 const oldValue = array [ i ] ; // value being sorted to a new index
7268 const oldIndex = this . getIndexDOM ( oldValue , DOM ) ; // index of old value
7369 const sortedIndex = counts [ Math . floor ( array [ i ] / exp ) % 100 ] - 1 ;
7470
75- Animation . swap ( oldIndex , sortedIndex ) ; // SG 07/10/2022 22:35 swap animation
76-
77- if ( finalIteration ) { // SG 07/11/2022 00:29 Sorted array at the last iteration, coloring them sortedColor
71+ Animation . swap ( oldIndex , sortedIndex ) ; // SG 07/10/2022 22:35 swap animation
72+
73+ if ( finalIteration ) {
74+ // SG 07/11/2022 00:29 Sorted array at the last iteration, coloring them sortedColor
7875 DOM [ sortedIndex ] . style . backgroundColor = SORTED_COLOR ;
7976 }
8077 sortedArray [ sortedIndex ] = oldValue ;
8178 counts [ Math . floor ( array [ i ] / exp ) % 100 ] -- ;
8279 }
8380
84-
85- for ( i = 0 ; i < k ; i ++ ) { // SG 07/10/2022 22:35 copying sorted values
81+ for ( i = 0 ; i < k ; i ++ ) {
82+ // SG 07/10/2022 22:35 copying sorted values
8683 array [ i ] = sortedArray [ i ] ;
8784 }
8885 }
8986
9087 async perform ( options , array ) {
91- let max = this . getMaxValue ( array ) ;
92- let DOM = Array . from ( document . querySelectorAll ( '.array-bars' ) ) ;
93-
94- for ( let i = 1 ; Math . floor ( max / i ) > 0 ; i *= 100 ) {
95- await this . sort ( array , i , DOM , options , ( ( Math . floor ( max / i * 100 ) > 0 ) && i !== 1 ) ) ;
88+ let max = this . getMaxValue ( array ) ;
89+ let DOM = Array . from ( document . querySelectorAll ( ".array-bars" ) ) ;
90+
91+ for ( let i = 1 ; Math . floor ( max / i ) > 0 ; i *= 100 ) {
92+ await this . sort (
93+ array ,
94+ i ,
95+ DOM ,
96+ options ,
97+ Math . floor ( ( max / i ) * 100 ) > 0 && i !== 1
98+ ) ;
9699 }
97100 }
98101}
99102
100- export { RadixSortLSD }
103+ export { RadixSortLSD } ;
0 commit comments