本文共 637 字,大约阅读时间需要 2 分钟。
a) 冒泡
public static void mp(int a[]) { int swap = 0; for (int i = 0; i < a.length; i++) { for (int j = i; j < a.length; j++) { if (a[j] > a[i]) { swap = a[i]; a[i] = a[j]; a[j] = swap; } }}System.out.println(Arrays.toString(a));}
b)二分查找
public static int ef(int a[], int tag) { int first = 0; int end = a.length; for (int i = 0; i < a.length; i++) { int middle = (first + end) / 2; if (tag == a[middle]) { return middle; } if (tag > a[middle]) { first = middle + 1; } if (tag < a[middle]) { end = middle - 1; } }return 0;}
转载地址:http://yqtbx.baihongyu.com/