排序的两种方法(冒泡排序,选择排序)

// 冒泡排序
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]);
}

原文地址:https://www.cnblogs.com/bd195746/p/5813866.html