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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:27:14+00:00 2026-06-05T15:27:14+00:00

I have this quick sort implementation to sort the characters. int main(){ char val[]

  • 0

I have this quick sort implementation to sort the characters.

int main(){
char val[] = "dcbeaaae";
    QuickSort(val, 0, length);

    for(int i=0;i<length;i++) //Sorted print
        cout<<val[i];
return 0
}

void QuickSort(char values[], int first, int last)
{
    if(first<last)
    {
        int splitPoint;
        Split(values, first, last, splitPoint);
        QuickSort(values, first, splitPoint-1);
        QuickSort(values, splitPoint+1, last);
    }
}

void Split(char values[], int first, int last, int& splitPoint)
{
    int splitVal = values[first];
    int saveFirst = first;
    bool onCorrectSide;

    first++;
    do
    {
        onCorrectSide = true;
        while(onCorrectSide)
        {
            if(values[first] > splitVal)
                onCorrectSide = false;
            else
            {
                first++;
                onCorrectSide = (first<=last);
            }
        }
        onCorrectSide = (first<=last);
        while(onCorrectSide)
            if(values[last] <= splitVal)
                onCorrectSide = false;
            else
            {
                last--;
                onCorrectSide = (first<=last);
            }

        if(first<last)
        {
            Swap(values[first], values[last]);
            first++;
            last--;
        }
    }while(first<=last);

    splitPoint = last;
    Swap(values[saveFirst], values[splitPoint]);
}

void Swap(char& item1, char& item2)
{
    char temp = item1;
    item1 = item2;
    item2 = temp;
}

However, the output that I get from this is a bit wrong i.e I get an additional space in the starting of these sorted characters. On putting a breakpoint, I saw that at index 0, the element is = 0

Input: aaabcdee
Output: aaabcdee (one additional space before the first a )

Any suggestions what I am missing out here?

  • 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-05T15:27:15+00:00Added an answer on June 5, 2026 at 3:27 pm

    Assuming length is the number of characters (excluding the NUL char) in the character array. You need to call the quicksort function as:

    QuickSort(val, 0, length-1);
    

    because the last argument to the function QuickSort is the index of the last element of the array and in a character array of length length this index is length - 1.

    By passing length to the function you are making even the NUL character participate in the sorting and as it is smaller than other characters it gets moved to the beginning of the sorted array which gets printed as a blank when printed.

    • 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
Currently, I have a barebones implementation of the quicksort algorithm to sort some randomly
This is a weired problem, I have implemented simple quick sort as follows.. static
I have this quick question, i have got the username variable from a form
i have this quick issue please. I have this code here which permits me
I will make this quick and simple I have a query and I want
Really quick jQuery question... I have this function: $(document).ready(function() { $('a#show1').click(function() { $('.item1').toggle(1000); return
I am hoping that this will have a pretty quick and simple answer. I
This is just a quick question in C#. I have a scenario where I
Say I have a string: the quick brown fox jumped over the moon.this text

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.