1

快速排序

介绍

快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下,排序 n 个项目要Ο(n log n)次比较。在最坏状况下则需要Ο(n2)次比较,但这种状况并不常见。事实上,快速排序通常明显比其他Ο(n log n) 算法更快,因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来。

快速排序使用分治法(Divide and conquer)策略来把一个串行(list)分为两个子串行(sub-lists)。

 

算法步骤

  1. 从数列中挑出一个元素,称为 “基准”(pivot),

  2. 重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。

  3. 递归地(recursive)把小于基准值元素的子数列和大于基准值元素的子数列排序。

递归的最底部情形,是数列的大小是零或一,也就是永远都已经被排序好了。虽然一直递归下去,但是这个算法总会退出,因为在每次的迭代(iteration)中,它至少会把一个元素摆到它最后的位置去。

 

算法实现

public class QuickSort {

    public void sort(int[] a, int left, int right) {
//        print(a);
        if (right < left) return;
        
        int key = a[left];
        
        int low = left;
        int high = right;
        while (low < high) {
            while (low < high && a[high] >= key) {
                high--;
            }
            a[low] = a[high];
            
            while(low < high && a[low] <= key){
                low++;
            }
            a[high] = a[low];
        }
        
        a[low] = key;
        
        print(a);
        
        sort(a, left, low - 1);
        sort(a, low + 1, right);
    }
    
    private void print(int[] a) {
        for (int i = 0; i < a.length ; i++) {
            System.out.print(a[i]);
            System.out.print(" ");
        }
        System.out.println("");
        System.out.println("=============");
    }
    
    public static void main(String[] args) {
        QuickSort qs = new QuickSort();
        int[] a = new int[]{2, 2, 4, 9, 3, 6 ,7, 1, 5};
        qs.print(a);
        qs.sort(a, 0, a.length - 1);
    }
}
posted @ 2016-10-11 17:39  TinyKing  阅读(242)  评论(0编辑  收藏  举报
我要啦免费统计