How do I get insertion sort to work with a doubly linked list

Joseph Campbell

I cannot get this method to sort the Doubly Linked List from least to greatest.

My input is : MyLinkedList: 3.0, 4.0, 2.0, 69.0, 76.0, 22.0, 341.0, 15.0

public void insertionSort(Comparator<? super E> compare) {
DLinkedNode<E> tempI = head.next;    
for (int i = 1; i < size  && tempI.next != null; i++) {      
  E temp = tempI.data;
  int j;
  DLinkedNode<E> tempJ = tempI.prev;
  for (j = i-1; j >= 0 && tempJ.prev != null; j--) {        
    if (compare.compare(tempJ.data, temp) <= 0){
      printLinkedList();
      System.out.println("");
      tempJ.next.data = tempJ.data;                  
    }
    tempJ = tempJ.prev;//j--;          
  }//end for
  tempJ.next.data = temp;      
  tempI = tempI.next;//i++;            
}//end for    
}// end insertionSort

The output after each iteration of the inner for loop is:

MyLinkedList: 3.0, 2.0, 2.0, 69.0, 76.0, 22.0, 341.0, 15.0
MyLinkedList: 3.0, 2.0, 2.0, 2.0, 76.0, 22.0, 341.0, 15.0
MyLinkedList: 3.0, 69.0, 2.0, 2.0, 76.0, 22.0, 341.0, 15.0
MyLinkedList: 3.0, 69.0, 2.0, 2.0, 2.0, 22.0, 341.0, 15.0
MyLinkedList: 3.0, 69.0, 2.0, 2.0, 2.0, 22.0, 341.0, 15.0
MyLinkedList: 3.0, 76.0, 69.0, 2.0, 2.0, 22.0, 341.0, 15.0
MyLinkedList: 3.0, 76.0, 69.0, 2.0, 2.0, 2.0, 341.0, 15.0
MyLinkedList: 3.0, 22.0, 69.0, 2.0, 2.0, 2.0, 341.0, 15.0
MyLinkedList: 3.0, 22.0, 69.0, 2.0, 2.0, 2.0, 2.0, 15.0
MyLinkedList: 3.0, 22.0, 69.0, 2.0, 2.0, 2.0, 2.0, 15.0
MyLinkedList: 3.0, 22.0, 69.0, 2.0, 2.0, 2.0, 2.0, 15.0
MyLinkedList: 3.0, 22.0, 69.0, 69.0, 2.0, 2.0, 2.0, 15.0
MyLinkedList: 3.0, 341.0, 22.0, 69.0, 2.0, 2.0, 2.0, 15.0

comparable method is:

public class DoubleComp implements Comparator<Double> {
  public int compare(Double a1, Double a2) {
  double foo = ((double) a1 - (double) a2);
  return (int) foo;
  }
}
Ivan Zaitsau
  1. Use Double.compare(double d1, double d2) to compare doubles if you are using JDK 7 or later.
  2. Sorting looks really messed up. Try using the following approach:

    Node root; Comparator comp;
    Node current = root;
    while (current.next != null) {
        E data = current.data;
        Node i = current;
        while (i.previous != null && comp.compare(data, i.previous.data) < 0) {
            i.data = i.previous.data;
            i = previous;
        }
        i.data = data;
        current = current.next;
    }
    

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get insertion sort to work with a doubly linked list

From Dev

insertion sort doubly linked list

From Dev

Linked List: How to Sort Doubly Linked List?

From Dev

How is insertion for a Singly Linked List and Doubly Linked List constant time?

From Dev

How is insertion for a Singly Linked List and Doubly Linked List constant time?

From Dev

How do I insertion sort a list of lists?

From Dev

In a doubly linked list, How many pointers are affected on an insertion operation?

From Dev

How do I Sort the names in a Linked List?

From Dev

Insertion Sort with a Linked List

From Dev

How Do I Fix This Sorted Linked List Insertion?

From Dev

How could I make this singly linked list into a doubly linked list?

From Dev

Doubly Linked List C, insertion at specific position

From Dev

Doubly Linked List Insertion With Recursion in C

From Dev

Doubly Linked List insertion Segmentation Fault - C

From Dev

Doubly Linked List C, insertion at specific position

From Dev

alphabetically sorted insertion into doubly-linked list

From Dev

Why do i get the same string value but different arithmetic values when printing from a Doubly Linked List

From Dev

Why do i get the same string value but different arithmetic values when printing from a Doubly Linked List

From Dev

How does traverse backward work for a Doubly Linked List in Java?

From Dev

How do I update this old C++ doubly linked list code to C++11?

From Dev

Merge Sort on a Doubly Linked List C++

From Dev

How do I sort a linked list in a alphabetical order in c

From Dev

How do I sort a map by order of insertion?

From Dev

How do I get the first character of a string from a linked list?

From Dev

insertion sort linked list c++

From Dev

Sorting a linked list in parallel using Insertion Sort

From Dev

Insertion sort in Double Linked List c++

From Dev

insertion sort using linked list and pointers

From Dev

How would I sort a linked list?

Related Related

  1. 1

    How do I get insertion sort to work with a doubly linked list

  2. 2

    insertion sort doubly linked list

  3. 3

    Linked List: How to Sort Doubly Linked List?

  4. 4

    How is insertion for a Singly Linked List and Doubly Linked List constant time?

  5. 5

    How is insertion for a Singly Linked List and Doubly Linked List constant time?

  6. 6

    How do I insertion sort a list of lists?

  7. 7

    In a doubly linked list, How many pointers are affected on an insertion operation?

  8. 8

    How do I Sort the names in a Linked List?

  9. 9

    Insertion Sort with a Linked List

  10. 10

    How Do I Fix This Sorted Linked List Insertion?

  11. 11

    How could I make this singly linked list into a doubly linked list?

  12. 12

    Doubly Linked List C, insertion at specific position

  13. 13

    Doubly Linked List Insertion With Recursion in C

  14. 14

    Doubly Linked List insertion Segmentation Fault - C

  15. 15

    Doubly Linked List C, insertion at specific position

  16. 16

    alphabetically sorted insertion into doubly-linked list

  17. 17

    Why do i get the same string value but different arithmetic values when printing from a Doubly Linked List

  18. 18

    Why do i get the same string value but different arithmetic values when printing from a Doubly Linked List

  19. 19

    How does traverse backward work for a Doubly Linked List in Java?

  20. 20

    How do I update this old C++ doubly linked list code to C++11?

  21. 21

    Merge Sort on a Doubly Linked List C++

  22. 22

    How do I sort a linked list in a alphabetical order in c

  23. 23

    How do I sort a map by order of insertion?

  24. 24

    How do I get the first character of a string from a linked list?

  25. 25

    insertion sort linked list c++

  26. 26

    Sorting a linked list in parallel using Insertion Sort

  27. 27

    Insertion sort in Double Linked List c++

  28. 28

    insertion sort using linked list and pointers

  29. 29

    How would I sort a linked list?

HotTag

Archive