排序

 1 public class TestSort{
 2 
 3     public static void main(String[] args){
 4 
 5     int nums[] = {88,65,77,90,68};
 6 
 7     int temp = 0;
 8 
 9     //重复找若干个数中的最大值
10     //循环一次,找一次最大值
11     //循环的次数是要排序的数的个数减一
12     for(int i = 1; i < nums.length; i++){
13         //找最大值就是进行重复的比较工作
14         //内循环一次,则进行一次比较    
15         //内循环的次数跟本次找最大值的个数有关
16         for(int j = 0; j < nums.length - i; j++){
17         //两个相邻的数相互比较
18         if(nums[j] > nums[j + 1]){
19             //交换位置
20             temp = nums[j];    
21             nums[j] = nums[j + 1];
22                 nums[j + 1] = temp;
23         }
24         }
25     }
26 
27     for(int num : nums){
28         System.out.print(num + "\t");
29     }
30 
31     }



 

 /*
       在java有内置的排序/升序
    */
   
int[] ints = new int[]{12, 345, 567, 45, 04, 34, 2323, 434};
Arrays.sort(ints);
for (int anInt : ints) {
    System.out.println(anInt);
}
 
/*
    降序/反序
*/

 String[] s = new String[]{"b", "c", "a", "e", "s", "a"};
//加上Collections.reverseOreder()就是降序 Arrays.sort(s, Collections.reverseOrder());
for (String anInt : s) { System.out.println(anInt); }


/*
  jdk 8的语法
  
    String[] s = new String[]{"b", "c", "a", "e", "s", "a"};
 
  
    Arrays.stream(s).sorted().toArray(String[]:: new);

*/

 

 

结果:

转载于:https://www.cnblogs.com/YouAreABug/p/10030395.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值