How to sort an array in a single loop ? | Java code to sort an array of integers 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 ; 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 main method ; Declaring an integer array of size 11. ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
import java . util . * ; class Geeks_For_Geeks { public static int [ ] sortArrays ( int [ ] arr ) { int length = arr . length ; for ( int j = 0 ; j < length - 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { int temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 99 , 9 , 8 , 7 , 6 , 0 , 5 , 4 , 3 } ; System . out . println ( " Original ▁ array : ▁ " + Arrays . toString ( arr ) ) ; arr = sortArrays ( arr ) ; System . out . println ( " Sorted ▁ array : ▁ " + Arrays . toString ( arr ) ) ; } }