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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:09:46+00:00 2026-05-28T11:09:46+00:00

I have written a bubble sort algorithm to sort a linked list. I am

  • 0

I have written a bubble sort algorithm to sort a linked list. I am a Java beginner and trying to learn data structures. I am confused why my second element is not sorted properly.

class SListNode {
    Object item;
    SListNode next;

    SListNode(Object obj) {
        item = obj;
        next = null;
    }

    SListNode(Object obj, SListNode next) {
        item = obj;
        this.next = next;
    }
}
public class SList {
    private SListNode head;
    private SListNode temp;

    public void sortList() {
        SListNode node = head, i, j;
        head = node;
        i = node;
        j = node.next;
        while (i.next != null) {
            while (j.next != null) {
                if ((Integer) i.item < (Integer) j.item) {
                    temp = i.next;
                    i.next = j.next;
                    j.next = temp;
                }
                j = j.next;
            }
            i = i.next;
        }
    }
}

This is the output I am getting

List after construction: [  3  6  9  4  12  15  ]
After sorting: [  3  4  9  12  6  15  ]

Besides I know the worst case scenario of a bubble sort is O(n2). Can I use mergesort on a linked list to have a better time complexity?

  • 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-28T11:09:47+00:00Added an answer on May 28, 2026 at 11:09 am

    There are many sorting algorithms that work on linked lists and mergesort works excellently in this case. I wrote an earlier answer to a question about sorting linked lists that explores many classic sorting algorithms on linked lists, along with their time and space complexities. You can use insertion sort, selection sort, mergesort, and quicksort on linked lists. With a bit of fudging, you can also get heapsort working. My older answer has details on how to do this.

    With regards to your code, notice that in your inner loop you advance j forward until the next pointer becomes null. At this point, you never reset j to be anything else, so on each future iteration of the outer loop the inner loop never executes. You should probably set j = i.next at the start of each iteration. Moreover, you probably don’t want to have the loop stop when j.next is null, but rather when j is null, since otherwise you skip the last element of the array.

    Additionally, the sorting algorithm you’ve written here is selection sort rather than bubble sort, because you’re making many passes over the linked list looking for the smallest element that you haven’t positioned yet. I don’t know if this is a problem or not, but I wasn’t sure if you were aware of this. That said, I think that’s probably a good thing, since bubble sort is less efficient than selection sort in most cases (unless the list is already close to being sorted).

    Hope this helps!

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

Sidebar

Related Questions

I have written 2 implementation of bubble sort algorithm in C and Haskell. Haskell
have written a stochastic simulation in Java, which loads data from a few CSV
I have written a Java program which I package and run from a JAR
I have written a web-service (in Java) which produces as a result a (rather
Have written all the code in a silverlight class library (dll) and linked this
I have written a WSDL and I want to generate java classes. I use
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written a webmethod that returns the list of the Users althought the
I have written the following method to return a list of Unserializable classes (LINQ
I have written a java axis2 1.4.1 web service and .net 3.5 WCF client

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.