今日算法:随机化快排RandomizedQuickSort

  基础工作swap交换和partition分治

/*
*交换数组的两个元素
*fromIndex和toIndex为要交换的两个元素的索引
*/
void swap(int *numArray,int fromIndex,int toIndex)
{
int temp=numArray[fromIndex];
numArray[fromIndex]=numArray[toIndex];
numArray[toIndex]=temp;
} int partition(int *numArray,int head,int tail)
{
int x=numArray[tail];
int i=head-;
for(int j=head;j<tail;j++)
{
if(numArray[j]<=x)
{
i++;
swap(numArray,i,j);
}
}
swap(numArray,i+,tail);
return i+;
}

  随机选择主元,快排

int randomizedPartition(int *numArray,int head,int tail)
{
int i=rand()%(tail-head+)+head;
swap(numArray,tail,i);
return partition(numArray,head,tail);
} void randomizedQuickSort(int *numArray,int head,int tail)
{
if(head<tail)
{
int q=randomizedPartition(numArray,head,tail);
randomizedQuickSort(numArray,head,q);
randomizedQuickSort(numArray,q+,tail);
}
}

测试及结果:

#include "stdafx.h"
#include <iostream>
#include "RandomizedQuickSort.h" using namespace std;
using namespace dksl;
int _tmain(int argc, _TCHAR* argv[])
{
int *a=new int[];
for(int i=;i<;i++)
a[i]=rand();
cout<<"排序前:";
for(int i=;i<;i++)
cout<<a[i]<< " ";
cout<<endl;
randomizedQuickSort(a,,);
cout<<"排序后:";
for(int i=;i<;i++)
cout<<a[i]<< " ";
cout<<endl;
system("PAUSE");
return ;
}

必须注意的是,此算法排序的数组中不能出现重复的元素。

《算法导论》——随机化快排RandomizedQuickSort的更多相关文章

  1. 《算法导论》——重复元素的随机化快排Optimization For RandomizedQuickSort

    昨天讨论的随机化快排对有重复元素的数组会陷入无限循环.今天带来对其的优化,使其支持重复元素. 只需修改partition函数即可: int partition(int *numArray,int he ...

  2. MIT算法导论——第四讲.Quicksort

    本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...

  3. 《算法导论》——顺序统计RandomizedSelect

    RandomizedSelect.h: #include <stdlib.h> namespace dksl { /* *交换 */ void Swap(int* numArray,int ...

  4. 排序--QuickSort 快排

    Quick の implementation 快排,就像它的名字一定,风一样的快.基本上算是最快的排序算法了.快排的基本思想是选择一个切分的元素.把这个元素排序了.所有这个元素左边的元素都小于这个元素 ...

  5. 基于visual Studio2013解决算法导论之011快排改良

     题目 快排改良 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...

  6. 基于visual Studio2013解决算法导论之010快排中应用插入排序

     题目 快排中引用插入排序 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...

  7. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  8. 冒泡,快排算法之javascript初体验

    引子:javascript实际使用的排序算法在标准中没有定义,可能是冒泡或快排.不用数组原生的 sort() 方法来实现冒泡和快排. Part 1:冒泡排序(Bubble Sort) 原理:临近的两数 ...

  9. scala写算法-快排

    快排算法很经典,今天用scala的函数式思维来整理一下并实现: def qsort(list: List[Int]):List[Int]=list match { case Nil=>Nil c ...

随机推荐

  1. @lazy注解处理循环注入问题

    @Service public class A extends GenericBaseService { @Autowired private B b; } @Service public class ...

  2. 《JavaScript Dom 编程艺术》读书笔记-第9章

    SS-DOM,本章内容: style属性 如何检索样式 如何改变样式三页一体的网页 结构层:由HTML或XHTML之类的标记语言负责创建.标签(tag)也就是尖括号里的单词,对网页内容的语义含义做出了 ...

  3. React Native backgroundColor 的颜色值

    React Native 的 css 跟 web开发中css还是很相似的,其中 backgroundColor 支持的颜色值与 css 一致,为方便查找写在这里. 其他样式可以参考 CSS 样式网站 ...

  4. centos7.5 安装mysql8.0.13

    在Linux系统上使用rpm包管理器安装mysql Installing MySQL on Linux Using RPM Packages 环境:CentOS Linux release 7.4.1 ...

  5. UITabView使用详解

    在开发iphone的应用时基本上都要用到UITableView,这里讲解一下UITableView的使用方法及代理的调用情况 - (void)viewDidLoad { [super viewDidL ...

  6. Python 学习之路的前言

    做为一个编程小白,除了大三的时候考了VB的二级之后,就在也没有接触过其它有关计算机之类的知识.考入材料的研究生之后,越来越觉得自己不想继续这个行业,选择计算机作为自己以后要走的路,所下的决心所用的时间 ...

  7. ArcSDE账户频繁被锁定(Oracle显示12560协议适配器错误)

    最近遇到了一个比较奇葩的问题,启动系统的时候无法显示地图服务,查找原因时发现无法连接Oracle.出现以下错误: 之前遇到这种问题,通常是由于同时安装了64位和32位Oracle客户端,且二者的环境变 ...

  8. c# ef

    找出不同项 ).ToList(); resultMsg = string.Join(",", query.select(p=>p.key).ToList())

  9. Go Example--panic

    package main import "os" func main() { //panic会中断程序执行,在此处一直往上抛panic,需要上游的recover来捕获 panic( ...

  10. ios-UILabel居中随内容自适应,后面的控件跟在其后

    如图绿蓝框所示,UILabel显示名字,Label框随名字长短而自适应,后面的性别图片跟在其后显示 分两部分:第一部分先布局 //名字 self.nameLab = [[UILabel alloc]i ...