从归并排序引申到排序链表
归并排序
递归版
public static void mergeSort(int[] nums) {
mergeSortHelper(0, nums.length, nums);
}
private static void mergeSortHelper(int low, int high, int[] nums) {
if (low >= high || high - low == 1) {
return;
}
int mid = (low+high)/2;
mergeSortHelper(low, mid, nums);
mergeSortHelper(mid, high, nums);
merge(low, mid, high, nums);
}
private static void merge(int low, int mid, int high, int[] nums) {
int i = low;
int j = mid;
int t = high - low;
int[] extra = new int[t];
int k = 0;
while (i < mid && j < high) {
if (nums[i] <= nums[j]) {
extra[k++] = nums[i++];
} else {
extra[k++] = nums[j++];
}
}
while (i < mid) {
extra[k++] = nums[i++];
}
while (j < high) {
extra[k++] = nums[j++];
}
for (int x = 0; x < t; x++) {
nums[low+x] = extra[x];
}
}
非递归版
public static void mergeSort2(int[] nums) {
for (int size = 1; size < nums.length; size *= 2) {
for (int i = 0; i < nums.length; i += 2*size) {
int n1 = i;
int n2 = i+size;
if (n2 > nums.length) {
continue;
}
int next = n2 + size;
if (next > nums.length) {
next = nums.length;
}
merge(nums, n1, n2, next);
}
}
}
排序链表
OJ链接
递归版
public class $148 {
public ListNode sortList(ListNode head) {
return sortList(head, null);
}
private ListNode sortList(ListNode head, ListNode tail) {
if (head == null) {
return head;
}
if (head.next == tail) {
head.next = null;
return head;
}
ListNode fast = head;
ListNode slow = head;
while (fast != tail && fast.next != tail) {
fast = fast.next.next;
slow = slow.next;
}
ListNode mid = slow;
ListNode list1 = sortList(head, mid);
ListNode list2 = sortList(mid, tail);
ListNode sorted = merge(list1, list2);
return sorted;
}
private ListNode merge(ListNode list1, ListNode list2) {
ListNode mergedHead = new ListNode(0);
ListNode temp = mergedHead;
ListNode temp1 = list1;
ListNode temp2 = list2;
while (temp1 != null && temp2 != null) {
if (temp1.val < temp2.val) {
temp.next = temp1;
temp1 = temp1.next;
} else {
temp.next = temp2;
temp2 = temp2.next;
}
temp = temp.next;
}
if (temp1 != null) {
temp.next = temp1;
} else {
temp.next = temp2;
}
return mergedHead.next;
}
}
非递归版
public class $148 {
public ListNode sortList2(ListNode head) {
int len = 0;
ListNode node = head;
while (node != null) {
node = node.next;
len++;
}
ListNode dummyHead = new ListNode(-1);
dummyHead.next = head;
for (int size = 1; size < len; size *= 2) {
ListNode prev = dummyHead;
ListNode cur = dummyHead.next;
while (cur != null) {
ListNode l1 = cur;
for (int i = 1; i < size && cur.next != null; i++) {
cur = cur.next;
}
ListNode l2 = cur.next;
cur.next = null;
cur = l2;
for (int i = 1; i < size && cur != null && cur.next != null; i++) {
cur = cur.next;
}
ListNode next = null;
if (cur != null) {
next = cur.next;
cur.next = null;
}
ListNode merged = merge(l1, l2);
prev.next = merged;
while (prev.next != null) {
prev = prev.next;
}
cur = next;
}
}
return dummyHead.next;
}
private ListNode merge(ListNode list1, ListNode list2) {
ListNode mergedHead = new ListNode(0);
ListNode temp = mergedHead;
ListNode temp1 = list1;
ListNode temp2 = list2;
while (temp1 != null && temp2 != null) {
if (temp1.val < temp2.val) {
temp.next = temp1;
temp1 = temp1.next;
} else {
temp.next = temp2;
temp2 = temp2.next;
}
temp = temp.next;
}
if (temp1 != null) {
temp.next = temp1;
} else {
temp.next = temp2;
}
return mergedHead.next;
}
}