How to sort an array in a single loop ? | Java code to sort an array of Strings with the help of 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 main method ; 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 .
import java . util . * ; class Geeks_For_Geeks { public static char [ ] sortArrays ( char [ ] arr ) { int length = arr . length ; for ( int j = 0 ; j < arr . length - 1 ; j ++ ) { int d1 = arr [ j ] ; int d2 = arr [ j + 1 ] ; if ( d1 > d2 ) { char temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } public static void main ( String args [ ] ) { String geeks = " GEEKSFORGEEKS " ; char arr [ ] = geeks . toCharArray ( ) ; System . out . println ( " Original ▁ array : ▁ " + Arrays . toString ( arr ) ) ; arr = sortArrays ( arr ) ; System . out . println ( " Sorted ▁ array : ▁ " + Arrays . toString ( arr ) ) ; } }