package com. my. util;
public class SingleNode {
public int value;
public SingleNode next;
public SingleNode ( int data) {
this . value = data;
}
}
package com. my. suanfa;
import com. my. util. SingleNode;
public class Solution13 {
public SingleNode selectionSort ( SingleNode head) {
SingleNode tail = null;
SingleNode cur = head;
SingleNode smallPre = null;
SingleNode small = null;
while ( cur != null) {
small = cur;
smallPre = getSmallestPreNode ( cur) ;
if ( smallPre != null) {
small = smallPre. next;
smallPre. next = small. next;
}
cur = cur == small ? cur. next : cur;
if ( tail == null) {
head = small;
} else {
tail. next = small;
}
tail = small;
}
return head;
}
public SingleNode getSmallestPreNode ( SingleNode head) {
SingleNode smallPre = null;
SingleNode small = head;
SingleNode pre = head;
SingleNode cur = head. next;
while ( cur != null) {
if ( cur. value < small. value) {
smallPre = pre;
small = cur;
}
pre = cur;
cur = cur. next;
}
return smallPre;
}
}