首页 > 代码库 > java基础11天

java基础11天

 冒泡排序
相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处,第二次比较厚,最大值放在了倒数第二的位置,一直到第二个元素确定了,整个数组的顺序也就确定了

    public class ArrayDemo {
        public static void main(String[] args) {
            int[] arr = {7,3,6,1,9,4,0};
            System.out.println("排序前");
            method(arr);
            bubbleSort(arr);
            System.out.println("排序后");
            method(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 method(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i]);
            }
            System.out.println();
        }
    }
选择排序
从0索引开始,依次和后面的元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处,依次类推,直到倒数第二个数和最后一个数比完,得出的较大就是倒数第一个位置上的数是所有数中的最大值

    public class ArrayDemo1 {
        public static void main(String[] args) {
            int[] arr ={3,4,8,1,2,5,7,6};
            System.out.println("排序前");
            print(arr);
            selectSort(arr);
            System.out.println("排序后");
            print(arr);
        }public static void selectSort(int[] arr){
            for (int i = 0; i < arr.length-1; i++) {
                for (int j = i+1; j < arr.length; j++) {
                    if(arr[i]>arr[j]){
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
        }
        public static void print(int[] arr){
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i]);
            }
            System.out.println();
        }
    }
总结
两种排序最大的不同是里层循环的开始值和结束值

冒泡排序:内层循环开始值都是0开始,结束值是变化的
选择排序:内层循环的开始值是变化的,结束值是固定的,都是最大索引值。
二分查找(折半查找)
    /*
     * 查找:
     *      基本查找:数组元素无序(从头找到尾)
     *      二分查找(折半查找):数组元素有序
     * 
     * 分析:
     *      A:定义最大索引,最小索引
     *      B:计算出中间索引
     *      C:拿中间索引的值和要查找的值进行比较
     *          相等:就返回当前的中间索引
     *          不相等:
     *              大   左边找
     *              小   右边找
     *      D:重新计算出中间索引
     *          大   左边找
     *              max = mid - 1;
     *          小   右边找
     *              min = mid + 1;
     *      E:回到B
     */
    public class ArrayDemo3 {
        public static void main(String[] args) {
            int[] arr = {3,4,2,8,5,6,1};
            selectSort(arr);
            int res = getIndex(arr, 6);
            System.out.println(res);
        }
        //定义方法,先将数组排列
        public static void selectSort(int[] ch){
            for (int i = 0; i < ch.length-1; i++) {
                for (int j = i+1; j < ch.length; j++) {
                    if (ch[i]>ch[j]) {
                        int temp = ch[i];
                        ch[i] = ch[j];
                        ch[j] = temp;
                    }
                }
            }
        }
        public static int getIndex(int[] arr, int a){
            //定义最大索引、最小索引
            int max = arr.length-1;
            int min = 0;
            //计算出中间索引
            int mid = (min+max)/2;
            while(true){
                if(arr[mid]==a){
                    return mid;
                }else{
                    if(arr[mid]<a){
                        min = mid +1;
                    }else{
                        max = mid -1;
                    }
                    if(min>max){
                        return -1;
                    }
                    mid = (min+max)/2;
                }
            }
        }
    }
Arrays类概述
系统提供针对数组进行的操作的工具类

提供了排序、查找等功能。不用我们手动去写算法,直接拿来就用

静态方法

public static String toString(int[] a):将数组转换成字符串
public static void sort(int[] a):对一个int数组进行升序排序ascending
public static int binarySearch(int[] a,int key):在数组中进行二分查找

java基础11天