Common Java class - Array sorting and binary search

An array (Arrays,) class

1, the array (Arrays) class

  • An array of tools for operations
  • It provides the ability to sort of find

2, members of the method

  • public static String toString(int[] a)
  • public static void sort(int[] a)
  • public static int binarySearch(int[] a,int key)

Second, sort the array

1, bubble sort

  • Any two adjacent elements, the next big release, for the first time is completed, the maximum in the biggest index
    Here Insert Picture Description

An array of bubble sort of code:

package shuzu;

public class ArrayDm {
    public static void main(String[] args){
        int[] arr = {90,12,54,3,76,15};//定义一个数组
        System.out.println("数组排序前:");
        printArray(arr);

        bubbleSort(arr);
        System.out.println("排序后:");
        printArray(arr);
    }

    public static void 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;
                 }
             }
         }
    }

    public static void printArray(int[] arr) {
        System.out.println("[");
        for (int i = 0; i < arr.length; i++){
            if(i == arr.length - 1){
                System.out.println(arr[i]);
            }else{
                System.out.println(arr[i] + ",");
            }
        }
    }
}

2, the string of characters to sort

package shuzu;

public class ArrayDm {
    public static void main(String[] args) {
        String s = "fdeacbg";//定义一个字符串
        char[] arr = s.toCharArray();//把字符串转换成数组
        paixu(arr);//把字符数组进行排序
        String result = String.valueOf(arr);//把排序后的字符数组转成字符串
        System.out.println(result);//输出字符串;
    }

    private static void paixu(char[] 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]) {
                    char temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

3. Select the sort

  • From 0 index, and turn back the elements of relatively small put forward, for the first time completed the minimum in the minimum index

Here Insert Picture Description

package shuzu;

public class ArrayDm {
    public static void main(String[] args) {
        // 定义一个数组
        int[] arr = { 24, 69, 80, 57, 13 };
        System.out.println("排序前:");
        printArray(arr);

        //用方法改进
        selectSort(arr);
        System.out.println("排序后:");
        printArray(arr);

    }

    public static void selectSort(int[] arr){
        for(int x=0; x<arr.length-1; x++){
            for(int y=x+1; y<arr.length; y++){
                if(arr[y] <arr[x]){
                    int temp = arr[x];
                    arr[x] = arr[y];
                    arr[y] = temp;
                }
            }
        }
    }

    // 遍历功能
    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int x = 0; x < arr.length; x++) {
            if (x == arr.length - 1) {
                System.out.print(arr[x]);
            } else {
                System.out.print(arr[x] + ", ");
            }
        }
        System.out.println("]");
    }
}

Third, the array search

1, binary search
Here Insert Picture Description

package shuzu;

public class ArrayDm {
    public static void main(String[] args) {
        //定义一个数组
        int[] arr = {11, 22, 33, 44, 55, 66, 77};

        //写功能实现
        int index = getIndex(arr, 33);
        System.out.println("index:" + index);

        //假如这个元素不存在后有什么现象呢?
        index = getIndex(arr, 333);
        System.out.println("index:" + index);
    }

    /*
     * 两个明确:
     * 返回值类型:int
     * 参数列表:int[] arr,int value
     */
    public static int getIndex(int[] arr, int value) {
        //定义最大索引,最小索引
        int max = arr.length - 1;
        int min = 0;

        //计算出中间索引
        int mid = (max + min) / 2;

        //拿中间索引的值和要查找的值进行比较
        while (arr[mid] != value) {
            if (arr[mid] > value) {
                max = mid - 1;
            } else if (arr[mid] < value) {
                min = mid + 1;
            }

            //加入判断,返回-1说明找不到
            if (min > max) {
                return -1;
            }

            mid = (max + min) / 2;
        }

        return mid;
    }
}

Directly call Java binary search tools

package shuzu;

import java.util.Arrays;//直接调用Java工具类

public class ArrayDm {
    public static void main(String[] args) {
        // 定义一个数组
        int[] arr = { 24, 69, 80, 57, 13 };

        //System.out.println(Arrays.toString(arr));

        // public static String toString(int[] a) 把数组转成字符串
        System.out.println("排序前:" + Arrays.toString(arr));

        // public static void sort(int[] a) 对数组进行排序
        Arrays.sort(arr);
        System.out.println("排序后:" + Arrays.toString(arr));

        // [13, 24, 57, 69, 80]
        // public static int binarySearch(int[] a,int key) 二分查找
        System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57));
        System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
    }
}

Guess you like

Origin blog.csdn.net/weixin_43860260/article/details/91353260