Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Concurrency Fork Join排序不正确_Concurrency_Mergesort_Insertion Sort_Fork Join - Fatal编程技术网

Concurrency Fork Join排序不正确

Concurrency Fork Join排序不正确,concurrency,mergesort,insertion-sort,fork-join,Concurrency,Mergesort,Insertion Sort,Fork Join,我正在尝试使用fork-join对合并排序进行自适应。 我将此用作fork/join示例,因此我需要保持它的基本特性。 我想编辑常规合并排序,以便当段大小小于或等于101 100时,它将使用插入排序对该段进行排序,然后退出递归调用并开始将段重新合并在一起。 这种方式根本不起作用。如果我更改invoke和join的顺序以阻止其他代码运行,那么它可以正常工作,因此我假设这是因为我的排序是并发运行的,这是不正确的。 例如,如果我写 int mid=lb+ub/2; MergeInsertSortQ l

我正在尝试使用fork-join对合并排序进行自适应。 我将此用作fork/join示例,因此我需要保持它的基本特性。 我想编辑常规合并排序,以便当段大小小于或等于101 100时,它将使用插入排序对该段进行排序,然后退出递归调用并开始将段重新合并在一起。 这种方式根本不起作用。如果我更改invoke和join的顺序以阻止其他代码运行,那么它可以正常工作,因此我假设这是因为我的排序是并发运行的,这是不正确的。 例如,如果我写 int mid=lb+ub/2; MergeInsertSortQ left=新的MergeInsertSortQf,lb,mid; MergeInsertSortQ right=新的MergeInsertSortQf,mid,ub; 左叉子;左。连接; 右叉;对。加入; mergef、lb、mid、ub; 它排序很好,但这是本质上的顺序,所以不是我真正想要做的

下面是我正在使用的代码,主要包括一个小测试

导入java.util.concurrent.*

公共类MergeInsertSortQ扩展了RecursiveAction{

private int[] f;
private int lb, ub;
public MergeInsertSortQ(int f[], int lb, int ub)
{
    this.f = f;
    this.lb=lb;
    this.ub=ub;
}

protected void compute(){
    //Insertion Sort performed when a segment of size
    //100 or less is reached otherwise do merge sort
    if(ub-lb>100)
    {
        int mid = (lb+ub)/2;
        MergeInsertSortQ left = new MergeInsertSortQ(f,lb,mid);
        MergeInsertSortQ right = new MergeInsertSortQ(f,mid,ub);
        invokeAll(left,right);
        left.join();
        right.join();
        merge(f,lb,mid,ub);
    }
    else if(ub-lb>=1)
    {
        for (int i = lb; i<ub;i++)
        {
            int temp = f[i];
            int j = i-1;
            while (j >= 0 && f[j] > temp)
            {
                f[j+1] = f[j];
                j = j-1;
            }
            f[j+1] = temp;
        }
    }
}

protected void merge(int f[], int lower, int middle, int top){
    int i = lower; int j = middle;

    //use temp array to store merged sub-sequence
    int temp[] = new int[top-lower]; int t = 0;

    while(i < middle && j < top)
    {
        if(f[i] <= f[j])
        {
            temp[t]=f[i];i++;t++;
        }
        else{
            temp[t] = f[j]; j++; t++;
        }
    }

    //tag on remaining sequence
    while(i < middle)
    {
        temp[t]=f[i]; i++; t++;
    }
    while(j < top)
    {
        temp[t] = f[j]; j++; t++;
    }

    //copy temp back to f
    i = lower; t = 0;
    while(t < temp.length)
    {
        f[i] = temp[t]; i++; t++;
    }
}

public static void main(String[] args)
{
    //Initialise & print array before sorting
    int A[]=new int[200];
    for(int j=0;j<A.length;j++)
    {
        A[j]=(int)(Math.random()*10000);
        System.out.print(A[j]+" ");
    }
    System.out.println();
    System.out.println("**********************************");
    System.out.println();

    // Do the sort
    ForkJoinPool fjPool=new ForkJoinPool();
    fjPool.invoke(new MergeInsertSortQ(A,0,A.length));

    //Check if array sorted and print
    boolean sorted=true;
    for(int i=0;i<A.length-1;i++)
    {
        System.out.print(A[i] + " ");
        if (A[i]>A[i+1])
        {
            System.out.println();
            System.out.println(A[i]+" is greater than "+A[i+1]+", location "+i);
            sorted=false;
            //break;
        }
    }
    System.out.println();
    if (sorted) System.out.println("The array is sorted!!!");
    else System.out.println("WARNING: The array is NOT SORTED");
}

}

如果您可以访问JDK1.8,请查看parallel.sort。该方法使用F/J进行排序。你的代码有太多的错误,所以看主代码是最好的建议。我知道了,谢谢。我在插入排序部分有一个bug。在while循环中,我应该有whilej>=lb&&f[j]>temp,而不是whilej>=0&&f[j]>temp