- 二分排序
- 冒泡排序
- 判断给定的数组是否已经排序,是返回true,否返回false
- 数组逆序
- 将给定的数组中,偶数放前面,奇数放后面输出。eg:1234 输出:4231
- 判断二进制1的个数
一、二分排序
public static int count = 0;
public static int binarySearch(int[] arr,int key){
int left =0;
int right = arr.length-1;
while(left <= right){
count++;
int mid = (left+right)/2;//相当于无类型替换(left+right)>>>1 无符号右移一位>>>1
if(arr[mid] < key){
left = mid + 1;
}else if(arr[mid]>key){
right = mid-1;
}else{
return mid;
}
}
return -1;
}
public static void main(String[] args) {
int[] array = new int[10000];
for(int i = 0; i < array.length;i++){
array[i]=i;
}
// System.out.println(binarySearch(array,9999));
// System.out.println(count);
System.out.println( Arrays.binarySearch(array,9999));
System.out.println(Arrays.binarySearch(array, 5, 20,19));//[ ) from to
}
二、冒泡排序
//冒泡排序
public static int[] bubbleSort(int[] arr){
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;
flag = true;
}
}
}
return arr;
}
//优化后的冒泡排序
public static int[] bubbleSort(int[] arr){
boolean flag =false;//没有发生交换
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;
flag = true;
}
}
if(flag == false){
break;
}
}
return arr;
}
public static void main(String[] args) {
int[] array = {1,3,5,16,9};
System.out.println(Arrays.toString(bubbleSort(array)));
}
三、判断给定的数组是否已经排序,是返回true,否返回false
public static boolean isUp(int[] arr){
int min = arr[0];
for(int i = 1; i < arr.length; i++){
//1,3,5,16,9
if(min < arr[i]){
min = arr[i];
continue;
}else{
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] array = {1,3,5,16,9};
System.out.println(isUp(array));
System.out.println(isSorted(array));
}
public static boolean isSorted(int[] arr){
for(int i = 0; i < arr.length-1; i++){
if(arr[i] > arr[i+1]){
return false;
}
}
return true;
}
四、将给出的数组逆序输出
//数组逆序 逆序的思想,从两端出发,结束条件是相遇
public static int[] reverse(int[] arr){
int left = 0;
int right = arr.length-1;
while(left < right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
return arr;
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5,6,7};
System.out.println(Arrays.toString(reverse(array)));
}
五、将给定的数组中,偶数放前面,奇数放后面输出。eg:1234 输出:4231
-
//将数组中的偶数放在前面,奇数放在后面 思想同上,从左边和右边出发, // 如果左边遇到奇数停下,右边遇到偶数停下,交换两个数,终止条件是相遇 public static int[] change(int[] arr){ int left = 0; int right = arr.length-1; while(left < right){ while(left < right && arr[left] % 2 == 0){//注意易错点:是arr[left]判断,不是对下标判断 left++;//一直加加不安全,可以下标越界 所以加上条件left < right } while(left < right && arr[right] % 2 != 0){ right--; } if(left < right){//加上这个条件是因为上面循环退出有两种情况 1.left = right 2.遇到奇数 int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } } return arr; } public static void main(String[] args) { int[] array = {1,2,3,4}; System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(change(array))); }
六、判断二进制1的个数
思想:判断每一位是不是1,即让当前数字和1相与
public static void main(String[] args) {//psvm
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int count = 0;
for( int i = 0; i < 32; i++){
if(((n>>i) & 1) == 1) {
count++;
}
}
System.out.println(count);
}
思路:n&(n-1) == 0时,相与的次数就是2进制1的个数
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n =scan.nextInt();
int count = 0;
while(n != 0){
n = n&(n-1);
count++;
}
System.out.println(count);
七、将给出的二进制数的偶数位和奇数位的数字分别输出 eg:1234 输出:4231
import java.util.Arrays;
/**
* @program: sixiang
* @description
* @author: wenwen
* @create: 2021-07-07 10:55
**/
//奇数放后面 偶数放前面 eg:1234 输出 :4231
public class even {
public static int[] evenOdd(int[] arr){
int left = 0;
int right = arr.length - 1;
for(int i = 0; i <= arr.length-1; i++){
while(left < right && arr[left] % 2 ==0){
left++;
}
while(left < right && arr[right] % 2 != 0){
right--;
}
if(left < right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
return arr;
}
public static void main(String[] args) {
int[] array = {1,2,3,4,4,5,6,9};
System.out.println( Arrays.toString(evenOdd(array))); ;
}
}
八、求两个数的最大公约数
/**
* @program: sixiang
* @description
* @author: wenwen
* @create: 2021-07-07 08:22
**/
public class gongYue {
public static int gonYe(int m,int n){
while(m % n != 0){
int temp = m % n;
m = n;
n = temp;
System.out.println(m+" "+n);
}
return n;
}
public static void main(String[] args) {
int m = 12;
int n = 24;
System.out.println(gonYe(12,24));
}
}