快速排序java並行實現

來源:互聯網
上載者:User


public class And extends Thread{

private int[] A;
private int first;
private int end;

public And(int[] A,int first,int end){
super();
this.A=A;
this.first=first;
this.end=end;
}

public void run(){
quickSort(A,first,end);
}

public void quickSort(int[] A, int first, int end)
    {
        int i = first, j = end;
        int tmp;
        if (first < end)
        {
            tmp = A[first];
            while (i != j)
            {
                while (j > i && A[j] >= tmp)
                    j--;
                A[i] = A[j];
                while (i < j && A[i] <= tmp)
                    i++;
                A[j] = A[i];
            }
            A[i] = tmp;
            quickSort(A, first, i - 1);
            quickSort(A, i + 1, end);
        }
    }

public void merge(int MAX, int[] a, int[] b, int[] c)//將b[]和c[]進行歸併排序,放入a[]數組裡面
    {
        int m = 0, n = 0, k;
        for (k = 0; k < MAX; k++)
        {
            if (m < MAX / 2 && n < MAX / 2)
            {
                if (b[n] <= c[m])
                {
                    a[k] = b[n];
                    n++;
                }
                else
                {
                    a[k] = c[m];
                    m++;
                }
            }
            if (m == MAX / 2 || n == MAX / 2)
            {
                if (m == MAX / 2)
                    a[k] = c[m - 1];
                else
                    a[k] = b[n - 1];
                k += 1;
                break;
            }
        }
        if (n < MAX / 2)
        {
            int tem = MAX / 2 - n;
            for (int p = 0; p < tem; p++)
            {
                a[k] = b[n];
                n++;
                k++;
            }
        }
        else if (m < MAX / 2)
        {
            int tem = MAX / 2 - m;
            for (int q = 0; q < tem; q++)
            {
                a[k] = c[m];
                m++;
                k++;
            }
        }
    }
public static int MAX=70000;
public static int MAXN=MAX/2;
public static void main(String[] args) throws InterruptedException {


int buf[]=new int[MAX+2];
int a[]=new int[MAX+2];
int b[]=new int[MAXN+2];
int c[]=new int[MAXN+2];
for(int i=0;i<MAX;i++){
buf[i]=(int)(Math.random()*10+1);
a[i]=buf[i];
//System.out.print(buf[i]+" ");
}
System.out.println();
for(int i=0;i<MAXN;i++){
b[i]=a[i];
c[i]=a[i+MAXN];
}
And thread1=new And(b,0,MAXN-1);
And thread2=new And(c,0,MAXN-1);
long startTime=System.currentTimeMillis();
thread1.start();
thread2.start();
thread1.join();
thread2.join();
And and=new And(a,0,MAX-1);
and.merge(MAX, a, b, c);
long endTime=System.currentTimeMillis();
//for(int i=0;i<MAX;i++){
//System.out.print(a[i]+" ");
//}
System.out.println();
long time1=endTime-startTime;
System.out.println("並行時間="+time1);

startTime=System.currentTimeMillis();
And serial=new And(buf,0,MAX-1);
serial.quickSort(buf,0,MAX-1);
//for(int i=0;i<MAX;i++){
//System.out.print(buf[i]+" ");
//}
System.out.println();
endTime=System.currentTimeMillis();
//System.out.println("串列結果="+sum);
long time2=endTime-startTime;
System.out.println("串列時間="+time2);
System.out.printf("加速比為%.2f",(time2*1.0/time1));
}


}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.