快排中partition函数-java

来源:互联网 发布:飞机升阻比知乎 编辑:程序博客网 时间:2024/05/22 03:18

快排中partition函数非常有用, 其两种实现很难在java这种有jvm干预的情况下测出哪个更优秀, 不知道是不是测试手段比较low? 总的来说, partition2交换的次数少一下. 对算法的理解也没到化入骨髓的地步, 先备注一下. 要是有大神指点就好了.

package com.test;import java.util.Random;public class PartitionHolder {    static int partition1(int[] arr, int start, int end, int pivotIndex) {        int pivot = arr[pivotIndex];        swap(arr, pivotIndex, end);        int storeIndex = start;        for (int i = start; i < end; ++i) {            if(arr[i] < pivot) {                swap(arr, i, storeIndex);                ++storeIndex;            }        }        swap(arr, storeIndex, end);        return storeIndex;    }    static int partition2(int[] arr, int start, int end, int pivotIndex) {        int pivot = arr[pivotIndex];        swap(arr, pivotIndex, end);        int i = start, j = end;        while (i < j) {            while(i < j && arr[i] <= pivot) ++i;            while(i < j && arr[j] >= pivot) --j;            if(i < j) swap(arr, i, j);        }        swap(arr, i, end);        return i;    }    static void qsort1(int[] arr, int start, int end) {        if (start < end)        {            int p = partition1(arr, start, end, end);            qsort1(arr, start, p - 1);            qsort1(arr, p + 1, end);        }    }    static void qsort2(int[] arr, int start, int end) {        if (start < end)        {            int p = partition2(arr, start, end, end);            qsort2(arr, start, p - 1);            qsort2(arr, p + 1, end);        }    }    static void swap(int[] arr, int from, int to) {        int temp = arr[from];        arr[from] = arr[to];        arr[to] = temp;        ++count;    }    public static void main(String[] args) {//        int[] arr = new int[] {7, 9, 4, 6, 10, 2, 8, 5, 1};//        int[] arr = new int[] {8, 1, 3, 9, 2, 4, 1, 9, 8, 7, 5};        Random random = new Random();        int[] arr = createRandomArray(random, 100);        for (int i = 0; i < 50; ++i) {            int[] copy = new int[100];            System.arraycopy(arr, 0, copy, 0, 100);            count = 0;            start = System.nanoTime();            qsort1(copy, 0, copy.length - 1);            System.out.println("count: " + count + ", useTime: " + (System.nanoTime() - start));        }        for (int i = 0; i < 50; ++i) {            int[] copy = new int[100];            System.arraycopy(arr, 0, copy, 0, 100);            count = 0;            start = System.nanoTime();            qsort2(copy, 0, copy.length - 1);            System.out.println("count: " + count + ", useTime: " + (System.nanoTime() - start));        }    }    private static int count = 0;    private static long start = 0L;    private static int[] createRandomArray(Random random, int size) {        int[] arr = new int[size];        for (int i = 0; i < size; ++i) {            arr[i] = random.nextInt(size);        }        return arr;    }}
0 0
原创粉丝点击