File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -268,6 +268,39 @@ class algorithmVisualiser{
268268 }
269269 } ,
270270
271+ {
272+ name : "Shell Sort" , algorithm : async ( ) => {
273+ // Define a gap distance.
274+ let gap = Math . floor ( self . dataSet . data . length / 2 ) ;
275+
276+ // Until gap is bigger then zero do elements comparisons and swaps.
277+ while ( gap > 0 ) {
278+ // Go and compare all distant element pairs.
279+ for ( let i = 0 ; i < ( self . dataSet . data . length - gap ) ; i += 1 ) {
280+ let currentIndex = i ;
281+ let gapShiftedIndex = i + gap ;
282+
283+ while ( currentIndex >= 0 ) {
284+ self . dataSet . iteration ( ) ; // Iterate counter
285+ // Compare and swap array elements if needed.
286+ if ( self . dataSet . lessthan ( gapShiftedIndex , currentIndex ) ) {
287+ self . dataSet . swap ( currentIndex , gapShiftedIndex ) ;
288+ }
289+ await self . wait ( self . options . delay ) ; self
290+ gapShiftedIndex = currentIndex ;
291+ currentIndex -= gap ;
292+ }
293+ }
294+
295+ // Shrink the gap.
296+ gap = Math . floor ( gap / 2 ) ;
297+ }
298+
299+ self . dataSet . swapIndicator = [ ] ; // Finished Empty active data
300+ self . dataSet . compIndicator = [ ] ; // Finished Empty active data
301+ }
302+ } ,
303+
271304 ] ;
272305 }
273306}
You can’t perform that action at this time.
0 commit comments