快速排序
非递归快排
import java.util.Arrays;
import java.util.Stack;
public class QuickSort {
public static int[] sort(int[] arr){
Stack<Integer> stack = new Stack<>();
stack.push(0);
stack.push(arr.length - 1);
while (!stack.isEmpty()){
int right = stack.pop();
int left = stack.pop();
int baseIndex = left + (int) (Math.random() * (right - left + 1));
swap(arr,left,baseIndex);
baseIndex = partition(arr,left,right);
if(left < baseIndex - 1){
stack.push(left);
stack.push(baseIndex - 1);
}
if(right > baseIndex + 1){
stack.push(baseIndex + 1);
stack.push(right);
}
}
return arr;
}
private static int partition(int[] arr,int left,int right){
int baseIndex = left;
int base = arr[baseIndex];
while(left < right){
while (left < right && arr[right] >= base){
right--;
}
while(left < right && arr[left] <= base){
left++;
}
if(left < right){
swap(arr,left,right);
}
}
swap(arr,baseIndex,left);
return left;
}
private static void swap(int[] arr,int a,int b){
int tmp = arr[a];
arr[a] = arr[b];
arr[b] = tmp;
}
public static void main(String[] args) {
int[] arr = {5,9,4,3,1,0,3,9,10};
QuickSort.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
递归快排
import java.util.Arrays;
public class RecurQuickSort {
public static int[] sort(int[] arr){
quickSort(arr,0,arr.length - 1);
return arr;
}
private static void quickSort(int[] arr, int left, int right){
if(left >= right){
return;
}
int base = left + (int) (Math.random() * (right - left + 1));
swap(arr,left,base);
base = partition(arr,left,right);
quickSort(arr,left,base - 1);
quickSort(arr,base + 1,right);
}
private static void swap(int[] arr,int a,int b){
int tmp = arr[a];
arr[a] = arr[b];
arr[b] = tmp;
}
private static int partition(int[] arr, int left,int right){
int baseIndex = left;
int base = arr[baseIndex];
while(left < right){
while (left < right && arr[right] >= base){
right--;
}
while (left < right && arr[left] <= base){
left++;
}
swap(arr,left,right);
}
swap(arr,baseIndex,left);
return left;
}
public static void main(String[] args) {
int[] arr = {5,9,4,3,1,0,3,9,10};
QuickSort.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
单向快排
public class SingleQuickSort {
private static void swap(int[] arr,int a,int b){
int tmp = arr[b];
arr[b] = arr[a];
arr[a] = tmp;
}
private static int partition(int[] arr, int left, int right) {
int i = left;
int j = i + 1;
int tmp = arr[left];
while (j <= right){
if(arr[j] <= tmp){
i++;
swap(arr,i,j);
}
j++;
}
swap(arr,left,i);
return i;
}
private static void quickPass(int[] arr, int left, int right) {
if(left < right){
int pos = partition(arr,left,right);
quickPass(arr,left,pos - 1);
quickPass(arr,pos + 1,right);
}
}
public static void quickSort(int[] arr){
quickPass(arr,0,arr.length - 1);
}
public static void main(String[] args) {
int[] arr = {1,5,7,9,4,3,6,8,2};
quickSort(arr);
System.out.println(Arrays.toString(arr));
}
}
单链表快排
public class LinkedQuickSort {
private static Node tail = null;
public static void main(String[] args) {
int[] nums = {3,2,5,8,4,7,6,9};
Node head = buildLinkedList(nums);
System.out.println();
quickSort(head,null);
print(head);
}
public static Node getPartion(Node head,Node end) {
int key = head.value;
Node p = head;
Node q = p.next;
while(q != end){
if(q.value < key){
p = p.next;
swap(p,q);
}
q = q.next;
}
swap(p, head);
return p;
}
public static void swap(Node p,Node q){
int tmp = p.value;
p.value = q.value;
q.value = tmp;
}
public static void quickSort(Node head,Node end){
if(head != end){
Node partion = getPartion(head,end);
quickSort(head, partion);
quickSort(partion.next, end);
}
}
public static Node buildLinkedList(int[] nums) {
Node head = null;
head = new Node(nums[0], null);
tail = head;
for(int i = 1;i < nums.length;i++){
Node tmp = new Node(nums[i], null);
tail.next = tmp;
tail = tail.next;
}
return head;
}
public static void print(Node head){
for(Node p = head;p != null;p = p.next){
System.out.print(p.value+" ");
}
}
}
class Node{
int value;
Node next;
public Node(int value,Node next){
this.value = value;
this.next = next;
}
}