上一篇提到,快速排序的平均时间复杂度是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)的更多相关文章

  1. 排序算法四:快速排序(Quicksort)

    快速排序(Quicksort),因其排序之快而得名,虽然Ta的平均时间复杂度也是O(nlgn),但是从后续仿真结果看,TA要比归并排序和堆排序都要快. 快速排序也用到了分治思想. (一)算法实现 pr ...

  2. Javascript算法系列之快速排序(Quicksort)

    原文出自: http://www.nczonline.net/blog/2012/11/27/computer-science-in-javascript-quicksort/ https://gis ...

  3. js实现两种实用的排序算法——冒泡、快速排序

      分类:js (4443) (0) 零:数据准备,给定数组arr=[2,5,4,1,7,3,8,6,9,0]; 一:冒牌排序 1思想:冒泡排序思想:每一次对比相邻两个数据的大小,小的排在前面,如果前 ...

  4. 排序算法入门之快速排序(java实现)

    快速排序也是一种分治的排序算法.快速排序和归并排序是互补的:归并排序将数组分成两个子数组分别排序,并将有序的子数组归并以将整个数组排序,会需要一个额外的数组:而快速排序的排序方式是当两个子数组都有序时 ...

  5. js实现两种排序算法——冒泡、快速排序

    * 一:冒牌排序1思想:冒泡排序思想:每一次对比相邻两个数据的大小,小的排在前面,如果前面的数据比后面的大就交换这两个数的位置要实现上述规则需要用到两层for循环,外层从第一个数到倒数第二个数,内层从 ...

  6. python排序算法-冒泡和快速排序,解答阿里面试题

    ''常见的排序算法\ 插入排序/希尔排序/直接排序/堆排序 冒泡排序/快速排序/归序排序/基数排序 给定一个列表,将这个列表进行排序,要求:> 时间复杂度要小于O(n^2) 复杂度:1.时间复杂 ...

  7. 排序算法系列:快速排序算法JAVA版(靠谱、清晰、真实、可用、不罗嗦版)

    在网上搜索算法的博客,发现一个比较悲剧的现象非常普遍: 原理讲不清,混乱 啰嗦 图和文对不上 不可用,甚至代码还出错 为了不误人子弟耽误时间,推荐看一些靠谱的资源,如[啊哈!算法]系列: https: ...

  8. 【Java】 大话数据结构(15) 排序算法(2) (快速排序及其优化)

    本文根据<大话数据结构>一书,实现了Java版的快速排序. 更多:数据结构与算法合集 基本概念 基本思想:在每轮排序中,选取一个基准元素,其他元素中比基准元素小的排到数列的一边,大的排到数 ...

  9. 排序算法-Java实现快速排序算法

随机推荐

  1. (一:NIO系列)JAVA NIO 简介

    出处:JAVA NIO 简介 Java 中 New I/O类库 是由 Java 1.4 引进的异步 IO.由于之前老的I/O类库是阻塞I/O,New I/O类库的目标就是要让Java支持非阻塞I/O, ...

  2. Windows Server 搭建企业无线认证(NPS搭建)

    现代企业无线网络是必备,移动办公更是需求日益剧增.而带来的无线网络安全隐患随之而来,也是面临着巨大的挑战.所以对无线网络做接入认证是现在企业很迫切的需求. 上一遍已经说明了Radius认证方案:htt ...

  3. websocket和基于swoole的简易即时通讯

    这里描述个基于swoole的websocket 匿名群聊 UI <!DOCTYPE html> <html> <head> <meta charset=&qu ...

  4. linux grep 设置高亮显示

    [root@eric ~]# vi /etc/profile alias grep='grep --color=auto' [root@eric ~]# source /etc/profile

  5. more - 在显示器上阅读文件的过滤器

    总览 (SYNOPSIS) more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file ... ] 描述 (DESCRIPTION) More 是 ...

  6. Spring_搭建过程中遇到的问题

    先看一下问题: 1.在web.xml中配置Spring 加载Spring mvc的时候配置如下: <!--配置SpringMVC的前端控制器--> <servlet> < ...

  7. CentOS7安装mysql8.0编译报错集合

    以下都是我安装mysql8.0遇到的一些报错和解决方法 1.does not appear to contain CMakeLists.txt. 原因:mysql下载的源码包不对 解决方法:下载正确的 ...

  8. (转)nginx+redis实现接入层高性能缓存技术

    转自:https://blog.csdn.net/phil_code/article/details/79154271 1. OpenRestyOpenResty是一个基于 Nginx与 Lua的高性 ...

  9. mysql 注入绕过小特性

    1. 注释 Select /*多行(单行)注释*/ version(); Select version(); #单行注释 Select version(); -- 单行注释 (两划线之后必须有空格) ...

  10. CSS--使用CSS Grid(网格)布局

    .一 CSS Grid(网格布局)简介 CSS Grid 布局由两个核心组成部分是父元素和子元素,父元素 是实际的 grid(网格),子元素是 grid(网格) 内的内容.下面是一个父元素和六个子元素 ...