排序算法五:随机化快速排序(Randomized quicksort)
上一篇提到,快速排序的平均时间复杂度是O(nlgn),比其他相同时间复杂度的堆排序、归并排序都要快,但这是有前提的,就是假定要排序的序列是随机分布的,而不是有序的。实际上,对于已经排好的序列,如果用快速排序时间复杂度是O(n2)。为应对这样的有序序列,于是出现了本篇要讲的随机化快速排序(Randomized quicksort)。
快速排序在选主元(pivot)时,总是选择第一个;随机化快速排序的思想是,随机从序列中选择一个作为主元。
(一)算法实现
protected void quicksort(int[] array, int first, int last) {
int randomIndex = CommonUtils.getRandomInt(first, last);
CommonUtils.swap(array, first, randomIndex); int pivot = array[first];
int i = first;
int j = last - 1;
boolean serachBig = true;
while (i < j) {
if (serachBig) {
if (array[j] < pivot) {
array[i] = array[j];
i++;
serachBig = false;
} else {
j--;
}
} else {
if (array[i] > pivot) {
array[j] = array[i];
j--;
serachBig = true;
} else {
i++;
}
}
}
array[i] = pivot; if (i - first > 1) {
quicksort(array, first, i);
}
if (last - i > 2) {
quicksort(array, i + 1, last);
}
}
Randomized quicksort
1)对于任何输入序列,随机化快速排序的时间复杂度是O(nlgn)
2)考虑递归,随机化快速排序空间复杂度是O(logn),不是原地排序
3)随机化快速排序属于比较排序
4)随机化快速排序不是稳定排序算法
(二)仿真结果
下面比较快速排序和随机化快速排序,分别对随机序列和有序序列排序。
**************************************************
Number to Sort is:2500
Randomized sequence to sort is:{741988,773994,855169,757518,82329,596105,876316,561224,928992,721115...}
Cost time of 【QuickSort】 is(milliseconds):0
Sort result of 【QuickSort】:{250,786,1209,1434,2306,3074,3715,3800,4669,5510...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):1
Sort result of 【RandomizedQuickSort】:{250,786,1209,1434,2306,3074,3715,3800,4669,5510...}
Ordered sequence to sort is:{250,786,1209,1434,2306,3074,3715,3800,4669,5510...}
Cost time of 【QuickSort】 is(milliseconds):6
Sort result of 【QuickSort】:{250,786,1209,1434,2306,3074,3715,3800,4669,5510...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):0
Sort result of 【RandomizedQuickSort】:{250,786,1209,1434,2306,3074,3715,3800,4669,5510...}
**************************************************
Number to Sort is:25000
Randomized sequence to sort is:{791264,308128,451250,42835,620880,820510,650527,51751,716592,292370...}
Cost time of 【QuickSort】 is(milliseconds):2
Sort result of 【QuickSort】:{52,82,148,166,180,182,232,354,382,394...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):3
Sort result of 【RandomizedQuickSort】:{52,82,148,166,180,182,232,354,382,394...}
Ordered sequence to sort is:{52,82,148,166,180,182,232,354,382,394...}
Cost time of 【QuickSort】 is(milliseconds):650
Sort result of 【QuickSort】:{52,82,148,166,180,182,232,354,382,394...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):2
Sort result of 【RandomizedQuickSort】:{52,82,148,166,180,182,232,354,382,394...}
**************************************************
Number to Sort is:250000
Randomized sequence to sort is:{595090,163678,171858,249808,15138,951048,53215,611066,766255,454662...}
Cost time of 【QuickSort】 is(milliseconds):30
Sort result of 【QuickSort】:{1,1,8,8,10,11,11,17,31,32...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):55
Sort result of 【RandomizedQuickSort】:{1,1,8,8,10,11,11,17,31,32...}
Ordered sequence to sort is:{1,1,8,8,10,11,11,17,31,32...}
Cost time of 【QuickSort】 is(milliseconds):54,886
Sort result of 【QuickSort】:{1,1,8,8,10,11,11,17,31,32...}
Cost time of 【RandomizedQuickSort】 is(milliseconds):19
Sort result of 【RandomizedQuickSort】:{1,1,8,8,10,11,11,17,31,32...}
相关代码:
package com.cnblogs.riyueshiwang.sort; import java.util.Arrays; public class RandomizedQuickSort extends abstractSort {
@Override
protected void sort(int[] toSort) {
quicksort(toSort, 0, toSort.length);
} protected void quicksort(int[] array, int first, int last) {
int randomIndex = CommonUtils.getRandomInt(first, last);
CommonUtils.swap(array, first, randomIndex); int pivot = array[first];
int i = first;
int j = last - 1;
boolean serachBig = true;
while (i < j) {
if (serachBig) {
if (array[j] < pivot) {
array[i] = array[j];
i++;
serachBig = false;
} else {
j--;
}
} else {
if (array[i] > pivot) {
array[j] = array[i];
j--;
serachBig = true;
} else {
i++;
}
}
}
array[i] = pivot; if (i - first > 1) {
quicksort(array, first, i);
}
if (last - i > 2) {
quicksort(array, i + 1, last);
}
} public static void main(String[] args) {
for (int j = 0, n = 2500; j < 3; j++, n = n * 10) {
System.out
.println("**************************************************");
System.out.println("Number to Sort is:" + n);
int[] array = CommonUtils.getRandomIntArray(n, 1000000); System.out.print("Randomized sequence to sort is:");
CommonUtils.printIntArray(array); int[] array1 = Arrays.copyOf(array, n);
new QuickSort().sortAndprint(array1);
int[] array2 = Arrays.copyOf(array, n);
new RandomizedQuickSort().sortAndprint(array2); System.out.print("Ordered sequence to sort is:");
CommonUtils.printIntArray(array1);
new QuickSort().sortAndprint(array1);
new RandomizedQuickSort().sortAndprint(array1); } }
}
RandomizedQuickSort.java
package com.cnblogs.riyueshiwang.sort; import java.util.Random; public class CommonUtils {
private static Random random = new Random(); public static void printIntArray(int[] array) {
System.out.print('{'); int length = Math.min(array.length, 10);
for (int i = 0; i < length; i++) {
System.out.print(array[i]);
if (i != length - 1) {
System.out.print(',');
} else {
if (array.length > 10) {
System.out.print("...");
}
System.out.println('}');
}
}
} public static int[] getRandomIntArray(int size, int maxValue) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = random.nextInt(maxValue);
}
return array;
} public static void swap(int[] toSort, int i, int j) {
int temp = toSort[i];
toSort[i] = toSort[j];
toSort[j] = temp;
} /**
*
* @param first
* begin value
* @param last
* end value
* @return a pseudo random, uniformly distributed int value between first
* (inclusive) and last (exclusive)
*
*/
public static int getRandomInt(int first, int last) {
return random.nextInt(last - first) + first;
}
}
CommonUtils.java
排序算法五:随机化快速排序(Randomized quicksort)的更多相关文章
- 排序算法四:快速排序(Quicksort)
快速排序(Quicksort),因其排序之快而得名,虽然Ta的平均时间复杂度也是O(nlgn),但是从后续仿真结果看,TA要比归并排序和堆排序都要快. 快速排序也用到了分治思想. (一)算法实现 pr ...
- Javascript算法系列之快速排序(Quicksort)
原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...
- js实现两种实用的排序算法——冒泡、快速排序
分类:js (4443) (0) 零:数据准备,给定数组arr=[2,5,4,1,7,3,8,6,9,0]; 一:冒牌排序 1思想:冒泡排序思想:每一次对比相邻两个数据的大小,小的排在前面,如果前 ...
- 排序算法入门之快速排序(java实现)
快速排序也是一种分治的排序算法.快速排序和归并排序是互补的:归并排序将数组分成两个子数组分别排序,并将有序的子数组归并以将整个数组排序,会需要一个额外的数组:而快速排序的排序方式是当两个子数组都有序时 ...
- js实现两种排序算法——冒泡、快速排序
* 一:冒牌排序1思想:冒泡排序思想:每一次对比相邻两个数据的大小,小的排在前面,如果前面的数据比后面的大就交换这两个数的位置要实现上述规则需要用到两层for循环,外层从第一个数到倒数第二个数,内层从 ...
- python排序算法-冒泡和快速排序,解答阿里面试题
''常见的排序算法\ 插入排序/希尔排序/直接排序/堆排序 冒泡排序/快速排序/归序排序/基数排序 给定一个列表,将这个列表进行排序,要求:> 时间复杂度要小于O(n^2) 复杂度:1.时间复杂 ...
- 排序算法系列:快速排序算法JAVA版(靠谱、清晰、真实、可用、不罗嗦版)
在网上搜索算法的博客,发现一个比较悲剧的现象非常普遍: 原理讲不清,混乱 啰嗦 图和文对不上 不可用,甚至代码还出错 为了不误人子弟耽误时间,推荐看一些靠谱的资源,如[啊哈!算法]系列: https: ...
- 【Java】 大话数据结构(15) 排序算法(2) (快速排序及其优化)
本文根据<大话数据结构>一书,实现了Java版的快速排序. 更多:数据结构与算法合集 基本概念 基本思想:在每轮排序中,选取一个基准元素,其他元素中比基准元素小的排到数列的一边,大的排到数 ...
- 排序算法-Java实现快速排序算法
随机推荐
- C#设计模式:命令模式(Command Pattern)
一,什么是命令模式(Command Pattern)? 命令模式:将请求封装成命令对象,请求的具体执行由命令接收者执行: 二,如下代码 using System; using System.Colle ...
- 解决Vscode编辑器不能打开多标签页问题
问题描述:编辑代码时,初用vscode,不能打开多个文件:每打开一个文件,都会替换前面一个文件标签,很不方便切换编码: 想要的效果: 解决方式: 方法一: 找到setting.json文件,最外层花括 ...
- powerdesigner数据库设计
(1)创建物理数据模型 打开PowerDesigner,然后点击File-->New Model然后选择如下图所示的物理数据模型(物理数据模型的名字自己起,然后选择自己所使用的数据库即可) ( ...
- 2018-10-16-weekly
Algorithm 判断子序列 What 给定字符串 s 和 t ,判断 s 是否为 t 的子序列.如,"ace"是"abcde"的一个子序列,而"a ...
- 程序员要注意!现在是RSS复兴的时候了
一般来说,现代网络不乏恐怖,从无所不在的网络黑客到所有信息平台,再到各大平台的评论系统.不幸的是,我们建立的这个互联网并没有什么灵丹妙药.但任何人都厌倦了黑箱算法,控制你在网上看到的东西,一直存在但始 ...
- pycharm安装第三方库失败module 'pip' has no attribute 'main'
用的pycharm2017.3,新创建一个项目,在安装appium-python-client时报错module 'pip' has no attribute 'main'.通过强大的度娘,知道是pi ...
- [洛谷P4172] WC2006 水管局长
问题描述 SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一条从A至B的水 ...
- AutoLayout面试题记录-用NSLayoutConstraint写动画
import UIKit class ViewController: UIViewController { @IBOutlet weak var topY: NSLayoutConstraint! @ ...
- P2627 修剪草坪 (单调队列优化$dp$)
题目链接 Solution 70分很简单的DP,复杂度 O(NK). 方程如下: \[f[i][1]=max(f[j][0]+sum[i]-sum[j])\]\[f[i][0]=max(f[i-1][ ...
- 【HDOJ6665】Calabash and Landlord(dfs)
题意:二维平面上有两个框,问平面被分成了几个部分 x,y<=1e9 思路:分类讨论可以 但数据范围实在太小了,离散化以后随便dfs一下 #include<bits/stdc++.h> ...