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 6363439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:02:20+00:00 2026-05-25T00:02:20+00:00

Newbie programmer here trying to implement quicksort, yet it won’t work. I’ve looked at

  • 0

Newbie programmer here trying to implement quicksort, yet it won’t work. I’ve looked at online resources but I just can’t seem to spot the error in my implementation. Thanks in advance.

EDIT Issue I’m having seems like it gets stuck in the quicksort function, and the program just hangs. When I tried debugging it with printf’s, the original array seems to have been modified with unexpected numbers (not from the original list), such as 0’s.

void quicksort(int a[], const int start, const int end)
{
   if( (end - start + 1 ) < 2)
      return;

   int pivot = a[rand()%(end - start)];

   //Two pointers
   int L = start;
   int R = end;

   while(L < R)
   {
      while(a[L] < pivot)
         L++;
      while(a[R] > pivot)
         R--;
      if(L < R)
         swap(a,L,R);      
   }
   quicksort(a, start, L-1);
   quicksort(a, L+1, end );
}

void swap(int a[], const int pos1, const int pos2)
{
   a[pos1] ^= a[pos2];
   a[pos2] ^= a[pos1];
   a[pos1] ^= a[pos2];
}

int main()
{
   int array[20] = {0};
   int size = sizeof(array)/sizeof(array[0]);//index range = size - 1

   int i = 0;
   printf("Original: ");
   for (i; i < size; i++)
   {
      array[i] = rand()%100+ 1;
      printf("%d ", array[i]);
   }
   printf("\n");

   quicksort(array,0,size-1);

   int j = 0;
   printf("Sorted: ");
   for(j; j < size; j++)
      printf("%d ", array[j]);
   printf("\n");
}

Additional Question: In regards to calling quicksort recursively, would the left and right pointer always point towards the pivot at the end of each partition? If so, is calling quicksort from start to L-1 and L+1 to end correct?

Also, is the if (L < R) before the swap necessary?

  • 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-25T00:02:21+00:00Added an answer on May 25, 2026 at 12:02 am

    I believe that the problems stem from two errors in the logic. The first one is here:

    int pivot = a[rand()%(end - start)];
    

    Note that this always picks a pivot in the range [0, end – start) instead of [start, end). I think you want to have something like

    int pivot = a[rand()%(end - start) + start];
    

    so that you pick a pivot in the range you want.

    The other error is in this looping code:

    while(L < R)
    {
       while(a[L] < pivot)
          L++;
       while(a[R] > pivot)
          R--;
        if(L < R)
          swap(a,L,R);      
    }
    

    Suppose that L < R, but that a[L], a[R], and pivot are all the same value. This might come up, for example, if you were quicksorting a range containing duplicate elements. It also comes up when you use rand with the standard Linux implementation of rand (I tried this on my machine and 27 was duplicated twice). If this is the case, then you never move L or R, because the conditions in the loops always evaluate to false. You will need to update your logic for partitioning elements when duplicates are possible, since otherwise you’ll go into an infinite loop here.

    Hope this helps!

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

Sidebar

Related Questions

Newbie to C++ but I am a Java programmer. Trying to learn C++ now.
I'm a Facebook programmer newbie. Could anybody recommend good resources for starters? I believe
newbie programmer here. I have 3 tables namely product, category, and subcategory. I configured
I'm a newbie programmer trying to jump in to Python by building a script
Django (and database) newbie here. I'm trying to figure out a way to enable
newbie programmer and lurker here, hoping for some sensible advice. :) Using a combination
I'm a newbie programmer trying to build a demo PHP site on my computer
I am a newbie servlet programmer. I am trying to do this right. I
I am an experienced Java programmer, but a Swing newbie so please bear with
I'm a C++ programmer, but I'm a newbie in Flex. I'm developing a Flex

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.