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);
    }
}
原文地址:https://www.cnblogs.com/yu011/p/12632761.html