What's wrong with my sorted linked list algorithm? (Java)

In summary, the conversation revolved around an algorithm that compares a node to be inserted with the current linked list set. The desired output was discussed and the code for the algorithm was provided. The main issue was an infinite loop occurring due to a mistake in assigning pointers. After fixing this mistake, the program worked as intended.
  • #1
iamsmooth
103
0
So my algorithm compares a node that needs to be inserted with the current linked list set. it goes from first (which contains the lowest integer) and moves forward until it hits null or when the key trying to be inserted is no longer greater than the nodes being compared to.

My input of 23, 17, 3, 28, 8, 12 should yield output of 3, 8, 12, 17, 23, 28

but instead it goes:

3, 8, 17, 23, 23, 23, 23, 23... where 23 becomes an infinite loop.


Code:
      Node traverseInOrder = first; // loops through the linked list (first is preconstructed node)
   
      // iterator is the node trying to be inserted
      while (iterator.key.compareTo(traverseInOrder.key) > 0) {
          if (traverseInOrder.successor != null) {
             traverseInOrder = traverseInOrder.successor;
          }
          else {
              break;
          }
      }
      // if node should be inserted in the front
      if (traverseInOrder.predecessor == null) {
          iterator.successor = traverseInOrder;
          traverseInOrder.predecessor = iterator;
          iterator.predecessor = null;
          first = iterator;
      }
      // when the node should be inserted at the end
      // there are 2 cases, as node could be at the end, or the one before the end
      else if (traverseInOrder.successor == null) {
          if (iterator.key.compareTo(traverseInOrder.key) < 0) {
              traverseInOrder.predecessor.successor = iterator;
              iterator.predecessor = traverseInOrder.predecessor;
              iterator.successor = traverseInOrder;
              traverseInOrder.predecessor = iterator;
          }
          else {
              iterator.predecessor = traverseInOrder;
              traverseInOrder.successor = iterator;
          }
      }
      // when the node should be inserted in the middle
      else if (traverseInOrder.successor != null && traverseInOrder.predecessor != null) {
          iterator.predecessor = traverseInOrder.predecessor;
          traverseInOrder.predecessor.successor = iterator;
          iterator.successor = traverseInOrder;
          traverseInOrder.predecessor = iterator;
      }


I can't figure out what I did wrong. I've tried rewriting my algorithm from scratch like 6 times but it's always some different variant of an infinite loop. Can anyone help make some suggestions or even give me a better algorithm? They have to be placed into the list sorted so I can't use any sorting algorithms afterwards.

Thanks!
 
Technology news on Phys.org
  • #2
What does 'successor' point to in a node you insert at the end of the list?
 
  • #3
The last node on the list should have successor null, and vise versa with the first node on the list having predecessor of null. I think my code already does that though, doesn't it? I don't see where it's wrong.
 
  • #4
I don't see any place in your code where you actually set it to null.
 
  • #5
Code:
// there are 2 cases, as node could be at the end, or the one before the end
      else if (traverseInOrder.successor == null) {
          // this condition is here because if traverse.successor is null, traversal is not incremented
          if (iterator.key.compareTo(traverseInOrder.key) < 0) {
              traverseInOrder.predecessor.successor = iterator;
              iterator.predecessor = traverseInOrder.predecessor;
              iterator.successor = traverseInOrder;
              traverseInOrder.predecessor = iterator;
          } 
          // this covers the case when successor is null, but the key to insert is greater
          else { 
              iterator.predecessor = traverseInOrder;
              traverseInOrder.successor = iterator; 
              //iterator.successor = null // **even if I put this here, nothing happens**
          }
      }

The nodes are constructed with all pointer nodes initialized as null, so I don't think that's what was wrong. I did change them to point to null in the code and it made no difference. Still an infinite loop. Or am I misunderstanding what you're saying?
 
  • #6
You need to post more code, including initialization and declarations. I suspect that there's something going wrong during the very first insertion, where the node with 23 ends up pointing at itself (lines 14-17 of your fragment).

Code:
     Node traverseInOrder = first; // loops through the linked list (first is preconstructed node)
...
     iterator.successor = traverseInOrder;
...
     first = iterator;

Unfortunately, I know C++ much better than I know Java. In C++, it would've been much clearer (to me, anyways) whether objects being assigned are pointers, references, or values.
 
Last edited:
  • #7
You were right. I was using a reference to the node's parent instead of the node itself which caused these crazy bugs. After removing it, the program works fine. Thanks!
 

1. Why is my sorted linked list not sorting the elements correctly?

There could be a few reasons for this. One possibility is that there is a bug in your sorting algorithm. Make sure to carefully review your code and check for any errors. Another possibility is that the elements in your linked list are not comparable, so the sorting algorithm is not able to properly sort them. Make sure that the elements in your linked list implement the Comparable interface and that you have correctly implemented the compareTo() method.

2. How do I know if my linked list is actually sorted?

You can check if your linked list is sorted by iterating through the list and comparing each element with the next one. If the elements are in ascending or descending order, then your list is sorted. Alternatively, you can use a sorting algorithm to sort the list and then compare the sorted list with the original one to see if they match.

3. Can I use a different sorting algorithm for my sorted linked list?

Yes, there are many different sorting algorithms that can be used for a sorted linked list. Some common ones include bubble sort, selection sort, insertion sort, merge sort, and quick sort. It is important to choose an algorithm that is efficient for your specific use case.

4. How can I optimize my sorted linked list algorithm for better performance?

One way to optimize your algorithm is to choose a more efficient sorting algorithm, as mentioned in the previous question. Another way is to carefully consider the data structure and implementation of your linked list. For example, you could use a doubly linked list instead of a singly linked list, which would make insertion and deletion operations faster.

5. What is the time complexity of a sorted linked list algorithm?

The time complexity of a sorted linked list algorithm depends on the specific sorting algorithm used. Most sorting algorithms have a worst-case time complexity of O(n^2), meaning that the time it takes to sort the list increases as the size of the list increases. However, there are also more efficient sorting algorithms, such as merge sort and quick sort, which have a worst-case time complexity of O(nlogn).

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
11K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top