冒泡排序
int arr[] = {8, 4, 6, 9, 7, 1, 3, 5, 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;
}
}
}
System.out.println(Arrays.toString(arr));
二分查找
int arr[] = {5,10,15,20,25,30,35,40,45};
System.out.println(“输入的数组为” + Arrays.toString(arr));
System.out.println("输入你想要搜索的数字");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int first = 0;
int last = arr.length - 1;
int mid = (first + last) / 2;
while(first<=last){
if(arr[mid]<num){
first = mid+1;
}else if(arr[mid] == num){
System.out.println("你找到了对应的数字他的下表为"+mid);
break;
}else {
last = mid-1;
}
mid = (first+last)/2;
}
if(first>last){
System.out.println("找不到对应的数字");
}
异或^
当一个数被^两次过后等于0
固因此有运算法则 a ^ a ^b = b