13-02 Java 数组高级算法,Arrays类

冒泡排序

冒泡排序原理

冒泡排序代码:

package cn.itcast_01;

/*
 * 数组排序之冒泡排序:
 *         相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处
 */
public class ArrayDemo {
    public static void main(String[] args) {
        // 定义一个数组
        int[] arr = { 24, 69, 80, 57, 13 };
        System.out.println("排序前:");
        printArray(arr);

        bubbleSort(arr);
        System.out.println("排序后:");
        printArray(arr);
    }
    
    //冒泡排序代码
    /*总共需要比较数组长度-1次,x < arr.length - 1
     *每一次比较完,下一次就会减少一次元素的比较。第一次比较有0个元素不比,第二次有1个元素不比,,,,所以 y < arr.length - 1 - x
     *两两比较,大的往后放
     * */
    public static void bubbleSort(int[] arr){
        for (int x = 0; x < arr.length - 1; x++) {
            for (int y = 0; y < arr.length - 1 - x; y++) {
                if (arr[y] > arr[y + 1]) {
                    int temp = arr[y];
                    arr[y] = arr[y + 1];
                    arr[y + 1] = 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("]");
    }
}

 

选择排序

选择排序原理图

选择排序代码

package cn.itcast_02;

/*
 * 数组排序之选择排序:
 *         从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
 */
public class ArrayDemo {
    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("]");
    }
}

 

 二分查找法

二分查找法原理

 

 二分法的代码实现:

package cn.itcast_04;

/*
 * 查找:
 *         基本查找:数组元素无序(从头找到尾)
 *         二分查找(折半查找):数组元素有序
 * 
 * 分析:
 *         A:定义最大索引,最小索引
 *         B:计算出中间索引
 *         C:拿中间索引的值和要查找的值进行比较
 *             相等:就返回当前的中间索引
 *             不相等:
 *                 大    左边找
 *                 小    右边找
 *         D:重新计算出中间索引
 *             大    左边找
 *                 max = mid - 1;
 *             小    右边找
 *                 min = mid + 1;
 *         E:回到B
 */
public class ArrayDemo {
    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;
            }
            
            //加入判断
            if(min > max){
                return -1;
            }
            
            mid = (max +min)/2;
        }
        
        return mid;
    }
}

Arrays类

package cn.itcast_05;

import java.util.Arrays;

/*
 * Arrays:针对数组进行操作的工具类。比如说排序和查找。
 * 1:public static String toString(int[] a) 把数组转成字符串
 * 2:public static void sort(int[] a) 对数组进行排序
 * 3:public static int binarySearch(int[] a,int key) 二分查找
 */
public class ArraysDemo {
    public static void main(String[] args) {
        // 定义一个数组
        int[] arr = { 24, 69, 80, 57, 13 };

        // 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));
    }
}

 

posted on 2017-05-16 11:24  白杨-M  阅读(332)  评论(0编辑  收藏  举报

导航