1.冒泡排序
int [] arr = {5,3,6,8,2}; for (int i = 0; i < arr.length-1; i++) { for (int j = 0; j < arr.length-1-i; j++) { if(arr[j]>arr[j+1]){ int temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for (int i : arr) { System.out.println(i); }
2.二分法
public static int getT(int [] a ,int tag){
int first =0;
int end = a.length;
for (int i = 0; i < a.length; i++) {
int temp = (first+end)/2;
if(tag == a[temp]){
return temp;
}
if(tag > a[temp]){
first = temp+1;
}
if(tag < a[temp]){
end = temp-1;
}
}
return 0;
}