I have implemented quicksort using multithreading, it is sorting the array of integers correctly but however it is taking longer than a normal quicksort to execute. For e.g to sort 10000 integers Multithreading: 6856 ms Normal: 1 ms
I do not know what is wrong in my code.
public void run()
{
quickSort1(this.Array,this.low,this.high);
}
int partition(int[]A,int l,int r)
{
int v = A[r];
int i = l;
int j = r;
while(i<j)
{
while(A[i]<v)
{
i++;
}
while((i<j)&&(A[j]>=v))
{
j--;
}
if(i<j)
{
int C = A[i];
A[i] = A[j];
A[j] = C;
}
else
{
int D = A[i];
A[i] = A[r];
A[r] = D;
}
}
return i;
}
void quickSort1(int[] A,int l,int r)
{
int i;
if(r>l)
{
i = partition(A,l,r);
quickSort sort1 = new quickSort(A,l,i-1);
Thread t1 = new Thread(sort1);
quickSort sort2 = new quickSort(A,i+1,r);
Thread t2 = new Thread(sort2);
t1.start();
t2.start();
try
{
t1.join();
t2.join();
}catch(Exception e){}
}
}
void normal_quickSort(int[] A,int l,int r)
{
int i;
if(r>l)
{
i = partition(A,l,r);
normal_quickSort(A,l,i-1);
normal_quickSort(A,i+1,r);
}
}