快速排序-单链表

本文介绍了一种在单向链表上实现快速排序的方法。通过定义节点类和一系列操作,如分区函数和快速排序函数,实现了对链表的有效排序。最后通过测试样例验证了算法的正确性。

快速排序-单链表

题目

对单向链表快速排序

思路

程序

package base;

import org.junit.Test;

/**
 * description:
 *
 * @author liyazhou
 * @since 2017-10-16 10:45
 */


public class LinkedQuickSort {

    private class Node{
        int key;
        Node next;
        public Node(int key_){
            key = key_;
        }
    }

    /**
     * 一次快排操作,链表分为两部分,前一部分小于枢轴结点,后一部分大于等于枢轴结点
     * @param start 起始结点
     * @param end 终止结点
     * @return 切分结点
     */
    private Node partition(Node start, Node end){
        int key = start.key;
        Node first = start;
        Node second = start.next;

        // 将较小的数字移动到前面
        while (second != end){
            // 从后面找到一个较小的结点,和前面一个较大的结点交换
            if (second.key < key){  // 有条件的移动第一个指针
                // 第一个指针向后移动一位,first指向结点的值是大于或等于second指向结点的值
                first = first.next;

                // 第一个指针和第二个指针指向的结点的值交换
                int temp = first.key;
                first.key = second.key;
                second.key = temp;
            }
            // 第二个指针向后移动一位
            second = second.next;
        }

        // 找到切分位置,并将该结点的值和枢轴结点的值交换
        int temp = first.key;
        first.key = start.key;
        start.key = temp;

        return first;  // 返回切分位置(枢轴结点的新位置)
    }

    public void quickSort(Node start, Node end){
        if (start != end){
            Node partition = partition(start, end);
            quickSort(start, partition);
            quickSort(partition.next, end);
        }
    }


    private void printLinkedList(Node start){
        for (Node node = start; node.next != null; node = node.next){
            System.out.print(node.key + " -> ");
        }
        System.out.println();
    }


    private Node[] generateLinkedList(){
        int[] arr = {2, 1, 5, 3,4};
        Node node01 = new Node(arr[0]);
        Node node02 = new Node(arr[1]);
        Node node03 = new Node(arr[2]);
        Node node04 = new Node(arr[3]);
        Node node05 = new Node(arr[4]);

        node01.next = node02;
        node02.next = node03;
        node03.next = node04;
        node04.next = node05;

        return new Node[]{node01, node05};
    }

    @Test
    public void test(){
        Node[] endpoints = generateLinkedList();
        Node start = endpoints[0];
        Node end = endpoints[1];
        printLinkedList(start);

        quickSort(start, end);

        printLinkedList(start);
    }
}

测试结果

2 -> 1 -> 5 -> 3 -> 
1 -> 2 -> 3 -> 5 -> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值