Skip to content

Instantly share code, notes, and snippets.

@oerpli
Last active August 29, 2015 14:01
Show Gist options
  • Save oerpli/bcfa2bd04dbddf960c5d to your computer and use it in GitHub Desktop.
Save oerpli/bcfa2bd04dbddf960c5d to your computer and use it in GitHub Desktop.
A (considering it's java) compact quicksort implementation for a double linked list.
private void sort(ListElement l, ListElement r, DoublyLinkedList in) {
ListElement LastMoved = r;
ListElement FirstNotMoved = null;
while (l != r) {
ListElement next = l.next;
if (l.getKey() > r.getKey()) {
move(l, LastMoved, in);
LastMoved = l;
} else if (FirstNotMoved == null)
FirstNotMoved = l;
l = next;
}
if (FirstNotMoved != null) sort(FirstNotMoved, r.prev, in);
if (r != LastMoved) sort(r.next, LastMoved, in);
}
private void move(ListElement f, ListElement p, DoublyLinkedList in) {
if (in.first == f)in.first = f.next;
f.next.prev = f.prev;
f.prev.next = f.next;
f.next = p.next;
f.prev = p;
p.next.prev = p.next = f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment