Stable Selection Sort | Javascript program for modifying Selection Sort so that it becomes stable . ; Iterate through array elements ; Find minimum element from arr [ i ] to arr [ n - 1 ] . ; Move minimum element at current i . ; driver function
function stableSelectionSort ( a , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) { let min = i ; for ( let j = i + 1 ; j < n ; j ++ ) if ( a [ min ] > a [ j ] ) min = j ; let key = a [ min ] ; while ( min > i ) { a [ min ] = a [ min - 1 ] ; min -- ; } a [ i ] = key ; } } function prletArray ( a , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( " " ) ; } let a = [ 4 , 5 , 3 , 2 , 4 , 1 ] ; let n = a . length ; stableSelectionSort ( a , n ) ; prletArray ( a , n ) ;