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 ; Type Conversion of char to int . ; Comparing the ascii code . ; Swapping of the characters ; Declaring a String ; Declaring a character array to store characters of geeks in it . ; 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 ++ ) { let d1 = arr [ j ] ; let d2 = arr [ j + 1 ] ; if ( d1 > d2 ) { let temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } let geeks = " " ; let arr = geeks . split ( " " ) ; document . write ( " " + arr . join ( " " ) + " " ) ; arr = sortArrays ( arr ) ; document . write ( " " + ( arr ) . join ( " " ) + " " ) ;