How to sort an array in a single loop ? | Function for Sorting the array using a single loop ; Finding the length of array ' arr ' ; Sorting using a single loop ; Checking the condition for two simultaneous elements of the array ; Swapping the elements . ; updating the value of j = - 1 so after getting updated for j ++ in the loop it becomes 0 and the loop begins from the start . ; Declaring an integer array of size 11. ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
function sortArrays ( arr ) { let length = arr . length ; for ( let j = 0 ; j < length - 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { let temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } let arr = [ 1 , 2 , 99 , 9 , 8 , 7 , 6 , 0 , 5 , 4 , 3 ] ; document . write ( " " + ( arr ) . join ( " " ) + " " ) ; arr = sortArrays ( arr ) ; document . write ( " " + arr . join ( " " ) + " " ) ;