• Java冒泡排序


    一、从小到大:

    代码:

      public class DemoThree {
        static double[] sort(double[] nums){
          double temp=0;//临时变量
          //开始排序
          for (int i = 0; i < nums.length-1; i++) {
             for (int j = 0; j < nums.length-i-1; j++) {
              if(nums[j]>nums[j+1]){
                temp=nums[j+1];
                nums[j+1]=nums[j];
                nums[j]=temp;
              }
            }
          }
          return nums;
         }

        public static void main(String[] args) {
          double[] n={1,5,4.5,6.5,1.5,3.0};
          System.out.println("排序前");
          for (double d : n) {
            System.out.print(d+" ");
          }
            System.out.println(" ---------------------");
            n=sort(n);//调用方法进行排序
            System.out.println("排序后");
            for (double d : n) {
                           System.out.print(d+" ");
          }
        }
      }

    结果如图:

      

    二、从大到小

    代码:

      public class DemoThree {
        static double[] sort(double[] nums){
          double temp=0;//临时变量
          //开始排序
          for (int i = 0; i < nums.length-1; i++) {
             for (int j = 0; j < nums.length-i-1; j++) {
              if(nums[j]<nums[j+1]){
                temp=nums[j+1];
                nums[j+1]=nums[j];
                nums[j]=temp;
              }
            }
          }
          return nums;
         }

        public static void main(String[] args) {
          double[] n={1,5,4.5,6.5,1.5,3.0};
          System.out.println("排序前");
          for (double d : n) {
            System.out.print(d+" ");
          }
            System.out.println(" ---------------------");
            n=sort(n);//调用方法进行排序
            System.out.println("排序后");
            for (double d : n) {
                           System.out.print(d+" ");
          }
        }
      }

    结果如图:

      

  • 相关阅读:
    cf1058c 暴力
    cf1058b 点是否在一个矩形里
    cf1058E 思维 前缀处理 位运算
    Codeforces Round #622 (Div. 2)C(单调栈,DP)
    Codeforces Round #623 (Div. 1, based on VK Cup 2019-2020
    Atcoder Beginner Contest 156E(隔板法,组合数学)
    【PAT甲级】1117 Eddington Number (25分)
    【PAT甲级】1116 Come on! Let's C (20分)
    【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)
    Codeforces Round #621 (Div. 1 + Div. 2)E(二分查找,枚举分界点,容斥原理)
  • 原文地址:https://www.cnblogs.com/shiyoushao/p/6082311.html
Copyright © 2020-2023  润新知