今日算法:随机化快排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. 使用md5加密算法完成简单的登录和注册功能

    原理: 登录:后端controller层获取到客户的密码,通过下面代码:new Sha256Hash(pwd).toHex();将密码转换成md5散列,生成一个新的字符串与数据库的值进行比对,根据不同 ...

  2. Caffe中Interp层的使用

    最近实验当中借鉴了FPN网络,由于FPN网络对图片shape有要求,采用了两种方式,其一是在data_layer.cpp中,对原图进行padding操作:其二是需要对特征图进行类似crop操作,使得两 ...

  3. FCC JS基础算法题(1):Factorialize a Number(计算一个整数的阶乘)

    题目描述: 如果用字母n来代表一个整数,阶乘代表着所有小于或等于n的整数的乘积.阶乘通常简写成 n!例如: 5! = 1 * 2 * 3 * 4 * 5 = 120. 算法: function fac ...

  4. 过滤函数 filter

    filter(lambda x:x.endswith('居'),house_type_list) 过滤函数,作用就是将“以‘居’结尾的字段都过滤出来,其它的字段都删除掉.”

  5. 剑指Offer 36. 两个链表的第一个公共结点 (链表)

    题目描述 输入两个链表,找出它们的第一个公共结点. 题目地址 https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tp ...

  6. 关于surface gradient

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2017/06/16 函数定义及前后文详见<Inverse Acoustic and Electromagneti ...

  7. css布局与文档流的关系之float(浮动)

    所谓文档流,指元素在排版布局的过程中,元素会自动从左到右,从上到下的流式排列.脱离文档流呢,就是元素打乱了这个排列,或是从排版中拿走. 说到文档流呢,我们先来说一下元素,每个元素呢,都有display ...

  8. poj2228 Naptime【(环结构)线性DP】

    Naptime Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:3374   Accepted: 1281 Descriptio ...

  9. 在Cygwin中出现JAVA_HOME出现故障找不到出现故障

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012516914/article/details/37689937 JAVA_HOME出现故障后查 ...

  10. Dart Map<> 添加 元素

    Map<String, WidgetBuilder> routesList() { Map<String, WidgetBuilder> re = new Map<Str ...