package chapter_2_listproblem;
public class Problem_16_ListSelectionSort {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static Node selectionSort(Node head) {
Node tail = null; // sorted part tail
Node cur = head; // unsorted part head
Node smallPre = null; // previous node of the smallest node
Node small = null; // smallest node
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 static Node getSmallestPreNode(Node head) {
Node smallPre = null;
Node small = head;
Node pre = head;
Node cur = head.next;
while (cur != null) {
if (cur.value < small.value) {
smallPre = pre;
small = cur;
}
pre = cur;
cur = cur.next;
}
return smallPre;
}
public static void printLinkedList(Node head) {
System.out.print("Linked List: ");
while (head != null) {
System.out.print(head.value + " ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = null;
head = selectionSort(head);
printLinkedList(head);
head = new Node(1);
head = selectionSort(head);
printLinkedList(head);
head = new Node(1);
head.next = new Node(2);
head = selectionSort(head);
printLinkedList(head);
head = new Node(2);
head.next = new Node(1);
head = selectionSort(head);
printLinkedList(head);
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head = selectionSort(head);
printLinkedList(head);
head = new Node(1);
head.next = new Node(3);
head.next.next = new Node(2);
head = selectionSort(head);
printLinkedList(head);
head = new Node(2);
head.next = new Node(1);
head.next.next = new Node(3);
head = selectionSort(head);
printLinkedList(head);
head = new Node(2);
head.next = new Node(3);
head.next.next = new Node(1);
head = selectionSort(head);
printLinkedList(head);
head = new Node(3);
head.next = new Node(1);
head.next.next = new Node(2);
head = selectionSort(head);
printLinkedList(head);
head = new Node(3);
head.next = new Node(2);
head.next.next = new Node(1);
head = selectionSort(head);
printLinkedList(head);
head = new Node(3);
head.next = new Node(1);
head.next.next = new Node(4);
head.next.next.next = new Node(2);
head = selectionSort(head);
printLinkedList(head);
}
}
Problem_16_ListSelectionSort
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- The average male drinks 2 L of water when active outdoors...
- The Problem and Spirit of Chinese Philosophy 中国哲学的精神和问题 T...
- Day 12 神句文档 The team contends that these bear more than a...