zoukankan      html  css  js  c++  java
  • 排序的两种方法(冒泡排序,选择排序)

    // 冒泡排序
    int[] a = { 4, 20, 3, 9, 14, 23, 10, 59 };
    for (int i = 0; i < a.length - 1; i++) {
      for (int j = 0; j < a.length - i - 1; j++) {
        if (a[j] > a[j + 1]) {// 把这里改成大于,就是升序了
          int temp = a[j];
          a[j] = a[j + 1];
          a[j + 1] = temp;
        }
      }
    }
    for (int i = 0; i < a.length; i++) {
      System.out.println(a[i]);
    }
    System.out.println("********************************************************************");
    // 选择排序
    for (int i = 0; i < a.length - 1; i++) {
      // 查找选择最小元素值的下标索引值
      for (int j = i + 1; j < a.length; j++) {
        if (a[j] < a[i]) {// 把这里改成大于,就是升序了
          int temp = a[i];
          a[i] = a[j];
          a[j] = temp;
        }
      }
    }
    for (int c = 0; c < a.length; c++) {
      System.out.println(a[c]);
    }

  • 相关阅读:
    1015词法分析
    0909作业
    华为云专家来公司
    SVN与Git的优点差异比较
    eclipse快捷键大全
    Servlet,HttpServletRequest 和 HttpServletResponse
    XML
    JDBC
    XMIND快捷键
    java数据
  • 原文地址:https://www.cnblogs.com/bd195746/p/5813866.html
Copyright © 2011-2022 走看看