《算法导论》——随机化快排RandomizedQuickSort
今日算法:随机化快排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的更多相关文章
- 《算法导论》——重复元素的随机化快排Optimization For RandomizedQuickSort
昨天讨论的随机化快排对有重复元素的数组会陷入无限循环.今天带来对其的优化,使其支持重复元素. 只需修改partition函数即可: int partition(int *numArray,int he ...
- MIT算法导论——第四讲.Quicksort
本栏目(Algorithms)下MIT算法导论专题是个人对网易公开课MIT算法导论的学习心得与笔记.所有内容均来自MIT公开课Introduction to Algorithms中Charles E. ...
- 《算法导论》——顺序统计RandomizedSelect
RandomizedSelect.h: #include <stdlib.h> namespace dksl { /* *交换 */ void Swap(int* numArray,int ...
- 排序--QuickSort 快排
Quick の implementation 快排,就像它的名字一定,风一样的快.基本上算是最快的排序算法了.快排的基本思想是选择一个切分的元素.把这个元素排序了.所有这个元素左边的元素都小于这个元素 ...
- 基于visual Studio2013解决算法导论之011快排改良
题目 快排改良 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...
- 基于visual Studio2013解决算法导论之010快排中应用插入排序
题目 快排中引用插入排序 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...
- Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...
- 冒泡,快排算法之javascript初体验
引子:javascript实际使用的排序算法在标准中没有定义,可能是冒泡或快排.不用数组原生的 sort() 方法来实现冒泡和快排. Part 1:冒泡排序(Bubble Sort) 原理:临近的两数 ...
- scala写算法-快排
快排算法很经典,今天用scala的函数式思维来整理一下并实现: def qsort(list: List[Int]):List[Int]=list match { case Nil=>Nil c ...
随机推荐
- python基础--windows环境下 安装python2和python3
一. python 安装 1. 下载安装包 1 2 3 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi # 2 ...
- LeetCode 81 搜索旋转排序数组II
题目: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判断给定的目标值是否存在于 ...
- bootstrap--------bootstrap table
bootstrap table 显示行号 <th rowspan="2" data-field="index" data-formatter=" ...
- Selenium2+python自动化-查看selenium API
前面都是点点滴滴的介绍selenium的一些api使用方法,那么selenium的api到底有多少呢?本篇就叫大家如何去查看selenium api,不求人,无需伸手找人要,在自己电脑就有. pydo ...
- 浅谈URI和URL
URI(Uniform Resource Identifier)字面上的意思是,统一资源标示符 URL(Uniform Resource Locator),统一资源定位符 光从字面上的意思,这个2个东 ...
- SVG的用法
三种添加方式 <iframe src="图的地址" frameborder="0"></iframe> <object width ...
- 2018年3月底的PTA(二)
C高级第二次PTA作业(1) 题目6-7 删除字符串中数字字符 1.设计思路 为了偷懒,本题算法和流程图是精简代码后的,具体请看本题实验代码的第二段代码. (1)算法(子函数) 第一步:定义子函数类型 ...
- poj 3694 无向图求桥+lca
题意抽象为: 给一个无向图和一些询问 对于每一次询问: 每次询问都会在图上增加一条边 对于每一次询问输出此时图上桥的个数. 桥的定义:删除该边后原图变为多个连通块. 数据规模:点数N(1 ≤ N ≤ ...
- 【数据库(一)】SQL语言-表定义、查询
基本模式定义+ SQL支持许多不同的完整性约束. not null, 在该属性上不允许空值 primary key 是否是是主码,主码必须非空且唯一 foreign key check(P),P是谓词 ...
- python 4
一.列表相关操作 l = ['布偶猫', '小断腿', '大白'] # . append l.append('哎呀') print(l) # . insert l.insert(, '小猪佩琪') p ...