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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:31:46+00:00 2026-05-28T13:31:46+00:00

I am trying to implement Quick Sort algorithm. Following code works for unique elements

  • 0

I am trying to implement Quick Sort algorithm. Following code works for unique elements but it doesn’t working for arrays having duplicate elements. Please tell me where I am doing wrong. Also when I change value of pivot to some other number other than 0 , program crashes. Here is the code:

#include <iostream>
#include <cstdlib>

using namespace std;

void swapme(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

void quicksort(int *arr, int size)
{    
    // these two variables will take care of position of comparison
    int lower = 0, upper = size - 1;  
    int pivot = 0;  // assigns pivot
    if (size <= 1)
        return;

    while (lower < upper)
    {
        while (arr[lower] < arr[pivot])
        {
            ++lower;
        }
    }

    while (arr[upper] > arr[pivot])
    {
        --upper;
    }

    if (upper > lower)
    {
        swapme(arr[upper], arr[lower]);
        // upper--;
        // lower++;
    }

    quicksort(arr, lower);
    quicksort(&arr[lower + 1], size - 1 - lower);
}

int main()
{
    int arr[30];

    for(int j = 0; j < 30; j++)
    {
        arr[j] = 1 + rand() % 5000;
    }

    for(int j = 0; j < 30; j++)
    {
        cout << arr[j] << "\t";
    }
    cout << endl;

    quicksort(arr, 30);

    for(int j = 0; j < 30; j++)
    {
        cout << arr[j] << "\t";
    }
    cout << endl;

    cin.get();
    cin.get();
}

Update: I have finally managed to make it work. Here is the fixed version:

void swapme(int &a, int &b )
{
    int temp = a;
    a = b;
    b = temp;
}

void quicksort(int *arr, int size)
{
    if (size <= 1)
        return;

    // These two variables will take care of position of comparison.
    int lower = 0;
    int upper = size-1;  

    int pivot = arr[upper/2]; // assigns pivot

    while (lower <= upper)
    {
        while (arr[lower] < pivot)
            ++lower;
        while (arr[upper] > pivot)
            --upper;

        if (upper >= lower)
        {
            swapme(arr[upper],arr[lower]);
            if(arr[upper] == arr[lower])
            {
                // Can either increment or decrement in case of duplicate entry
                upper--; // lower++;
            }
        }
    }

    quicksort(arr, lower);
    quicksort( &arr[lower+1], size-1-lower);
}
  • 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-28T13:31:47+00:00Added an answer on May 28, 2026 at 1:31 pm

    You are storing the index of your pivot element in the pivot variable, so swapping the elements can potentially change the choice of pivot element during the loop. Not a very good idea. I would suggest storing the actual value of the pivot element inside pivot instead.

    Also, if this really isn’t homework, why don’t you simply use the standard library facilities?

    #include <algorithm>
    
    // ...
    
    std::sort(arr + 0, arr + 30);
    

    You will get heavily optimized and tested code that will outperform your handwritten Quicksort anytime.

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

Sidebar

Related Questions

I am trying to implement the quick sort algorithm using ruby. See what I
I am trying to implement a quick sort in java. However, I am experiencing
I'm trying to implement QuickSort algorithm program in Java, but I'm getting incorrect answer.
I am trying to implement the quicksort algorithm in OCaml, and I thought I
I'm extremely new to SOAP and I'm trying to implement a quick test client
I'm trying to implement a Quicksort algorithm, I have read how to do it
I am trying to implement the following method of QLPreviewControllerDelegate and the method requires
I've been trying to make my app implement the built in quick search, similar
I'm trying to figure out a logical and quick way to implement the PRG
So I've been trying to implement a quicksort myself, but it generates a stackoverflowerror,

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.