Stable sort for descending order | Bubble sort implementation to sort elements in descending order . ; Sorts a [ ] in descending order using bubble sort . ; Driver code
function print ( a , n ) { for ( let i = 0 ; i <= n ; i ++ ) document . write ( a [ i ] + " " ) ; document . write ( " " ) ; } function sort ( a , n ) { for ( let i = n ; i >= 0 ; i -- ) for ( let j = n ; j > n - i ; j -- ) if ( a [ j ] > a [ j - 1 ] ) { let tempswap = a [ j ] ; a [ j ] = a [ j - 1 ] ; a [ j - 1 ] = tempswap ; } print ( a , n ) ; } let n = 6 ; let a = [ ] ; a . push ( 2 ) ; a . push ( 4 ) ; a . push ( 3 ) ; a . push ( 2 ) ; a . push ( 4 ) ; a . push ( 5 ) ; a . push ( 3 ) ; sort ( a , n ) ;