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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:19:00+00:00 2026-06-06T16:19:00+00:00

I am trying to write a quicksort function to sort anywhere between 10 and

  • 0

I am trying to write a quicksort function to sort anywhere between 10 and 1,000,000 numbers. It iterates through everything but does not sort, just prints the vector as is.

For some reason it jumps out of the while loop way too soon.
The test input I’m using is: (3 6 2 5 1 7 9 10 4 8).
And it’s output: (1 2 6 5 3 7 9 10 4 8)

int main()
{
    std::cout << "Which file would you like to sort?\n";
    std::cin >> file;

    std::ifstream in(file.c_str());

    // Read all the ints from in:
    std::copy(std::istream_iterator<int>(in), std::istream_iterator<int>(),
            std::back_inserter(numbers));

    int max = numbers.size();
    quickSort(numbers, 0, max-1);

    // Print the vector with tab separators:
    std::copy(numbers.begin(), numbers.end(),
            std::ostream_iterator<int>(std::cout, "\t"));
    std::cout << std::endl;

    return 0;
}


void quickSort(vector<int> &numbers, int start, int end)
{
    int i = start;
    int j = end;
    int pivot=numbers[start];
    int temp;
    while( i != j )
    {
        while( numbers[i] < pivot && i < j)
            i++;
        while( numbers[j] >= pivot && i < j)
            j--;

        temp = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = temp;

        if( j < start )
        {
            quickSort( numbers, start, j );
        }

        if( i < start )
        {
            quickSort( numbers, i, end);
        }
    }
    return;
}
  • 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-06-06T16:19:01+00:00Added an answer on June 6, 2026 at 4:19 pm

    Possibly among other things, you aren’t actually looking at the contents of the vector when you move your indices to find a swap. This section:

        while( i < pivot && i < j)
            i++;
        while( j >= pivot && i < j)
            j--;
    

    should be changed to this:

        while( numbers[i] < pivot && i < j)
            i++;
        while( numbers[j] >= pivot && i < j)
            j--;
    

    As one of the commenters mentioned, the bigger lesson is to learn to use a good debugger to step through your code.

    Similarly, you should be selecting pivot as an array value. E.g. pivot = numbers[start]

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

Sidebar

Related Questions

I am trying write a function that generates simulated data but if the simulated
I was trying to write a templatized quicksort function. The thought in my head
Trying to write a conditional with jQuery that basically states, if div.gathering does not
Trying to write out syslog entries containing strings but they don't register. // person.name
Trying to write a function to see how often an object exists and give
Trying to write this small program to help me in my Stats class, everything
Im trying to write a recursive function which returns the factorial of a number.
Trying to write a trivial application, But I have stuck into one of the
Trying to write a simple jQuery function that will multiple the index of an
Trying to write a matrix-multiplying function for arbitrary-sized matrices in C. I'm trying the

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.