区别主要在于如何将大于和小于等于pt_value的元素分组
测试样例case
https://leetcode.cn/problems/sort-an-array/
1. 正常交换搬移
超时
class Solution {
private void swap(int[] nums, int l, int r) {
int t = nums[l];
nums[l] = nums[r];
nums[r]= t;
}
private void quickSort(int[] nums, int l, int r) {
if (r <= l) {
return;
}
int ptVal = nums[l];
int lc = l;
int rc = r;
while (lc < rc) {
while (nums[lc] <= ptVal && lc < r) {
lc++;
}
while(nums[rc] > ptVal && l < rc) {
rc--;
}
if (rc > lc) {
swap(nums, lc, rc);
}
}
swap(nums, rc, l);
quickSort(nums, l, rc - 1);
quickSort(nums, rc+1, r);
}
public int[] sortArray(int[] nums) {
if (nums.length < 1) {
return nums;
}
quickSort(nums, 0, nums.length - 1);
return nums;
}
}
2. 随机pt
执行用时分布
2020ms 击败8.93%使用 Java 的用户
class Solution {
private void swap(int[] nums, int l, int r) {
int t = nums[l];
nums[l] = nums[r];
nums[r]= t;
}
private void quickSort(int[] nums, int l, int r) {
if (r <= l) {
return;
}
int rand = new Random().nextInt(r - l + 1);
swap(nums, l, l+rand);
int ptVal = nums[l];
int lc = l;
int rc = r;
while (lc < rc) {
while (nums[lc] <= ptVal && lc < r) {
lc++;
}
while(nums[rc] > ptVal && l < rc) {
rc--;
}
if (rc > lc) {
swap(nums, lc, rc);
}
}
swap(nums, rc, l);
quickSort(nums, l, rc - 1);
quickSort(nums, rc+1, r);
}
public int[] sortArray(int[] nums) {
if (nums.length < 1) {
return nums;
}
quickSort(nums, 0, nums.length - 1);
return nums;
}
}
3. 三指针
执行用时分布
1015
ms
击败
32.07%
使用 Java 的用户
class Solution {
private void swap(int[] nums, int l, int r) {
int t = nums[l];
nums[l] = nums[r];
nums[r]= t;
}
private void quickSort(int[] nums, int l, int r) {
if (r <= l) {
return;
}
// 小于区间右边界
int i = l;
// 大于区间左边界
int rl = r + 1;
// 指针
int cursor = l + 1;
int selVal = nums[l];
while (cursor < rl) {
if (nums[cursor] > selVal) {
rl -= 1;
swap(nums, rl, cursor);
} else if (nums[cursor] < selVal){
swap(nums, i+1, cursor);
cursor += 1;
i += 1;
} else {
cursor+=1;
}
}
swap(nums, i, l);
quickSort(nums, l, i-1);
quickSort(nums, rl, r);
}
public int[] sortArray(int[] nums) {
if (nums.length < 1) {
return nums;
}
quickSort(nums, 0, nums.length - 1);
return nums;
}
}
链表快排
链表快排1
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
private ListNode quickSort( ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode newH = partion(head);
ListNode next = head.next;
head.next = null;
ListNode l = quickSort(newH);
ListNode r = quickSort(next);
return merge(l, r);
}
private ListNode merge(ListNode l1, ListNode l2) {
ListNode p = l1;
while (p.next != null) {
p = p.next;
}
p.next = l2;
return l1;
}
private ListNode partion( ListNode next) {
if (next == null || next.next == null) {
return next;
}
ListNode pt = next;
int ptVal = pt.val;
ListNode cursor = next.next;
next.next = null;
ListNode left = new ListNode();
ListNode ll = left;
ListNode right = new ListNode();
ListNode rr = right;
while (cursor != null) {
if (cursor.val < ptVal) {
left.next = cursor;
left = left.next;
} else {
right.next = cursor;
right = right.next;
}
ListNode t = cursor.next;
cursor.next = null;
cursor = t;
}
left.next = pt;
pt.next = rr.next;
return ll.next;
}
public ListNode sortList(ListNode head) {
return quickSort(head);
}
}
链表快排2
class Solution {
private void quickSort(ListNode head, ListNode prev, ListNode after) {
if (head == after || head.next == after) {
return;
}
ListNode left = partition(head, prev, after);
quickSort(prev.next, prev, head);
quickSort(head.next, head, after);
}
private ListNode partition(ListNode head, ListNode prev, ListNode after) {
if (head == null || head.next == null) {
return null;
}
int ptVal = head.val;
ListNode right = new ListNode();
ListNode left = prev;
ListNode cursor = head;
while (cursor != after) {
ListNode t = cursor.next;
if (cursor.val >= ptVal) {
right.next = cursor;
right = right.next;
} else {
left.next = cursor;
left = left.next;
}
cursor.next = null;
cursor = t;
}
right.next = after;
left.next = head;
return (left == prev) ? null : left;
}
public ListNode sortList(ListNode head) {
ListNode vir = new ListNode();
vir.next = head;
quickSort(head, vir, null);
return vir.next;
}
}