@@ -24,7 +24,7 @@ var heightScale = d3
2424
2525// initialized a chart with random value
2626createChart ( data ) ;
27-
27+ /*
2828let Sort = new sortData(data);
2929Sort.selectionSort = function () {
3030 const timer = (ms) => new Promise((res) => setTimeout(res, ms));
@@ -101,12 +101,145 @@ Sort.bubbleSort = function () {
101101 }
102102 sort();
103103};
104+ */
105+ const selectionS = {
106+ selectionSort ( ) {
107+ const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
108+
109+ async function sort ( self ) {
110+ for ( let i = 0 ; i < data . length ; i ++ ) {
111+ if ( self . abort ) {
112+ self . abort = false ;
113+ return ;
114+ }
115+ smallest = data [ i ] ;
116+ pos = i ;
117+ changeBarColor ( smallest , smallestColor ) ;
118+ await timer ( time ) ;
119+ for ( var j = i + 1 ; j < data . length ; j ++ ) {
120+ if ( self . abort ) {
121+ self . abort = false ;
122+ return ;
123+ }
124+ changeBarColor ( data [ j ] , traverseColor ) ;
125+ if ( smallest > data [ j ] ) {
126+ await timer ( time ) ;
127+ changeBarColor ( smallest , unsortedColor ) ;
128+ smallest = data [ j ] ;
129+ pos = j ;
130+ }
131+
132+ changeBarColor ( smallest , smallestColor ) ;
133+ await timer ( time ) ;
134+ changeBarColor ( data [ j ] , unsortedColor ) ;
135+ }
136+ if ( data [ i ] != smallest ) {
137+ temp = data [ i ] ;
138+ data [ i ] = smallest ;
139+ data [ pos ] = temp ;
140+
141+ var swooshAudio = new Audio ( "./sound-effects/swoosh.mp3" ) ;
142+ swooshAudio . play ( ) ;
143+ }
144+ changeBarColor ( smallest , sortedColor ) ;
145+ swapBar ( data ) ;
146+ await timer ( time ) ; // then the created Promise can be awaited
147+ }
148+ }
149+
150+ sort ( this ) ;
151+ } ,
152+
153+ selectionSortStop ( ) {
154+ this . abort = true ;
155+ } ,
156+ } ;
104157
158+ const bubbleS = {
159+ bubbleSort ( ) {
160+ const timer = ( ms ) => new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
161+
162+ async function sort ( self ) {
163+ var temp ;
164+ for ( let i = 0 ; i < data . length - 1 ; i ++ ) {
165+ console . log ( self . abort ) ;
166+ if ( self . abort ) {
167+ self . abort = false ;
168+ return ;
169+ }
170+
171+ changeBarColor ( data [ 0 ] , smallestColor ) ;
172+ await timer ( time ) ;
173+ for ( j = 0 ; j < data . length - i - 1 ; j ++ ) {
174+ if ( self . abort ) {
175+ self . abort = false ;
176+ changeBarColor ( data [ j ] , unsortedColor ) ;
177+ return ;
178+ }
179+ await timer ( time ) ;
180+ changeBarColor ( data [ j + 1 ] , traverseColor ) ;
181+ await timer ( time ) ;
182+ if ( data [ j ] > data [ j + 1 ] ) {
183+ temp = data [ j ] ;
184+ data [ j ] = data [ j + 1 ] ;
185+ data [ j + 1 ] = temp ;
186+ changeBarColor ( data [ j + 1 ] , smallestColor ) ;
187+ var swooshAudio = new Audio ( "./sound-effects/swoosh.mp3" ) ;
188+ swooshAudio . play ( ) ;
189+ swapBar ( data ) ;
190+ await timer ( time ) ;
191+ } else {
192+ changeBarColor ( data [ j + 1 ] , smallestColor ) ;
193+ }
194+ changeBarColor ( data [ j ] , unsortedColor ) ;
195+ }
196+ changeBarColor ( data [ j ] , sortedColor ) ;
197+ }
198+ }
199+
200+ sort ( this ) ;
201+ } ,
202+
203+ bubbleSortStop ( ) {
204+ this . abort = true ;
205+ } ,
206+ } ;
207+
208+ function stopSorting ( ) {
209+ const stopBubbleSort = bubbleS . bubbleSortStop . bind ( bubbleS ) ;
210+ const stopSelectionSort = selectionS . selectionSortStop . bind ( selectionS ) ;
211+
212+ stopBubbleSort ( ) ;
213+ stopSelectionSort ( ) ;
214+ }
215+ function startSorting ( ) {
216+ if ( getAlgo ( ) == "bubble-sort" ) {
217+ const bubbleSortStarted = bubbleS . bubbleSort . bind ( bubbleS ) ;
218+ console . log ( "clicked buble" ) ;
219+ bubbleSortStarted ( ) ;
220+ } else if ( getAlgo ( ) == "selection-sort" ) {
221+ const selectionSortStarted = selectionS . selectionSort . bind ( selectionS ) ;
222+ console . log ( "clicked Selection" ) ;
223+ selectionSortStarted ( ) ;
224+ }
225+ }
226+
227+ document . getElementById ( "sort" ) . addEventListener ( "click" , function ( ) {
228+ startSorting ( ) ;
229+ } ) ;
230+
231+ document . getElementById ( "random-data" ) . addEventListener ( "click" , function ( ) {
232+ stopSorting ( ) ;
233+ svg . remove ( ) ;
234+ var data = randomData ( maxElement , dataRange ) ;
235+ createChart ( data ) ;
236+ } ) ;
237+ /*
105238document.getElementById("sort").addEventListener("click", function () {
106239 if (getAlgo() == "selection-sort") {
107- Sort . selectionSort ( ) ;
240+ selectionS .selectionSort.bind(bubbleS );
108241 } else if (getAlgo() == "bubble-sort") {
109- Sort . bubbleSort ( ) ;
242+ bubbleS .bubbleSort.bind(bubbleS );
110243 }
111244});
112245
@@ -116,3 +249,4 @@ document.getElementById("random-data").addEventListener("click", function () {
116249 svg.remove();
117250 createChart(data);
118251});
252+ */
0 commit comments