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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:17:20+00:00 2026-05-14T23:17:20+00:00

I am working on a Recursive QuickSort method implementation in a GenericList Class. I

  • 0

I am working on a Recursive QuickSort method implementation in a GenericList Class. I will have a second method that accepts a compareDelegate to compare different types, but for development purposes I’m sorting a GenericList<int>.

I am recieving stackoverflow areas in different places depending on the list size.

I’ve been staring at and tracing through this code for hours and probably just need a fresh pair of (more experienced) eyes. I definitely want to learn why it is broken, not just how to fix it.

public void QuickSort()
{
    int i, j, lowPos, highPos, pivot;
    GenericList<T> leftList = new GenericList<T>();
    GenericList<T> rightList = new GenericList<T>();
    GenericList<T> tempList = new GenericList<T>();

    lowPos = 1; highPos = this.Count;
    if (lowPos < highPos)
    {
        pivot = lowPos;
        for (i = 2; i <= highPos; i++)
        {
            if (this[i].CompareTo(this[pivot]) <= 0)
                leftList.Add(this[i]);
            else
                rightList.Add(this[i]);
        }
        leftList.Add(this[pivot]);
        leftList.QuickSort();
        rightList.QuickSort();

        for(i=1;i<=leftList.Count;i++)
            tempList.Add(leftList[i]);
        for(i=1;i<=rightList.Count;i++)
            tempList.Add(rightList[i]);

        this.items = tempList.items;
        this.count = tempList.count;
    }

}

Finished Product:

public void QuickSort()
{
    Random random = new Random();
    int i, j, lowPos, highPos, pivot;
    GenericList<T> leftList = new GenericList<T>();
    GenericList<T> rightList = new GenericList<T>();
    GenericList<T> tempList = new GenericList<T>();

    if (this.Count > 1)
    {
        pivot = random.Next(1,this.Count);
        for (i = 1; i <= this.Count; i++)
        {
            if (i == pivot) continue;
            if (this[i].CompareTo(this[pivot]) < 0)
                leftList.Add(this[i]);
            else
                rightList.Add(this[i]);
        }
        leftList.QuickSort();
        rightList.QuickSort();

        for(i=1;i<=leftList.Count;i++)
            tempList.Add(leftList[i]);
        tempList.Add(this[pivot]);
        for(i=1;i<=rightList.Count;i++)
            tempList.Add(rightList[i]);

        this.items = tempList.items;
        this.count = tempList.count;
    }

}
  • 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-14T23:17:20+00:00Added an answer on May 14, 2026 at 11:17 pm

    Your implementation is including the pivot in your sublists. By including the pivot in your sublists (in this case your left list because your condition is <=), you are setting yourself up for a possible infinite recursion if that pivot ends up in the middle of the sublist.

    Example:

    1. List = [3, 6, 12, 4, 8] Pivot = 3 (12) Left = [3, 6, 12, 4, 8] Right = [ Empty ]
    2. List = [3, 6, 12, 4, 8] Pivot = 3 (12) Left = [3, 6, 12, 4, 8] Right = [ Empty ]
    3. List = [3, 6, 12, 4, 8] Pivot = 3 (12) Left = [3, 6, 12, 4, 8] Right = [ Empty ]
    4. … Infinite Loop

    Because the pivot is not excluded (although its final position is known), it can result in you sorting the same list over and over forever, rather than decreasing the size of the lists to sort with each recursive call.

    If you exclude your pivot (by index, not by value) from the sublists and add it back it into the final tempList between leftList and rightList, it will work properly.

            ...
            for (i = 1; i <= highPos; i++)
            {
                if (i == pivot) continue; // Add this
                if (this[i].CompareTo(this[pivot]) <= 0)
            ...
            for (i = 1; i <= leftList.Count; i++)
                tempList.Add(leftList[i]);
            tempList.Add(this[pivot]); // Add this
            for (i = 1; i <= rightList.Count; i++)
                tempList.Add(rightList[i]);
            ...
    

    See Also: Wikipedia article on Quicksort (with pseudocode)

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

Sidebar

Ask A Question

Stats

  • Questions 407k
  • Answers 407k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After you have uploaded them to the initial upload directory,… May 15, 2026 at 6:34 am
  • Editorial Team
    Editorial Team added an answer The Eclipse editor is not fully WYSIWYG, it's somewhat limited.… May 15, 2026 at 6:34 am
  • Editorial Team
    Editorial Team added an answer You're doing an INSERT query, so $login won't contain any… May 15, 2026 at 6:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.