About
package set2;
import java.util.*; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.RecursiveAction;
public class Application {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] array = {4,5,1,8,9,2};
System.out.println(Arrays.toString(array));
ForkJoinPool fjk= new ForkJoinPool();
fjk.invoke(new bubblesorttask(array));
System.out.println("sorted array: " + Arrays.toString(array));
}
}
class bubblesorttask extends RecursiveAction{
private int[] a;
private int low;
private int high;
private static final int THRESHOLD = 1000;
public bubblesorttask(int[] array) {
this(array,0,array.length-1);
}
public bubblesorttask(int[] array, int start, int end) {
// TODO Auto-generated constructor stub
this.a=array;
this.high=end;
this.low=start;
}
@Override
protected void compute() {
// TODO Auto-generated method stub
if (high-low <= THRESHOLD) {
bubblesort();
}
else
{
int mid= low + (high-low)/2;
bubblesorttask lefttask= new bubblesorttask(a,low,mid);
bubblesorttask righttask=new bubblesorttask(a, mid+1, high);
invokeAll(lefttask,righttask);
merge(lefttask.a,righttask.a);
}
}
private void merge(int[] leftarray, int[] rightarray) {
// TODO Auto-generated method stub
int i=0,j=0,k=low;
while (i<leftarray.length && j< rightarray.length) {
if(leftarray[i]<= rightarray[i]) {
a[k]= leftarray[i];
i++;
}
else
{
a[k]= rightarray[i];
j++;
}
k++;
}
while (i<leftarray.length) {
a[k]=leftarray[i];
i++;
k++;
}
while ( i<rightarray.length) {
a[k]=rightarray[i];
j++;
k++;
}
}
private void bubblesort() {
// TODO Auto-generated method stub
for (int i= low; i< high;i++) {
for (int j=low;j<= high-i-1;j++) {
if(a[j]>a[j+1]) {
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
}