Add elements in start to sort the array | Variation of Stalin Sort | Function to sort the array ; Iterator < Integer > index = arr . iterator ( ) ; ; Driver Code ; Function call
function variationStalinsort ( arr ) { let j = 0 ; while ( true ) { let moved = 0 ; for ( let i = 0 ; i < ( arr . length - 1 - j ) ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { let index ; let temp ; index = arr [ i ] ; temp = arr [ i + 1 ] ; arr . splice ( i , 1 ) ; arr . splice ( i , 0 , temp ) ; arr [ i ] = temp ; arr . splice ( i + 1 , 1 ) ; arr . splice ( i + 1 , 0 , index ) arr [ i + 1 ] = index ; moved ++ ; } } j ++ ; if ( moved == 0 ) { break ; } } document . write ( " " + arr + " " ) ; } let arr = [ 2 , 1 , 4 , 3 , 6 , 5 , 8 , 7 , 10 , 9 ] ; let arr1 = [ ] ; for ( let i = 0 ; i < arr . length ; i ++ ) arr1 . push ( arr [ i ] ) ; variationStalinsort ( arr1 ) ;