双向链表的快排(Java)

思路:

1、同数组快速排序思想

2、交换元素时,只交换元素的值,不交换指向的节点

 

代码:

package com.datastructure.link;

import java.util.Arrays;
import java.util.List;

/**
 * 双向链表快排 
 */
public class DoubleLinkedListQuickSort {

	static class Node {
		int value;
		Node pre;
		Node next;
		
		Node(int value) {
			this.value = value;
		}
		
        @Override
        public String toString() {
            if (this.next == null) {
                return String.valueOf(this.value);
            }
            return this.value + "->" + this.next.toString();
        }
	}
	
	/**
	 * 参数为头节点和尾节点 
	 */
	public static void quickSort(Node head, Node tail) {
		if (head == null || tail == null || head == tail || head.next == tail) {
			return;
		}
		
		if (head != tail) {
			Node mid = getMid(head, tail);
			quickSort(head, mid);
			quickSort(mid.next, tail);
		}
	}
	
	public static Node getMid(Node start, Node end) {
		int base = start.value;
		while (start != end) {
			while(start != end && base <= end.value) {
				end = end.pre;
			}
			start.value = end.value;
			while(start != end && base >= start.value) {
				start = start.next;
			}
			end.value = start.value;
		}
		start.value = base;
		return start;
	}
	
	/**
	 * 使用如内部实现使用双向链表的LinkedList容器实现的快排 
	 */
	public static void quickSort(List<Integer> list) {
		if (list == null || list.isEmpty()) {
			return;
		}
		quickSort(list, 0, list.size() - 1);
	}
	
	private static void quickSort(List<Integer> list, int i, int j) {
		if (i < j) {
			int mid = partition(list, i, j);
			partition(list, i, mid);
			partition(list,mid + 1, j);
		}
	}
	
	private static int partition(List<Integer> list, int i, int j) {
		int baseVal = list.get(i);
		while (i < j) {
			while (i < j && baseVal <= list.get(j)) {
				j--;
			}
			list.set(i, list.get(j));
			while (i < j && baseVal >= list.get(i)) {
				i++;
			}
			list.set(j, list.get(i));
		}
		list.set(i, baseVal);
		return i;
	}
	
	public static void main(String[] args) {
		Node node1 = new Node(5);
        Node node2 = new Node(4);
        Node node3 = new Node(5);
        Node node4 = new Node(2);
        Node node5 = new Node(1);
        Node node6 = new Node(0);
        
        node1.next = node2;
        node2.pre = node1;
        
        node2.next = node3;
        node3.pre = node2;
        
        node3.next = node4;
        node4.pre = node3;
        
        node4.next = node5;
        node5.pre = node4;
        
        node5.next = node6;
        node6.pre = node5;
        
        System.out.println("Origin link: " + node1);
        quickSort(node1, node6);
        System.out.println("Sorted link: " + node1);
		
		Integer[] l = {2,3,4,1,0,5};
		// 可在此处使用LinkedList容器存储
		List<Integer> list = Arrays.asList(l);
		quickSort(list);
		System.out.println("Sorted list: " + list);
	}

}

 

参考:

链表快排:https://blog.csdn.net/u010429424/article/details/77776731

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值