並行選擇排序算法的java實現

本人近日將普通的串行選擇排序算法改成了並行算法,在多核處理器上經測試速度大約是普通串行選擇排序算法的1.5倍到2倍。網上並行選擇排序算法相關的介紹基本沒有看到,因此我將其發上來供大家參考,代碼如下

import java.math.BigInteger;

import static java.util.concurrent.locks.LockSupport.park;
import static java.util.concurrent.locks.LockSupport.unpark;

class ComValues
{
    private BigInteger arr[];
    private int nElem;
    private Thread mainThread;

    ComValues (BigInteger arr[], int num, Thread mainThread)
    {
        this.arr = arr;
        this.nElem = num;
        this.mainThread = mainThread;
    }

    public BigInteger[] getArr ()
    {
        return arr;
    }

    public void setArr (BigInteger[] arr)
    {
        this.arr = arr;
    }

    public int getnElem ()
    {
        return nElem;
    }

    public void setnElem (int nElem)
    {
        this.nElem = nElem;
    }

    public Thread getMainThread ()
    {
        return mainThread;
    }

    public void setMainThread (Thread mainThread)
    {
        this.mainThread = mainThread;
    }

}

class SelectSortRightThread extends Thread
{
    Thread thrd;
    private ComValues cvs;
    int max, left;
    int out, in;

    SelectSortRightThread (String name, ComValues cvs)
    {
        this.cvs = cvs;
        thrd = new Thread(this, name);
        thrd.start();
    }

    // This is the entry point for thread.
    public void run ()
    {
        int nElems = cvs.getnElem();
        BigInteger[] a = cvs.getArr();

        try
        {
            left = 0;
            out = nElems - 1;
            do
            {
                max = out; // maxima
                for (in = out - 1; in >= left; in--) // inner loop
                    if (a[in].compareTo(a[max]) > 0) // if max smaller,
                        max = in; // we have a new max
                left++;
                unpark(cvs.getMainThread());
                park();

                out--;
            } while (out >= (nElems % 2 == 0 ? nElems / 2 : nElems / 2 + 1)); // outer loop
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

class ArraySel
{
    private BigInteger[] a; // ref to array a
    private int nElems; // number of data items
    // --------------------------------------------------------------

    public ArraySel (int max) // constructor
    {
        a = new BigInteger[max]; // create the array
        nElems = 0; // no items yet
    }

    // --------------------------------------------------------------
    public void insert (BigInteger value) // put element into array
    {
        a[nElems] = value; // insert it
        nElems++; // increment size
    }

    // --------------------------------------------------------------
    public void display () // displays array contents
    {
        for (int j = 0; j < nElems; j++) // for each element,
            System.out.print(a[j].longValue() + " "); // display it
        System.out.println("");
    }

    // --------------------------------------------------------------
    public void selectionSort ()
    {
        int out, in, min, max, outTh;
        int right = nElems - 1;

        ComValues cvs = new ComValues(a, nElems, Thread.currentThread());
        SelectSortRightThread SSRT = new SelectSortRightThread("ssrt", cvs);

        for (out = 0; out <= nElems / 2 - 1; out++) // outer loop
        {
            min = out; // minimum
            for (in = out + 1; in <= right; in++) // inner loop
                if (a[in].compareTo(a[min]) < 0) // if min greater,
                    min = in; // we have a new min

            park();
            max = SSRT.max;
            outTh = SSRT.out;

            swap(out, min);
            if (!(max == out && outTh == min))
            {
                if (max == out)
                    swap(outTh, min);
                else
                    swap(outTh, max);
            }
            unpark(SSRT.thrd);
            right--;
        } // end for(out)
        //        service.shutdown();
    } // end selectionSort()
    // --------------------------------------------------------------

    private void swap (int one, int two)
    {
        BigInteger temp = a[one];
        a[one] = a[two];
        a[two] = temp;
    }
    // --------------------------------------------------------------
} // end class ArraySel
////////////////////////////////////////////////////////////////

class SelectSortApp
{
    public static void main (String[] args)
    {
        int maxSize = 5000; // array size
        ArraySel arr; // reference to array
        arr = new ArraySel(maxSize); // create the array

        for (int index = 0; index < maxSize; index++)
        {
            long n = (long) (java.lang.Math.random() * (maxSize - 1));
            arr.insert(new BigInteger(Long.toString(n)));               // insert items
        }

        arr.display(); // display items

        arr.selectionSort(); // selection-sort them

        arr.display(); // display them again
    } // end main()
} // end class SelectSortApp
////////////////////////////////////////////////////////////////

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章