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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:19:36+00:00 2026-05-27T04:19:36+00:00

I am having issues implementing a quicksort to sort an array of strings. I

  • 0

I am having issues implementing a quicksort to sort an array of strings. I am also relatively new to c++ so still struggling with some issues there. Right now my code correctly reads in and creates an array of strings but I run into problems when I try to use my quicksort algorithm. The first problem I am running into is that I believe that the recursion is not stopping when it should. I get a segmentation fault after the quicksort runs for a little bit.

Code (Modified):

    #include "MyParser.h"
    #include <iostream>
    #include <fstream>
    #include <string>

    void resize(string*& words, int size)
    {
     string* newArray = new string[size*2];

     for (int i = 0; (i < size)&&(i<size*2); i++)
         newArray[i] = words[i];

     for (int i = size; i < size*2; i++)
     newArray[i] = "";

     delete[] words;
     words = newArray;
    }

    void partitionArray(string*& words, int& left, int& right, int pi)
    {
    int i = left;
    int j = right;
    string tmp;
    string pivot = words[pi];
    while (i < j) {
        string str1 = words[i];
        string str2 = words[j];
        while ((str1.compare(pivot) < 0) && (i < right))
            i++;
        while ((str2.compare(pivot) >= 0) && (j > left))
            j--;
        if (i <= j)
        {
            tmp = words[i];
            words[i] = words[j];
            words[j] = tmp;
            i++;
                j--;
            }
        };
    }

    void quickSort(string*& words, int left, int right)
    {
        int i = left;
        int j = right;
        string tmp;
        string pivot = words[(left + right) / 2];
        /* partition */
        int pivotIndex = (left + right) / 2;
        pivotIndex = partitionArray(words, 0, right, pivotIndex);
        cout << "start recursion" << endl;
        /* recursion */
        if (left < j)
            quickSort(words, left, j);
        if (i < right)
            quickSort(words, i, right);
    }

    int main()
    {
        // define file reader
        ofstream outData;
        outData.open("logData.txt");

        Parser* myParser = new Parser("testData.txt");
        int sizeOfArray = 500;
        string* words = new string[sizeOfArray];
        int index = 0;
        while(myParser->hasTokens())
        {
            if (index >= sizeOfArray)
            {
                resize(words, sizeOfArray);
                sizeOfArray = sizeOfArray*2;
            }
            string currentWord = myParser->nextToken();
            if (currentWord != "")
            {
                words[index] = currentWord;
                index++;
            }
        }
        int lastWordInArrayIndex = index;
        quickSort(words, 0, lastWordInArrayIndex);
        return 0;
     }

Any help on this would be greatly appreciated!

MODIFIED

right now it will sort the following 11 elements correctly:
adfgh
btyui
dfghj
eerty
fqwre
kyuio
verty
wwert
yrtyu
zbsdf
zsdfg

but when attempting to sort the following parsed text, free from all delimiters but worse “like-this” with a single hyphen or words with an apostrophe like “they’re”, it does not terminate:

Three days after the quarrel, Prince Stepan Arkadyevitch
Oblonsky–Stiva, as he was called in the fashionable world–
woke up at his usual hour, that is, at eight o’clock in the
morning, not in his wife’s bedroom, but on the leather-covered
sofa in his study. He turned over his stout, well-cared-for
person on the springy sofa, as though he would sink into a long
sleep again; he vigorously embraced the pillow on the other side
and buried his face in it; but all at once he jumped up, sat up
on the sofa, and opened his eyes.

“Yes, yes, how was it now?” he thought, going over his dream.
“Now, how was it? To be sure! Alabin was giving a dinner at
Darmstadt; no, not Darmstadt, but something American. Yes, but
then, Darmstadt was in America. Yes, Alabin was giving a dinner
on glass tables, and the tables sang, Il mio tesoro–not Il mio
tesoro though, but something better, and there were some sort of
little decanters on the table, and they were women, too,” he
remembered.

Again any help with this issue would be greatly appreciated!

  • 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-27T04:19:36+00:00Added an answer on May 27, 2026 at 4:19 am

    Your quickSort function will indeed recurse indefinitely:

    void quickSort(string*& words, int left, int right)
    {
        int i = left;
        int j = right;
    
        ...
    
        if (left < j)
            quickSort(words, left, j);
        if (i < right)
            quickSort(words, i, right);
    }
    

    i, j, left and right are not modified anywhere in that function, so if left < right the function will be called recursively with the same parameters again and again.

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

Sidebar

Related Questions

HI, I am new to JQuery. I am having issues implementing the same. I
I'm having some issues with implementing a logarithm class with operator overloading in C++.
I'm creating a shopping cart application and I'm having some issues with implementing a
I'm having some issues implementing JPA 2.0 in my app. I'm using Criteria queries
So, I'm having some issues here implementing this math problem in C++. Let's say
I'm having some issues implementing a TimePicker in my application that allows the user
Still having issues with this problem. Please help if you can. So I am
At work we're having issues with different people wanting/suggesting different names for a new
I'm working on a C project implementing some generic containers and am having this
I'm having issues with implementing a flex button component: I have applied a CSS

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.