使用两个线程快速排序

时间:2017-06-25 22:50:55

标签: java multithreading algorithm quicksort

我正在尝试使用两个线程来处理快速排序中的第一个数组拆分以加快进程。我的运行时间稍微快一些,但通常是相同的运行时间而不使用线程。可能是什么问题呢?我允许使用两个线程的快速排序方法在for循环中运行一次,然后返回一个传递给不使用线程的quicksort方法的数组。

public class Partitioning {
    private static boolean Switch = true;

    public static void quickSort(int[] array) throws InterruptedException{
        quicksort(array, 0, array.length-1);
    }

    private static int pivotG(int[] array, int left, int right){
        int pivot = array[(left+right)/2];
        return pivot;
    }

    private static int indexG(int[] array, int left, int right, int pivot){
        int index = partition(array, left, right, pivot);
        return index;
    }

    private static void quicksort(int[] array, int left, int right) throws InterruptedException {
        if (left>= right){
            return;
        }
        Object lock = new Object();

        if (isSwitch()){
        Thread t1 = new Thread (new Runnable(){
            public void run(){
                int pivot = pivotG (array, left, right);
                int index = indexG(array, left, right, pivot);
                try {
                    quicksort(array, left, index-1);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread (new Runnable(){
            public void run(){
                int pivot = pivotG (array, left, right);
                int index = indexG(array, left, right, pivot);
                try {
                    quicksort(array, index, right);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();
        setSwitch (false);

        }else{
            int pivot = pivotG (array, left, right);
            int index = indexG(array, left, right, pivot);
            quicksort(array, left, index-1);
            quicksort(array, index, right);
        }
    }

    private static int partition(int[] array, int left, int right, int pivot) {
        while (left<=right){
            while (array[left]< pivot){
                left++;
            }
            while (array[right]>pivot){
                right--;
            }
            if (left<=right){
                int temp1 = array[left];
                int temp2 = array[right];
                array[left] = temp2;
                array[right] = temp1;
                left++;
                right--;
            }
        }
        return left; 
    }

    public static void main (String [] args) throws InterruptedException{
        long time1 = System.nanoTime();
        Random ra =new Random();
        int[] list1 = new int[1000];
        for (int i=0; i<1000; i++){
            list1[i] = ra.nextInt(1000);
        }
        quickSort(list1);

        long elapsed = System.nanoTime() - time1;
        long finalSec = (elapsed/1000000 );
        System.out.println("time it took           "+finalSec);
    }

    public static boolean isSwitch() {
        return Switch;
    }

    public static void setSwitch(boolean switch1) {
        Switch = switch1;
    }
}

1 个答案:

答案 0 :(得分:0)

一方面做1000000个printlns会对性能产生严重影响。先结束你的结束时间。另一方面,我不认为您已正确实施快速排序。您应该使用较小的数据集进行确认