quick sort and randomized quick sort

    The idea of classic quick sort is to divided the original array to two parts, and the when you doing the partition, you should make sure the element in left subarray always less than a pivot, and the element in right subarray always greater than a pivot. Then make the new subarrays as input and recursively call the method. The different between the classic quick sort and randomized quick sort is how to choose a suitable pivot. For the classic quick sort you can choose the first one or the last one as a pivot, but for the randomized quick sort, you need to choose the pivot depends a random-number generator.

    For the classic quick sort, time is often O(nlgn), but if the input is ordered already, it will be down to the O(n2), so the randomized quick sort was invented, it produce a pivot from a random-number generator, and make sure the running time is independent of the input ordering, it makes no assumptions about the input distribution. The time is always O(nlgn).

The detail steps of classic quick sort algorithm are as follow:

1. partition the array to two parts. 

    1.1.choose the first element as a pivot. Make sure there are two indicators to do the moving, make left indicator point to the pivot, and right indicator point to the next element behind the pivot.

    1.2.check the element that the right indicator is pointing. if it is less than the pivot, move the left indicator to the next one, and swap the elements pointing by two indicators. if it is greater than the pivot, only move the right indicator to the next one, and hold on the left indicator.

    1.3.after the checking, the element position that is pointing by the left indicator is where the pivot should be. so swap the first element with the left indicator pointing one and return that position at last.

2. make the two subarrays as new input parameters and call the method respectively.

The detail steps of randomized quick sort algorithm are very similar with the classic quick sort. The difference is only you should use a random-number generator to produce a pivot.

The complexity of randomized quick sort is always O(nlgn).

The following is the implement of classic quick sort by java.

private void quickSort(int[] array, int left, int right){
if(left < right){
int pi = partition4QuickSort(array,left,right);
quickSort(array,left,pi-1);
quickSort(array,pi+1,right);
}
}
private int partition4QuickSort(int[] array, int left, int right){
int pivot = array[left];
int i = left;
for(int j = i + 1;j < right;j++){
if(array[j] < pivot){
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i];
array[i] = array[left];
array[left] = temp;
return i;

}

The following is the implement of the randomized quick sort.

private void randomizedQuickSort(int[] array, int left, int right){
    if(left < right){
        int pi = partition4RandomizedQuickSort(array,left,right);
        randomizedQuickSort(array,left,pi-1);
        randomizedQuickSort(array,pi+1,right);
    }
}
private int partition4RandomizedQuickSort(int[] array, int left, int right){
    Random random = new Random();
    int r = random.nextInt(right-left)+left;
    int temp = array[left];
    array[left] = array[r];
    array[r] = temp;
    return partition4QuickSort(array,left,right);
}
private int partition4QuickSort(int[] array, int left, int right){
    int pivot = array[left];
    int i = left;
    for(int j = i + 1;j < right;j++){
        if(array[j] < pivot){
            i++;
            int temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }
    int temp = array[i];
    array[i] = array[left];
    array[left] = temp;
    return i;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值