java冒泡排序与二分法;代码如下
/**
* 冒泡排序
*
* @param array
* @return
*/
public static String bubbingSort(int[] array) {
System.out.println(Arrays.toString(array));
for (int i = 0; i < array.length - 1; i++) {//外层循环的次数
System.out.println("外层循环的次数--" + i);
for (int j = 0; j < array.length - 1 - i; j++) {//依次循环的次数
if (array[j + 1] < array[j]) {
Integer a = array[j];
array[j] = array[j + 1];
array[j + 1] = a;
}
System.out.println("内层一次层循环的次数--" + j);
}
}
for (int ar : array) {
System.out.print(ar + ",");
}
return "";
}
/**
* 二分法
* @param s
* @param arry
* @return
*/
public static int dichotomy(int s, int[] arry) {
int start = 0;
int end = arry.length - 1;
while (start <= end) {
int i1 = (end + start) / 2;
if (s < arry[i1]) {
end = i1 - 1;
}else if(s >arry[i1]){
start = i1 + 1;
} else {
return end;
}
}
return -1;
}