Java: 實現順序表和單鏈表的快速排序

快速排序

  • 快速排序原理

  快速排序(Quick Sort)的基本思想是,經過一趟排序將待排記錄分割成獨立的兩部分,其中一部分記錄的關鍵字均比另外一部分記錄的關鍵字小,則可對這兩部分記錄繼續進行排序,以達到整個序列有序的目的。java

  • 主程序
package Sort;

public class QuickSort {
	
	private void Qsort(int[] list, int low, int high){
		int privot;
		if(low < high) {
			print(list);
			/*算出樞軸值privot*/
			privot = Partition(list, low, high);
			/*對低子表遞歸排序*/
			Qsort(list, low, privot-1);
			/*對高子表遞歸排序*/
			Qsort(list, privot+1, high);			
		}
	}
	
	private int Partition(int[] list, int low, int high){
		/*用表的第一個記錄作樞軸記錄*/
		int privotkey = list[low];
		while (low < high){
			while (low < high && list[high] > privotkey)
				high--;			
			/*將比樞軸記錄小的記錄交換到低端*/
			swap(list, low, high);
			while (low < high && list[low] < privotkey)
				low++;
			/*將比樞軸記錄大的記錄交換到高端*/
			swap(list, low, high);			
		}
		/*返回樞軸所在的位置*/
		return low;
	}
	
	private void swap(int [] list,int j, int i){
		int temp;
		temp = list[i];
		list[i] = list[j];
		list[j] = temp;
	}	
	
	private void print(int[] data) {
		System.out.println("當前list數組的值:");
		for (int i = 0; i < data.length; i++) {
			System.out.print(data[i] + "\t");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		int[] list = {50,40,80,30,60,70,10,90,20};
		QuickSort qs = new QuickSort();
		qs.Qsort(list, 0, list.length-1);
		qs.print(list);
	}
}

  

  • Qsort()分析
	private void Qsort(int[] list, int low, int high){
		int privot;
		if(low < high) {
			print(list);
			/*算出樞軸值privot*/
			privot = Partition(list, low, high);
			/*對低子表遞歸排序*/
			Qsort(list, low, privot-1);
			/*對高子表遞歸排序*/
			Qsort(list, privot+1, high);			
		}
	}

  

  • Partition()分析
	private int Partition(int[] list, int low, int high){
		/*用表的第一個記錄作樞軸記錄*/
		int privotkey = list[low];
		while (low < high){
			while (low < high && list[high] > privotkey)
				high--;			
			/*將比樞軸記錄小的記錄交換到低端*/
			swap(list, low, high);
			while (low < high && list[low] < privotkey)
				low++;
			/*將比樞軸記錄大的記錄交換到高端*/
			swap(list, low, high);			
		}
		/*返回樞軸所在的位置*/
		return low;
	}

  

  • 輸出結果
當前list數組的值:
50	40	80	30	60	70	10	90	20	
當前list數組的值:
20	40	10	30	50	70	60	90	80	
當前list數組的值:
10	20	40	30	50	70	60	90	80	
當前list數組的值:
10	20	30	40	50	70	60	90	80	
當前list數組的值:
10	20	30	40	50	60	70	90	80	
當前list數組的值:
10	20	30	40	50	60	70	80	90	

  

  • 時間複雜度分析

  最優狀況下,快速排序算法的時間複雜度爲O(nlogn),空間複雜度也爲O(nlogn)算法

  •  單鏈錶快速排序實現
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode sortList(ListNode head) {
        if(head == null || head.next == null) return head;
        
        ListNode left = new ListNode(0), leftHead = left;
        ListNode right = new ListNode(0), rightHead = right;
        ListNode mid = new ListNode(0), midHead = mid;
        int val = head.val;
        
        while(head != null) {
            if(head.val < val) {
                left.next = head;
                left = head;
            } else if(head.val > val) {
                right.next = head;
                right = head;
            } else {
                mid.next = head;
                mid = head;
            }
            head = head.next;
        }
        
        left.next = null;
        right.next = null;
        mid.next = null;
        return merge(sortList(leftHead.next), midHead.next, sortList(rightHead.next));
    }
    
    public ListNode merge(ListNode left, ListNode mid, ListNode right) {
        ListNode leftTail = getTail(left);
        ListNode midTail = getTail(mid);
        midTail.next = right;
        if(leftTail != null) {
            leftTail.next = mid;
            return left;
        } else {
            return mid;
        }
    }
    
    public ListNode getTail(ListNode head) {
        if(head == null) return head;
        while(head.next != null) {
            head = head.next;
        }
        return head;
    }
}
相關文章
相關標籤/搜索