java中的Arrays工具類及排序和查找


  • java.util.Arrays
    1、Arrays是一個工具類。其中有一個sort()方法,可以排序。靜態方法,直接使用類名調用就行。
    2、代碼示例:
import java.util.Arrays;

public class Demo{
    public static void main(String[] args) {
        int[] a = {6,7,11,43,5};
        Arrays.sort(a);
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}

輸出:
在這里插入圖片描述

  • 冒泡排序
    1、圖例:
    在這里插入圖片描述
    2、簡言之就是:
    先把最大的往最右邊挪,然后就可以忽略掉最右邊的;再把第二大的往最右邊挪,以此類推...每一次循環結束之后,都要找出最大的數據,放到最右邊。
    3、代碼示例:
public class Demo{
    public static void main(String[] args) {
        int[] a = {9, 6, 4, 3, 1};
        /*設置比較次數,
        這里的比較次數也是交換次數*/
        int count = 0;
        for (int i = a.length-1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (a[j] > a[j+1]){
                    int temp;
                    temp = a[j+1];
                    a[j+1] = a[j];
                    a[j] = temp;
                    count++;
                }
            }
        }
        System.out.println("比較次數為:" + count);
        for (int k = 0; k < a.length; k++) {
            System.out.println(a[k]);
        }
    }
}

輸出:
在這里插入圖片描述

  • 選擇排序
    1、選擇排序:
    每一次從這堆參與比較的數據當中找出最小值。拿着這個最小值和最前面的元素交換位置。選擇排序比冒泡排序好在:每一次的交換位置都是有意義的
    2、所以選擇排序比冒泡排序的效率高,高在交換位置的次數上。
    3、代碼示例:
public class Demo{
    public static void main(String[] args) {

        int[] a = {9,6,4,3,1};
        int count=0;
        int count2=0;

        for (int i = 0; i < a.length-1; i++) {
            int min = i;
            for (int j = i+1; j < a.length; j++) {
                count++;
                if (a[min] > a[j]){
                    min = j;
                }
            }
            if (min != i) {
                int temp;
                temp = a[min];
                a[min] = a[i];
                a[i] = temp;
                count2++;
            }
        }
        System.out.println("比較次數:" + count);
        System.out.println("交換次數:" + count2);
        for (int k = 0; k < a.length; k++) {
            System.out.println(a[k]);
        }

    }
}

輸出:
在這里插入圖片描述

  • 二分法查找
    1、數組的元素查找數組元素查找有兩種方式:
    第一種方式:一個一個挨着找,直到找到為止。
    第二種方式:二分法查找(算法),這個效率較高。
    2、二分法查找算法是基於排序的基礎之上。(沒有排序的數據是無法查找的。)
    3、二分法查找的終止條件:一直折半,直到中間的那個元素恰好是被查找的元素。
    4、代碼示例:
public class Demo{
    public static void main(String[] args) {
        int[] a = {1,3,4,7,12,34,45,56};
        int index = binarySearch1(a,12);
        System.out.println(index == -1? "該元素不存在" : "下標為" + index);
        int index2 = binarySearch1(a,88);
        System.out.println(index2 == -1? "該元素不存在" : "下標為" + index2);

    }

    private static int binarySearch1(int[] a, int dest) {
        int begin = 0;
        int end = a.length-1;
       /* 開始元素的下標只要在結束元素下標的左邊,
       就有機會繼續循環。*/
        while (begin <= end){
            int mid = (begin + end)/2;
            if (a[mid] == dest){
                return mid;
            }else if (a[mid] < dest){
                begin = mid + 1;//一直增
            }else {
                end = mid - 1;//一直減
            }
        }
        return -1;
    }

}

輸出:
在這里插入圖片描述

  • java.util.Arrays之binarySearch方法
    代碼示例:
import java.util.Arrays;

import static java.util.Arrays.binarySearch;

public class DemoTest{
    public static void main(String[] args) {
        int[] a = {32,12,4,56,2,78,45,92,3};
        //先排序
        Arrays.sort(a);
        //SUN公司寫的二分法查找:binarySearch方法
        int index = binarySearch(a,12);
        /*返回值應為:-(a.length+1)。
        因為源碼中binarySearch方法返回值是:
        return -(low + 1); // key not found.
        */
        System.out.println(index == -(a.length+1)? "該元素不存在" : "下標為" + index);
        int index1 = binarySearch(a,99);
        System.out.println(index1 == -(a.length+1)? "該元素不存在" : "下標為" + index1);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM