今日算法:随机化快排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. 显示“快捷键清单” acessksy

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Python编程--类的分析

    一.类的概念 python是面向对象的编程语言,详细来说,我们把一类相同的事物叫做类,其中用相同的属性(其实就是变量描述),里面封装了相同的方法,比如:汽车是一个类,它包括价格.品牌等属性.那么我们如 ...

  3. 实力封装:Unity打包AssetBundle(大结局)

    →→前情提要:让用户选择要打包的文件←← 大结局:更多选择 Unity打包AssetBundle从入门到放弃系列终于要迎来大结局了[小哥哥表示实在写不动了o(╥﹏╥)o]... 经过上一次的教程,其实 ...

  4. python基础08_set集合

    关于前几次课的回顾: #!/usr/bin/env python # coding:utf-8 ## 字符串 数字 列表 元组 字典 ## 可变:列表 字典 ## 不可变:字符串, 数字, 元组 na ...

  5. 使用element-ui 遇到的问题

    Pagination 分页 在使用分页的时候,每次切换pageSize的时候,需要把current-page置为1 重新加载数据. 但是当current-page !== 1的时候,修改current ...

  6. 剑指Offer 23. 二叉搜索树的后序遍历序列 (二叉搜索树)

    题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 题目地址 https://www.nowcoder ...

  7. 3070 Fibonacci

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21048   Accepted: 14416 Descr ...

  8. go延时队列

    package main import ( "errors" "flag" "fmt" log "github.com/cihub ...

  9. 【软件安装】nvidia驱动安装事宜

    https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html https://docs.nvidia.com/cuda/arch ...

  10. mod_conference ESL控制一(原理)

    本文介绍通过freeswitch mod_conference 的配置和APP,以及如何通过这些事件实现会议控制. 需求 ESL内联,发起会议.加人.踢人.静音.恢复静音.申请发言.结束会议等基础功能 ...