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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:43:51+00:00 2026-06-02T06:43:51+00:00

I am doing my homework, and completed quicksort recursive, however it doesn’t sort in

  • 0

I am doing my homework, and completed quicksort recursive, however it doesn’t sort in a correct way. Seems to be it doesn’t swap correctly.

Here is my code

#include<iostream>
#include<ctime>
#include<string>
using namespace std;


int quick_sort_help(string &text,int left, int right, int pivot){


  char val = text[pivot];
  char temp;

  //swap
 // temp =text[pivot];
  //text[pivot]= text[right];
  //text[right]=temp;

  //swap(&text[left],&text[right]);

  int l = left;
  int r = right;

  int i=left;
  while (i<=r)
  {
      while (text[i]<val)
          i++;
      while (text[right]>val)
          r--;
      if (i<=r)
      {
          temp=text[i];
          text[i]=text[r];
          text[r]=temp;
          i++;
          r--;
      }
  }


  return l;
     }


void quicksort(string &text,int left, int right){

      if (left < right){

          int pivot=(left+right)/2;
          int pivottwo = quick_sort_help(text, left, right, pivot);
          quicksort(text, left, pivottwo - 1);
          quicksort(text, pivottwo + 1, right);
          }
 }  
void quick_sort(string &text,int size){
              quicksort(text,0,size);}


int main()
{

    string text="this is a test string text,.,!";
    int size = text.length();
    float t1, t2;
    t1 = clock();
    quick_sort(text,size);

    t2=clock();
    cout<<"quicksort Sort: "<<(t2-t1)/CLK_TCK*1000<<" msec\n"; 
    cout<<text<<endl;


    system("pause");
    return 0;
}

the output I am getting:

hi  a e  e,g.nii,r!tssssxttttt
  • 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-02T06:43:53+00:00Added an answer on June 2, 2026 at 6:43 am

    You have to:

    1) Don’t use size but size-1 value

    void quick_sort(string &text,int size){
              quicksort(text,0,size-1);}
    

    2) Pivot is not (left+right)/2 but it’s the value returned by quick_sort_help, and pivottwo is not necessary:

    void quicksort(string &text,int left, int right)
    {
      if (left < right)
      {
          int pivot = quick_sort_help(text, left, right);
          quicksort(text, left, pivot - 1);
          quicksort(text, pivot + 1, right);
      }
    }
    

    3) Test my j value (your r) in the second while and make the exchange before returning the pivot (the i value):

    int quick_sort_help(string &text,int left, int right)
    {
      char val = text[right];
      char temp;
    
      int j = right;
      int i = left - 1;
    
      while (true)
      {
      while (text[++i] < val);
    
      while (text[--j] > val) {
          if(j == left)
              break;
      }
    
      if(i >= j)
          break;
    
      temp=text[i];
      text[i]=text[j];
      text[j]=temp;
     }
    
     temp=text[i];
     text[i]=text[right];
     text[right]=temp;
    
     return i;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing some homework here (second assignment, still extremely green...). The object is to read
This is NOT a homework question, actually I am doing this for fun. Here
newbie doing Java homework here. I have one class named Album which contains the
I am doing some assembly homework and thought the best way to get my
I was doing homework in C# that required sorting an array. I could sort
No problems here, just need explanation how does that work. I was doing homework
I am doing homework in Matlab, calculating numeric integration using different methods like simpson,
I am doing homework but I have problem with non-RestFul routes. My spec is:
while doing some homework in my very strange C++ book, which I've been told
I'm doing some homework and while I have some experience with SML, Haskell has

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.