我很困惑,随机快速排序比正常的快速排序慢

时间:2016-05-25 12:16:41

标签: java algorithm

随着算法的介绍。教授介绍随机快速排序可以减少不良情况的百分比,提高时间成本的性能。但我想知道我的代码是否有任何导致结果的问题 相反。 这是我的java代码

public class quickSort {
    public final int size = 200000;
    public final int times = 10;

    @Test
    public void quick_sort() {
        int test1[] = new int[size];
        int test2[];
        int k = 0;

        long t1 = 0;
        long t2 = 0;

        for (int i = 0; i < size; i++) {
            test1[i] = i;
        }
        test2 = test1.clone();
        while (k < times) {

            shuffle(test1);
            shuffle(test2);
             long start1 = System.currentTimeMillis();
             split1(test1, 0, test1.length -1);
             t1 += System.currentTimeMillis() - start1;

            long start2 = System.currentTimeMillis();
            split2(test2, 0, test2.length -1);
            t2 += System.currentTimeMillis() - start2;
            k++;
        }

        System.out.println("normal quick sort time is" + t1 + "ms");
        System.out.println("random quick sort time is " + t2 + "ms");
    }

    public void split1(int[] arr, int start, int end) {
        if (start >= end) {
            return;
        } else {
            int middle = sort(arr, start, end);
            split1(arr, start, middle - 1);
            split1(arr, middle + 1, end);
        }
    }

    public void split2(int[] arr, int start, int end) {
        if (start >= end) {
            return;
        } else {
            int middle = random_sort(arr, start, end);
            split2(arr, start, middle - 1);
            split2(arr, middle + 1, end);
        }
    }

    // random quick sort
    public int random_sort(int[] arr, int start, int end) {

        int random = start + new Random().nextInt(end - start + 1);
        int key = arr[random];
        int loop = start;
        int front = random == start ? start + 1 : start;

        while (loop <= end) {
            if (loop == random) {
                loop++;
                continue;
            }
            if (arr[loop] < key) {
                int temp = arr[loop];
                arr[loop] = arr[front];
                arr[front] = temp;
                front = front + 1 == random ? front += 2 : front + 1;
            }
            loop++;
        }
        if (front < end) {

            int temp = arr[random];
            arr[random] = arr[front];
            arr[front] = temp;
        } else {
            front = end;
        }

        return front;
    }

    // normal quick sort
    public int sort(int[] arr, int start, int end) {
        int loop = start;

        int key = arr[start];
        int front = start + 1;
        while (loop <= end) {

            if (key > arr[loop]) {
                int temp = arr[loop];
                arr[loop] = arr[front];
                arr[front++] = temp;
            }

            loop++;
        }
        if (front != 1) {
            int temp = arr[front - 1];
            arr[front - 1] = arr[start];
            arr[start] = temp;
        }
        return front - 1;
    }

    // shuffle the Array
    public void shuffle(int[] arr) {
        int length = arr.length;
        int random_num = 0;
        for (int i = 0; i < length; i++) {
             random_num = new Random().nextInt(length);
            int temp = arr[i];
            arr[i] = arr[random_num];
            arr[random_num] = temp;
        }
    }

}

0 个答案:

没有答案