今日算法:随机化快排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. 用setTimeout模拟setInterval的功能

    偶然看到这个题目,稍微写了下,做个笔记,不足之处请指正 //用setTimeout模仿setInterval var MyInterVal = function(fun,tm){ if(this == ...

  2. 50个常用的Linux命令(二)sed

    [root@localhost cee]# echo this thisthisthis |sed 's/this/THIS/g'THIS THISTHISTHIS[root@localhost ce ...

  3. UDP协议学习(转)

    reference: https://blog.csdn.net/s_lisheng/article/details/73538229                  https://blog.cs ...

  4. Java 1.7 NQuery

    package org.rx.common; import java.lang.reflect.Array; import java.util.*; /** * Created by wangxiao ...

  5. DAY 04运算符与流程控制

    输入输出补充: python2与python3的输入输出不同 python2中有两种用户 输入方式,一种是raw_input,和input raw_input与python3的input是相同的 而p ...

  6. [转] C++ 和 python之间的互相调用

    转载自:https://www.cnblogs.com/apexchu/p/5015961.html 一.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不 ...

  7. single-cell RNA-seq 工具大全

    [怪毛匠子-整理] awesome-single-cell List of software packages (and the people developing these methods) fo ...

  8. Pyhon全栈之路----数据类型

    1.定义时不需要像C语言一样加 ' int ' , 'char ' 等,直接写即可,解释器会直接自动识别数据类型.例:  age = 22    (默认为数字类型)      name = 'Alex ...

  9. 内存溢出eclipse启动tomcat

    1.在eclipse中的Window->preferences->Java->install jar->选择JDK,然后在点击Edit,在Default VM argument ...

  10. 2019西湖论剑网络安全技能大赛(大学生组)部分WriteUp

    这次比赛是我参加以来成绩最好的一次,这离不开我们的小团队中任何一个人的努力,熬了一整天才答完题,差点饿死在工作室(门卫大爷出去散步,把大门锁了出不去,还好学弟提了几个盒饭用网线从窗户钓上来才吃到了午饭 ...