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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:29:32+00:00 2026-05-23T06:29:32+00:00

I’m going nuts over this special quicksort algorithm and I don’t know where is

  • 0

I’m going nuts over this special quicksort algorithm and I don’t know where is the problem. I’ve found an example on a forum that explains very well what I’m trying to do.

Given an index array of contiguous,
ordered numbers (representing elements
in the data array), you still have to
compare the data values; you just
access them via the index. You swap
the index values, however, not the
data values.

Unsorted data: 09 04 47 05 03 12
Index array:   00 01 02 03 04 05

After sort,
Unsorted data: 09 04 47 05 03 12
Index array:   04 01 03 00 05 02

Print the values indexed by the index array,
from beginning to end of the array:

Index array [0] value [04] data array [04] = 03
            [1]        01             [01]   04
            [2]        03             [03]   05
            [3]        00             [00]   09
            [4]        05             [05]   12
            [5]        02             [02]   47

Output is the data, ordered at the output

I only add one thing. The comparator is a normal comparator with the exception that if we are comparing two different characteres with same values, we compare the next character of each and return that result. That is, comparing the rotations.

int compare(unsigned char i, unsigned char j);

I won’t post the definition because I am sure that it works perfect. The bug lies in the quicksort definition.

void quicksort(unsigned char* a, unsigned char left, unsigned char right) {
    unsigned char i = left;
    unsigned char j = right;
    unsigned char half = (left + right) / 2;
    while (i <= j) {
        while ((compare(a[i], a[half]) == -1) && (i < right))
            i++;
        while ((compare(a[j], a[half]) == 1) && (j > left))
            j--;

        if (i <= j) {
            unsigned char aux = a[i];
            a[i] = a[j];
            a[j] = aux;
            i++;       //THERE
            j--;       //THERE
        }

    }

    if (left < j)
        quicksort(a, left, j);
    if (i < right)
        quicksort(a, i, right);

}

Sometimes i= 255 and j=0, and I don’t why, program gets to THERE, and their values go overflow. I’ve looked for bugs a thousand times and I can’t find where is the mistake.
Notes:
1) I am perfectly aware of C++ unsigned char ranges, and I can’t change them to int.
2) I don’t actually include the declaration of the actual data array. The compare function has access to it since it is an atribute of its class.

  • 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-23T06:29:33+00:00Added an answer on May 23, 2026 at 6:29 am

    Uhh you can’t store numbers larger than 255 or lower than 0 in an unsigned char. An unsigned char can store the range defined by one unsigned byte, so 8 binary digits. 2^8 = 256, and since we include 0, we have 256 – 1, giving us 255. (or in hex, 0xFF)

    Try using ints, or even shorts. (keyword short)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.