1

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);
    }
}
KMB
  • 21
  • 4

1 Answers1

4

Two problems,

(1) You are creating too many threads. Creating threads is expensive. For a compute-bound task, it's probably a mistake if the total number of threads that you create is greater than the number of CPUs in your platform.

The trick is to pool the threads. (e.g., create a java.util.concurrent.ThreadPoolExecutor, and give it tasks to perform).

(2) You are breaking the work up into too-small tasks. Synchronizing threads and passing data between threads is expensive too. Not nearly as expensive as creating and destroying threads, but there's still a limit to how many times you want to do it.

Your algorithm potentially divides the work up to where you're giving a thread a list of one element, and asking the thread to "sort" it. That's a ridiculously small task for multi-threading.

To implement quicksort, you have to keep splitting the array into smaller and smaller chunks, but you don't have to give each chunk to a different thread, and after they get small enough, you shouldn't. I don't know how small is small enough (100? 1000? 10,000?), but you could easily experiment to find out.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57