二分查找、归并排序和快速排序

package com.mingde.offer;

/**
 * @author bro
 * @date 2020/9/22 9:23
 * @desc
 */
public class Sort {
    
    
    /**
     * 二分查找
     *
     * @param array
     * @param a
     * @return
     */
    public static int biSearch(int[] array, int a) {
    
    
        int low = 0;
        int high = array.length - 1;
        while (low <= high) {
    
    
            int mid = (low + high) / 2;
            if (array[mid] == a) return mid + 1;
            else if (array[mid] > a) high = mid - 1;
            else low = mid + 1;
        }
        return -1;
    }

    /**
     * 快排
     * https://blog.csdn.net/nrsc272420199/article/details/82587933?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param
     *
     * @param arr
     * @param low
     * @param high
     */
    public static void quickSort(int[] arr, int low, int high) {
    
    
        if (low < high) {
    
    
            int index = getIndex(arr, low, high);
            quickSort(arr, low, index - 1);
            quickSort(arr, index + 1, high);
        }
    }

    public static int getIndex(int[] arr, int low, int high) {
    
    
        int tmp = arr[low]; // 基准数字
        while (low < high) {
    
    
            //1. 队尾元素大于基准元素的时候 high--
            while (low < high && arr[high] > tmp) high--;
            // 如果队尾元素小于tmp了,需要将其赋值给low
            arr[low] = arr[high];
            //2. 队首元素小于基准元素的时候 low++
            while (low < high && arr[low] < tmp) low++;
            // 当队首元素大于tmp时,需要将其赋值给high
            arr[high] = arr[low];
        }
        //tmp赋值给arr[low]
        arr[low] = tmp;
        return low;// 返回tmp的正确位置
    }

    /**
     * 归并排序
     * @param a
     * @param first
     * @param mid
     * @param last
     */
    public static void mergeArray(int a[], int first, int mid, int last) {
    
    
        int temp[] = new int[a.length];        // 临时数组
        int i = first, j = mid + 1; //  j右数组第一个元素索引  n右数组第最后一个元素索引
        int m = mid, n = last;      //  i左数组第一个元素索引  n左数组第最后一个元素索引
        int k = 0;  // k 记录临时数组的索引
        // 从两个数组中取出最小的放入临时数组
        while (i <= m && j <= n) {
    
    
            if (a[i] <= a[j])
                temp[k++] = a[i++];
            else temp[k++] = a[j++];
        }
        // 剩余部分依次放入临时数组(实际上两个while只会执行其中一个)
        while (i <= m) {
    
    
            temp[k++] = a[i++];
        }
        while (j <= m) {
    
    
            temp[k++] = a[j++];
        }
        for (i = 0; i < k; i++) {
    
       // 将临时数组中的内容拷贝回原数组中
            a[first + i] = temp[i];
        }
    }

    static void mergeSort(int a[], int first, int last, int temp[]) {
    
    
        if (first < last) {
    
    
            int mid = (first + last) / 2; // 找出中间索引
            mergeSort(a, first, mid, temp);  // 对左边数组进行递归
            mergeSort(a, mid + 1, last, temp);    // 对右边数组进行递归
            mergeArray(a, first, mid, last);   // 合并
        }
    }

    public static void main(String[] args) {
    
    
        int a[] = {
    
    3, 6, 2, 4, 7};
        int temp[] = new int[a.length];
        mergeSort(a, 0, a.length - 1, temp);
        for (Integer i : a) {
    
    
            System.out.println(i);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Zmd_23/article/details/108740721