11import { Animation } from "../../Animation/Animation" ;
22
3-
43class QuickSort {
54 constructor ( ) {
65 this . options = null ;
76 }
87
98 getIndexDOM ( element , DOM ) {
10- for ( const e of DOM ) {
11- if ( element === parseInt ( e . id ) ) return DOM . indexOf ( e ) ;
9+ for ( const e of DOM ) {
10+ if ( element === parseInt ( e . id ) ) return DOM . indexOf ( e ) ;
1211 }
1312 }
1413
15-
1614 async swap ( array , left , right ) {
1715 // SG 07/14/2022 12:08 getting index of elements to be swapped in the DOM for animation
18- let DOM = Array . from ( document . querySelectorAll ( ' .array-bars' ) ) ;
16+ let DOM = Array . from ( document . querySelectorAll ( " .array-bars" ) ) ;
1917 let first = this . getIndexDOM ( array [ left ] , DOM ) ;
2018 let second = this . getIndexDOM ( array [ right ] , DOM ) ;
2119
@@ -24,13 +22,12 @@ class QuickSort {
2422 array [ left ] = array [ right ] ;
2523 array [ right ] = temp ;
2624
27-
2825 await Animation . getAnimation ( this . options . delay ) ;
2926 Animation . swap ( first , second ) ;
3027 }
3128
3229 async partition ( array , left , right ) {
33- let DOM = Array . from ( document . querySelectorAll ( ' .array-bars' ) ) ;
30+ let DOM = Array . from ( document . querySelectorAll ( " .array-bars" ) ) ;
3431 let i = left ;
3532 let j = right ;
3633
@@ -41,14 +38,14 @@ class QuickSort {
4138 // SG 07/14/2022 14:13 coloring pivot
4239 DOM [ pivotIndex ] . style . backgroundColor = this . options . currentMinBarColor ;
4340
44- while ( left <= right ) {
45- while ( array [ left ] < pivot ) {
41+ while ( left <= right ) {
42+ while ( array [ left ] < pivot ) {
4643 left ++ ;
4744 }
48- while ( array [ right ] > pivot ) {
45+ while ( array [ right ] > pivot ) {
4946 right -- ;
5047 }
51- if ( left <= right ) {
48+ if ( left <= right ) {
5249 await this . swap ( array , left , right ) ;
5350 left ++ ;
5451 right -- ;
@@ -61,51 +58,47 @@ class QuickSort {
6158
6259 // SG 07/14/2022 13:14 color pivor back to default bar color
6360 DOM [ pivotIndex ] . style . backgroundColor = this . options . defaultBarColor ;
64-
61+
6562 return left ;
6663 }
6764
68-
6965 async quickSort ( array , left , right ) {
7066 let i ;
71- let DOM = Array . from ( document . querySelectorAll ( ' .array-bars' ) ) ;
67+ let DOM = Array . from ( document . querySelectorAll ( " .array-bars" ) ) ;
7268
73- // SG 07/14/2022 13:50 coloring the partition
69+ // SG 07/14/2022 13:50 coloring the partition
7470 DOM [ left ] . style . backgroundColor = this . options . processingColor ;
7571 DOM [ right ] . style . backgroundColor = this . options . processingColor ;
7672
77- if ( array . length > 1 ) {
73+ if ( array . length > 1 ) {
7874 i = await this . partition ( array , left , right ) ;
79- if ( left < i - 1 ) {
80- if ( this . options . skipJ ) {
75+ if ( left < i - 1 ) {
76+ if ( this . options . skipJ ) {
8177 this . quickSort ( array , left , i - 1 ) ;
8278 } else {
8379 await this . quickSort ( array , left , i - 1 ) ;
8480 }
85- }
86- if ( i < right ) {
87- if ( this . options . skipJ ) {
81+ }
82+ if ( i < right ) {
83+ if ( this . options . skipJ ) {
8884 this . quickSort ( array , i , right ) ;
89- } else {
85+ } else {
9086 await this . quickSort ( array , i , right ) ;
9187 }
9288 }
9389 }
9490
95-
9691 // SG 07/14/2022 13:38 coloring sorted bars
9792 DOM [ left ] . style . backgroundColor = this . options . sortedBarColor ;
9893 DOM [ right ] . style . backgroundColor = this . options . sortedBarColor ;
9994
100-
10195 return array ;
10296 }
10397
10498 async perform ( options , array ) {
10599 this . options = options ;
106- await this . quickSort ( array , 0 , array . length - 1 ) ;
100+ await this . quickSort ( array , 0 , array . length - 1 ) ;
107101 }
108102}
109103
110-
111- export { QuickSort } ;
104+ export { QuickSort } ;
0 commit comments