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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:27:00+00:00 2026-06-13T19:27:00+00:00

I need to make a pivot sorting algorithm where essentially you take the first

  • 0

I need to make a pivot sorting algorithm where essentially you take the first item of a linked list, sort it into two separate lists one with items greater than or eual to the first item, and one with items less than the first item, then sort them recursively until you reach a linked list of size 1 where it is already sorted, then just merge together the lesser and greater than lists to form a sorted list.

My algorithm keeps getting stuck on the last item in an infinite loop and I’ve been without sleep now for about 23 hours and need a fresh pair of eyes to spot where I’m making the mistake. If you could help out I would be much obliged.

The Linked list class is simple, two items, first is just a positive integer, second is of course the next item in the list. The code gets caught in the Sort() function when it hits the last item and doesn’t then append the last items together

public class PivotSort {
   public static void main(String[] args) {
      try {
         intList x = intList.CreateFromUserInput(); // just makes a linked list
                                                    // from numbers I type in
         x = Sort(x);
         x.PrintList();
      } catch (Exception e) {
         System.out.println(e);
      }
   }

   public static intList Sort(intList list) {
      if (list == null || list.item == null)
         return list;
      return Append(Sort(Part_Less(list.item, list)),
            Sort(Part_Greater(list.item, list)));
   }

   public static intList Part_Less(int N, intList L1) {
      if (L1 == null)
         return null;
      if (L1.item < N)
         return new intList(L1.item, Part_Less(N, L1.next));
      else
         return Part_Less(N, L1.next);
   }

   public static intList Part_Greater(int N, intList L1) {
      if (L1 == null)
         return null;
      if (L1.item >= N)
         return new intList(L1.item, Part_Greater(N, L1.next));
      else
         return Part_Greater(N, L1.next);
   }

   public static intList Append(intList L1, intList L2) {
      try {
         if (L1 == null)
            return L2;
         if (L2 == null)
            return L1;
         for (intList curr = L1; curr != null; curr = curr.next) {
            if (curr.next == null) {
               curr.next = L2;
               break;
            }
         }
         return L1;
      } catch (Exception e) {
         System.out.println(e);
         return null;
      }
   }
}
  • 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-13T19:27:01+00:00Added an answer on June 13, 2026 at 7:27 pm

    The problem is, that your base case of Sort can never be reached for the second call:

    Sort(Part_Greater(list.item, list))
    

    The Part_Greater() function will at first construct a list that contains all items bigger or equal than the first item. Let’s say this is your origianl list:

    10 5 7 11 15 7 16 20
    

    Part_Greater() will construct a list, that contains 10 11 15 16 20. When you pass that to Sort, it will call Part_Greater() on that very list again, which results in just that list.

    Since no elements are removed after the first call to Part_Greater(), you enter an infinite recursion.

    What you need to do is remove the pivot element from the list. This ensures, that with every recursive step, your list of numbers gets shorter by at least one, eventually hitting the base case when the list is empty.

    return Append(
        Sort(Part_less(list.item, list.next)),
        new intList(list.item, Sort(Part_Greater(list.item, list.next)))
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need make a merge sort using an additional array. Here is my code:
I need make such request: choose list of cities which include letter i and
there have two value, $a and $b . I need make a judge, $a
SQL Server 2008r2 Express SP1. I need to make a sting with list of
I need make some action (dump statistical data) before the Dart program ends. The
I need make all of my posts update. I use bulk upload for store,
Need to make certain Ruby strings in my program to be immutable. What is
I need to make a administrative GUI with lots of functionality like lists, forms,
I need to make a moderate system like the one in fmylife.com. Basically the
I need to make a point in time branch, when we release a new

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.