zoukankan      html  css  js  c++  java
  • 排序

     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 + "	");
    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);

    */

    结果:

  • 相关阅读:
    查看object信息
    Google C++单元测试框架之宏
    Google C++单元测试框架
    通过iscsi协议使用ceph rbd
    OpenStack+Ceph存储空间回收《转》
    IO
    golang之interface
    mysql 初始化
    ceph之ceph osd tree下的weight, reweight
    c++单元测试框架googletest
  • 原文地址:https://www.cnblogs.com/YouAreABug/p/10030395.html
Copyright © 2011-2022 走看看