public static void quick_sort(int[] nums, int l, int r) { if(l>=r) return; int m = nums[l+r>>1],left = l-1,right = r+1; //注意此处的标兵取值 必须与下面递归的标兵相反 l - right while (left<right){ do left++; while (nums[left] < m); do right--; while (nums[right] > m); if(left<right) { int tmp = nums[left]; nums[left] = nums[right]; nums[right] = tmp; } } quick_sort(nums,l,right); quick_sort(nums,right+1, r); }
import java.util.ArrayList; import java.util.Comparator; import java.util.List; // 对结构体排序 class Node implements Comparable<Node>{ int x; int y; public Node(int x, int y) { this.x = x; this.y = y; } // 先按 x 排序 再按 y排序 @Override public int compareTo(Node other) { if(other.x > this.x) return 1; else if(other.x < this.x) return -1; else { return other.y - this.y; } } }
public class NodeSort { public static void main(String[] args) { List<Node> list = new ArrayList<>(); list.sort(new Comparator<Node>() { @Override public int compare(Node o1, Node o2) { return o1.x-o2.x; } }); } }