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

  • Home
  • SEARCH
  • 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 7493725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:08:29+00:00 2026-05-29T17:08:29+00:00

In this quicksort implementation I think the program I’m running gets into a racing

  • 0

In this quicksort implementation I think the program I’m running gets into a racing condition which origin i can not understand. So i turn myself to SO community for guidance.

If I remove “#pragma omp parallel for private(i)” before for-loop in quicksort() then it sorts correctly.

Below is a sorting sample and the code.

Unsorted
3 6 7 5 3 5 6 2 9 1

Sorted
0 1 2 3 3 5 6 7 9 5 



    size_t average (size_t a, size_t b)
    {
            return (a + b) / 2;
    }

    void swap (int *array, size_t x, size_t y)
    {
            int tmp;
            tmp = array[x];
            array[x] = array[y];
            array[y] = tmp;
    }

    void quicksort (int *array, int left, int right)
    {
            int i, last;

            if (left >= right)
            {
                    return;
            }

            swap (array, left, average(left, right));
            last = left;

            #pragma omp parallel for private(i)
            for (i = left + 1; i <= right; i++)
            {
                    if (array[i] < array[left] )
                    {
                            #pragma omp critical
                            {
                                    last++;
                            }
                            swap(array, last, i);
                    }
            }

            swap (array, left, last);

            #pragma omp parallel sections
            {
                    #pragma omp section
                    {
                            quicksort(array,left,last-1);
                    }

                    #pragma omp section
                    {
                            quicksort (array, last+1, right);
                    }
            }
    }
  • 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-29T17:08:35+00:00Added an answer on May 29, 2026 at 5:08 pm

    The swap operations that you do in that loop are deeply dependent of each other. There is no way of gaining parallelism, here.

    But with the branch parallelism that you develop with the two parallel sections this shouldn’t be necessary. Do you have performance problems with that?

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

Sidebar

Related Questions

Can anyone point out why this implementation of quick sort is not working, i
I am studying the randomized-quicksort algorithm. I realized that the running time of this
This quicksort is supposed to sort v[left]...v[right] into increasing order; copied (without comments) from
is it just me or this code in Programming Pearls is wrong (quicksort wants
This is a bit of a long shot, but if anyone can figure it
I find this Quicksort partitioning approach confusing and wrong, yet it seems to work.
I am working on a Recursive QuickSort method implementation in a GenericList Class. I
After a lot of searching for an implementation of parallel quicksort in c, I'm
I can see there's a sorting object, Sorting , with a quicksort method, quickSort
I have a linked list implementation and I'm experimenting with both MergeSort and QuickSort

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.