Java : Sort integer array without using Arrays.sort()

后端 未结 12 2627
滥情空心
滥情空心 2021-02-14 00:59

This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I \'do my homework\' and I\'m not just being lazy asking someon

12条回答
  •  别那么骄傲
    2021-02-14 01:26

    Simple way :

    int a[]={6,2,5,1};
                System.out.println(Arrays.toString(a));
                 int temp;
                 for(int i=0;i a[j+1]){   // use < for Descending order
                             temp = a[j+1];
                             a[j+1] = a[j];
                             a[j]=temp;
                         }
                     }
                 }
                System.out.println(Arrays.toString(a));
    
        Output:
        [6, 2, 5, 1]
        [1, 2, 5, 6]
    

提交回复
热议问题