数组冒泡、有序、二分查找
import java.util.Arrays;
public class SHUZU11 {
public static void main(String[] args) {
int [] array={9,8,6,3,2,1};
jiOuPaiXu(array);
System.out.println(Arrays.toString(array));
}
public static void jiOuPaiXu(int [] array){
int left = 0;
int right =array.length-1;
while(left<right){
if (left<right && array[left]%2==0){
left++;
}
if (left<right && array[right]%2!=0){
right--;
}
if (left<right) {
int tmp = array[left];
array[left] = array[right];
array[right] = tmp;
}
}
}
public static void main6(String[] args) {
int [] array={9,8,6,3,2,1};
int [] a=copyOf(array);
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(a));
}
public static int[] copyOf(int [] array){
int [] array1=new int[array.length];
for (int i = 0; i < array.length; i++) {
array1[i]=array[i];
}
return array1;
}
public static void main1(String[] args) {
int [] array={9,8,6,3,2,1};
maoPao(array);
System.out.println(Arrays.toString(array));
}
public static void maoPao(int[] array){
boolean bool=false;
int tmp=0;
for (int i = 0; i < array.length-1; i++) {
bool =false;
for (int j = 0; j <array.length-1-i ; j++) {
if(array[j]>array[j+1]){
tmp=array[j];
array[j]=array[j+1];
array[j+1]= tmp;
bool =true;
}
}
if (bool==false){
break;
}
}
}
public static void main2(String[] args) {
int a[] ={1,2,4,5};
System.out.println(paiXu(a));
System.out.println(paiXu1(a));
}
public static boolean paiXu1(int [] arrsy) {
boolean bool = false;
for (int i = 0; i < arrsy.length-1; i++) {
if (arrsy[i] < arrsy[i + 1]) {
bool =true;
}else{
bool =false;
break;
}
}
return bool;
}
public static boolean paiXu(int [] arrsy) {
for (int i = 0; i < arrsy.length-1; i++) {
if (arrsy[i] > arrsy[i + 1]) {
return false;
}
}
return true;
}
public static void main4(String[] args) {
int [] array={1,3,9,11,17,22,30};
int num =erFen(array, 17);
System.out.println("该数字在数组中的位置下标为:"+num);
}
public static int erFen(int[] array,int key){
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (key < array[mid]) {
right = mid - 1;
} else if (key > array[mid]) {
left = mid + 1;
} else {
return mid;
}
}
return -1;
}
}