text: Difference between Insertion sort and Selection sort | Function to implement the selection sort ; One by one move boundary of unsorted subarray ; Find the minimum element in unsorted array ; Swap the found minimum element with the first element ; Function to print an array ; Driver Code ; Function Call ; Print the array
code: function selectionSort ( arr , n ) { let i , j , min_idx ; for ( i = 0 ; i < n - 1 ; i ++ ) { min_idx = i ; for ( j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] < arr [ min_idx ] ) min_idx = j ; let temp = arr [ min_idx ] ; arr [ min_idx ] = arr [ i ] ; arr [ i ] = temp ; } } function printArray ( arr , size ) { let i ; for ( i = 0 ; i < size ; i ++ ) { document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let arr = [ 64 , 25 , 12 , 22 , 11 ] ; let n = arr . length ; selectionSort ( arr , n ) ; document . write ( " " ) ; printArray ( arr , n ) ;