递归、非递归、单向、单链表——快排

这篇博客介绍了四种快速排序的实现方式:非递归、递归、单向和链表快排。每种实现都包含核心的partition函数和交换元素的逻辑。通过随机选取基准值,这些算法实现了数组和链表的排序。代码示例详细展示了每种方法的步骤,适合学习和理解快速排序算法。
摘要由CSDN通过智能技术生成

快速排序

非递归快排

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);
        //打印看看链表是否建立成功
        //print(head);
        //获取链表尾结点
        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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值