Write a Program to sort an integer array using single loop

public class Sort{
     public static void main(String []args){
        int []a = new int [] {10,5,8,11,4,3,2,7}; 
        for(int i = 0 ; i<a.length-1 ; i++) 
        {
            if(a[i] < a[i+1])
            {
               int temp = a[i];
               a[i] = a[i+1];
               a[i+1] = temp;
               i = -1; 
            }
            
        }
        System.out.println("Sorting an array in descending order");
        for(int k = 0 ; k<a.length ; k++)
        {
        System.out.print(a[k] + " ");
        }
     }
}

Output: –

Sorting an array in descending order
11 10 8 7 5 4 3 2

Leave a Reply

Your email address will not be published. Required fields are marked *