利用荷兰国旗问题改进经典快排和随机快排

利用荷兰国旗问题改进经典快排和随机快排

1、经典快排思想

每次取数组中最后一个值,依照这个值把数组分为两份,小于的在左边,大于的在右边。再依次按照这样的思想进行操作。

2、荷兰国旗问题对快排进行改良

ps:荷兰国旗问题可以看另一篇博客

荷兰国旗问题把数组是分为三个部分的,小于、等于、大于这三个部分。按照这样的思想,等于部分就不需要进行再次进行排序,这样就能减少很大一部分的开销。

3、代码实现


/**
 * 改进之后的经典快排和随机快排。
 * @author tyeerth
 * @date 2020/10/26 - 20:00
 */
public class QuickSort {
    
    
    public static void quickSort(int[] arr) {
    
    
        if (arr == null || arr.length < 2) {
    
    
            return;
        }
        quickSort(arr, 0, arr.length - 1);
    }

    public static void quickSort(int[] arr, int l, int r) {
    
    
        if (l < r) {
    
    
            //加上下面这一行之后就是随机快排。
            //随机选一个数作为划分
            swap(arr, l + (int) (Math.random() * (r - l + 1)), r);
            int[] p = partition(arr, l, r);

            //p[0]和p[1]之间的数是中间值,等于arr[r],不用进行操作。

            //左边的数继续进行快排。
            quickSort(arr, l, p[0] - 1);
            //右边的数继续进行快排。
            quickSort(arr, p[1] + 1, r);
        }
    }

    //荷兰国旗问题。以r位置上的数作为判断。把数组最后的一个数作为划分的中间值。
    //                          l到r之间变有序
    public static int[] partition(int[] arr, int l, int r) {
    
    
        int less = l - 1;
        int more = r;
        while (l < more) {
    
    
            if (arr[l] < arr[r]) {
    
    
                //当前数往后移动一位,小于区域加以一,交换。
                swap(arr, ++less, l++);
            } else if (arr[l] > arr[r]) {
    
    
                //当前数不移动,大于区域往前移动一位。加一。
                swap(arr, --more, l);
            } else {
    
    
                l++;
            }
        }
        //使得r位置上的数在中间。

        swap(arr, more, r);
        return new int[] {
    
     less + 1, more };
    }

    public static void swap(int[] arr, int i, int j) {
    
    
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    // for test
    public static void comparator(int[] arr) {
    
    
        Arrays.sort(arr);
    }

    // for test
    public static int[] generateRandomArray(int maxSize, int maxValue) {
    
    
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
    
    
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }

    // for test
    public static int[] copyArray(int[] arr) {
    
    
        if (arr == null) {
    
    
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
    
    
            res[i] = arr[i];
        }
        return res;
    }

    // for test
    public static boolean isEqual(int[] arr1, int[] arr2) {
    
    
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
    
    
            return false;
        }
        if (arr1 == null && arr2 == null) {
    
    
            return true;
        }
        if (arr1.length != arr2.length) {
    
    
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
    
    
            if (arr1[i] != arr2[i]) {
    
    
                return false;
            }
        }
        return true;
    }

    // for test
    public static void printArray(int[] arr) {
    
    
        if (arr == null) {
    
    
            return;
        }
        for (int i = 0; i < arr.length; i++) {
    
    
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // for test
    public static void main(String[] args) {
    
    
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        for (int i = 0; i < testTime; i++) {
    
    
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            int[] arr2 = copyArray(arr1);
            quickSort(arr1);
            comparator(arr2);
            if (!isEqual(arr1, arr2)) {
    
    
                succeed = false;
                printArray(arr1);
                printArray(arr2);
                break;
            }
        }
        System.out.println(succeed ? "Nice!" : "Fucking fucked!");

        int[] arr = generateRandomArray(maxSize, maxValue);
        printArray(arr);
        quickSort(arr);
        printArray(arr);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_45372719/article/details/109297504