快速排序中的分割算法实现

来源:互联网 发布:bootstrap 数据绑定 编辑:程序博客网 时间:2024/06/02 05:30

这里介绍快速排序使用到的两种分割算法。

对于快速排序而言,先选定一个枢轴元素,以枢轴元素为基准比枢轴元素小的元素放在枢轴元素的左边,比枢轴元素大的元素放在枢轴元素的右边。这就是一次分割过程。

1,先介绍第一种分割算法

该算法是《算法导论》中描述的PARTITION过程。这个分割的思想应该有很多应用,比如应用到快速排序中,再比如应用到如果获取中位数(或者给定一组数,找出其中第K小的元素)

该分割算法的思想是,始终选定数组中的最后一个元素作为枢轴元素,设置指针 i 初始值为数组起始元素索引减1,设置指针 j 由数组下标从低到高扫描整个数组,若遇到的元素小于枢轴元素则 i 自增然后交换 i 数组元素的值和i 数组元素的值;若遇到的元素大于 枢轴元素则i指针不动,只有j指针自增。此算法的时间复杂度为O(n),空间复杂度为O(1)。

具体的JAVA实现代码如下,算法伪代码参考《算法导论》

/*     * split array     * this method always use the last elements as pivot.     * when the method finished, the left elements of array smaller than pivot.     * and the right elements of array larger than pivot.     * @return  the pivot element in array     */    public int partition(int arr[], int low, int high) {        int pivot = arr[high];        int i = low - 1;        for (int j = low; j < high; j++) {            if (arr[j] <= pivot) {                i++;                swap(arr, i, j);            }        }        swap(arr, i + 1, high);        return i + 1;    }

/* * switch two elements of arry * @param int[] arr an array for switch * @param int i, j index of array *  */private void swap(int[] arr, int i, int j){int tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}

2,第二种分割算法的实现思路

假设总是以第一个元素作为枢轴元素,设置两个指针low, high。low 指向数组的第一个元素,high 指向数组的最后一个元素。high 指针从后向前扫描,若碰到的元素大于枢轴元素则 high 直接自减,若碰到的元素小于枢轴元素则将该元素与枢轴元素交换并break 此次扫描。

high 指针的当次扫描被中断后,low 指针开始由前向后扫描,若碰到的元素小于枢轴元素则 low 直接自减,若碰到的元素大于枢轴元素则将该元素与枢轴元素交换并break 此次扫描。

直至low 指针 超过 high指针为止。

具体的实现参考:使用JAVA泛型实现快速排序


第一种分割算法的完整实现+测试版。

public class Partition {/* * split array * this method always use the last elements as pivot. * when the method finished, the left elements of array smaller than pivot. * and the right elements of array larger than pivot. * @return  the pivot element in array */public int partition(int arr[], int low, int high) {int pivot = arr[high];int i = low - 1;for (int j = low; j < high; j++) {if (arr[j] <= pivot) {i++;swap(arr, i, j);}}swap(arr, i + 1, high);return i + 1;}/* * create a random pivot and use it to partition array */public int randomPartition(int[] arr, int low, int high){int rand = getRandom(low, high);swap(arr, arr[rand], arr[high]);//arr[high] is a random and will be as pivotreturn partition(arr, low, high);}/* * return a random between min and max----[min, max] */private int getRandom(int min, int max){Random random = new Random();return  random.nextInt(max + 1) % (max-min+1) + min;}/* * switch two elements of arry * @param int[] arr an array for switch * @param int i, j index of array *  */private void swap(int[] arr, int i, int j){int tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}public static void main(String[] args) {Partition p = new Partition();int arr[] = {3,5,9,6,2,8};int result = p.partition(arr, 0, arr.length - 1);System.out.println(result);System.out.println(arr);}}


借助partition方法,实现快速排序就很容易了。代码如下:

public void quickSort(int[] arr, int low, int high){if(low < high){int pivot_location = partition(arr, low, high);quickSort(arr, low, pivot_location - 1);quickSort(arr, pivot_location + 1, high);}}





0 0
原创粉丝点击