Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6964361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:54:54+00:00 2026-05-27T15:54:54+00:00

I am having simple code of paralellizing QuickSort algorithm in Java, in run method

  • 0

I am having simple code of paralellizing QuickSort algorithm in Java, in run method I everytime create two seperate new threads for parallelizing processing of Array elements. But as it encounters join() statements for both created threads, threads never backs and halts on joins(), seems join() never releases them.

Below is the code.

class StartJoinQuickSort implements Runnable 
{
    private int m_Low, m_High;
    private int[] m_Array = null;

    private final static int NR_OF_VALUES = 10; // TOTAL_NO_VALUES
    private int PivotElement;   
    private static Random m_random = new Random( );


    public StartJoinQuickSort(int[] a_Array,int a_Low,int a_High)
    {
        this.m_Array = a_Array;
        this.m_Low = a_Low;
        this.m_High = a_High;
    }
    private void SwapArrayElements(int a_i,int a_j)
    {
        int temp = this.m_Array[a_i];
        this.m_Array[a_i] = this.m_Array[a_j];
        this.m_Array[a_j] = temp;

    }// end of SwapArrayElements

    private static int nextRandomFunctionValue(int aStart, int aEnd)
    {
        if ( aStart > aEnd )
        {
           throw new IllegalArgumentException("Start cannot exceed End.");
        }

        //get the range, casting to long to avoid overflow problems
        long range = (long)aEnd - (long)aStart + 1;

        // compute a fraction of the range, 0 <= frac < range

        long fraction = (long)(range * m_random.nextDouble());
        int randomNumber =  (int)(fraction + aStart);    
        return randomNumber;

    }// end of nextRandomFunctionValue

    private static int[] GetArrayWithRandomValues()
    {
        int[] ArrayToBePopulatedWithRandomValues  = new int[NR_OF_VALUES];
        for(int index =0; index<NR_OF_VALUES;index++)
        {
            int RandomValue = StartJoinQuickSort.nextRandomFunctionValue(0,NR_OF_VALUES);
            ArrayToBePopulatedWithRandomValues[index] =  RandomValue;

        }//end of for

        return ArrayToBePopulatedWithRandomValues;

    }//end of GetArrayWithRandomValues
    private int middleIndex(int left, int right) 
    {
        return left + (right - left) / 2;
    }
    public int Partition(int a_Start,int a_end)
    {
    //  System.out.println("Partition ..thId : " + Thread.currentThread().getId());
        int pivotIndex = 0;

        int i = a_Start;
        int j = a_end;

        try
        {
            pivotIndex = middleIndex(a_Start , a_end);
            this.PivotElement = this.m_Array[pivotIndex];

            do
            {
                while(this.m_Array[i] < PivotElement )
                    i++;

                if(j>0)
                {
                    try
                    {
                        while( this.m_Array[j] > PivotElement )
                            j--;
                    }
                    catch(Exception ex){System.out.println(" j : " + j);}

                }//end of if

                if(i<=j)
                {
                    SwapArrayElements(i,j);

                //  System.out.println("Swap .." + + Thread.currentThread().getId());

                    i++;
                    j--;

                }//end of if

            }while(i<=j);
        }
        catch(Exception except)
        {
            System.out.println("exception in Partition " + except);
        }
        return j;
    }

    public void run()  
    {

        //System.out.println("in run..");


        //System.out.println("after PARTITION");

        StartJoinQuickSort oStartQuickSort_1 = null;
        StartJoinQuickSort oStartQuickSort_2 = null;



        if(this.m_Low < this.m_High )
        {
            int Index = Partition(this.m_Low,this.m_High);



            Thread thPart_1 = new Thread ( new StartJoinQuickSort( this.m_Array,this.m_Low,Index ) );
            Thread thPart_2 = new Thread ( new StartJoinQuickSort( this.m_Array,Index + 1,this.m_High ) );

            thPart_1.start();   thPart_2.start();


        //}//end of if
        //if( Index + 1 < this.m_High)
        //{         
            try
            {           
                    thPart_1.join(); thPart_2.join();

            }catch (InterruptedException e) { e.printStackTrace();}         
        }
    }//end of run

Regards
Usman

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:54:55+00:00Added an answer on May 27, 2026 at 3:54 pm

    Hmmm, it is never a good idea to implement a recursive algorithm in parallel like this. You will end up creating a huge number of threads (exponential at every level) and will eventually oversubscribe the system.

    The best idea is to have a cutoff point, which let’s say is equal to the number of available cores. Then when the current level of recursion has a number of branches equal to the cutoff point switch to a sequential quicksort. Some very rough pseudocode of the flow:

    parallel_quicksort(level, interval) {
        // compute subintervals interval1, interval2
        if(level < cutoff) { 
            spawn1: parallel_quicksort(level + 1, interval1);
            spawn2: parallel_quicksort(level + 1, interval2);
    
            join1();     
            join2();
        } else {
            quicksort(interval1);
            quicksort(interval2);
        }
    }
    

    Also have a look over this implementation to see if you’ve missed something: http://www.inf.fh-flensburg.de/lang/algorithmen/sortieren/quick/quicken.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having two errors everytime I try to debug a simple project in Visual
I am having some trouble getting this simple code to work: #pragma once #include
I'm having problem with rendering depth in OpenGL Following code is a simple example
Can u give me the simple code snippt using org.apache.poi.ss.formula.FormulaParser. FormulaParser class having the
I'm new to Objective-C, and I'm trying to create an app with two buttons
I have a some simple Java code that looks similar to this in its
I'm having issue with a simple code downloading a remote image and adding content
I'm having some hard time figuring out what's wrong in this simple code: This
I wanted to know, the best methodology (with code sample please) of having a
What are the options for having a simple blog, content management system that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.