2.1 shuffle sort(洗牌)
1.目的:将数组以随机的顺序重新排序,类似洗牌的过程
2.用途用于快速排序或者任何以划分为基础的排序中,目的是减少最坏可能性发生的概率。
3.想法1:给数组的每一个元素产生一个随机的数字作为键,然后使用排序算法,排列数字,即可以完成shuffling
缺点:需要排序的开销
4.想法2:在第i次循环,在0到i之间均匀随机的取整数r,然后交换a[i]和a[r]
可以做到在线性时间里完成shuffling
package com.cx.sort; public class Shuffling {
public static void sort(Comparable[] a) {
int N=a.length;
for(int i=1;i<N;i++) {
//第i次迭代,随机找r
int r=(int)(Math.random()*(i+1));
exch(a, i, r);
}
} private static boolean less(Comparable v,Comparable w) {
return v.compareTo(w)<0;
} private static void exch(Comparable[] a,int i ,int j ) {
Comparable t=a[i];
a[i]=a[j];
a[j]=t;
}
private static void show(Comparable[] a) {
for(int i=0;i<a.length;i++) {
System.out.print(a[i]+" "); }
System.out.println();
}
public static void main(String[] args) {
String a[]= {"s","o","r","t","e","x","a","m","p","l","e"};
show(a);
sort(a);
show(a);
}
}
2.1 shuffle sort(洗牌)的更多相关文章
- Shuffle(洗牌)
Shuffle(洗牌) 图 map 1.Map Task的输出k v,一开始会进入溢写缓冲区中,对数据做处理,比如分区.排序等操作. 2.有几个Map Task ...
- 文本数据挖掘---课后作业shuffle函数洗牌C++
题目: 代码如下:#include <iostream> #include <random> #include <algorithm> #include <v ...
- 闲话shuffle(洗牌)算法
工作中经常会用到洗牌算法,看到这篇文章不错,原文摘自:http://www.atatech.org/article/detail/11821/928 作者:子仲 场景 洗牌算法的应用场景其实很多 ...
- [CareerCup] 18.2 Shuffle Cards 洗牌
18.2 Write a method to shuffle a deck of cards. It must be a perfect shuffle—in other words, each of ...
- uva 10710 - Chinese Shuffle(完美洗牌)
option=com_onlinejudge&Itemid=8&category=474&page=show_problem&problem=1651"> ...
- [LeetCode] Advantage Shuffle 优势洗牌
Given two arrays A and B of equal size, the advantage of A with respect to B is the number of indice ...
- js 随机数 洗牌算法
function shuffle(arr){ var len = arr.length; for(var i = 0;i<len -1;i++) { var idx = Math.floor(M ...
- java集合--模拟斗地主发牌洗牌
import java.util.*; /** * @Date: 2020/6/17 19:53 */public class Test04 { public static void main(Str ...
- [LeetCode] Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
随机推荐
- set && muliset
#include <set> #include <iostream> #include <cstdio> #include <cctype> using ...
- centos 扩展lrzsz通过xshell 下载安装文件
yum自动安装: yum install lrzsz 手动安装方法如下: 定制安装的linux可能没有把rzsz包安装到系统,这对用securecrt这样的windows工具传输文件特别不方便.为了使 ...
- 2017杭电多校第六场03Inversion
传送门 Inversion Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- BFS(两点搜索) FZOJ 2150 Fire Game
题目传送门 题意:'#'表示草地,两个人在草地上点火,相邻的草地会烧起来,每烧一格等1秒,问最少要等几秒草地才烧完 分析:这题和UVA 11624 Fire!有点像,那题给定了两个点,这题两点不确定, ...
- 387 First Unique Character in a String 字符串中的第一个唯一字符
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1.案例:s = "leetcode"返回 0.s = "loveleetcode&qu ...
- Spring.Net学习笔记(6)-方法注入
一.开发环境 系统:win10 编译器:VS2013 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Logging.dll 三.开发过程 1.项目结构 2.编写Mobile ...
- TCPClient、TCPListener的用法
支持Http.Tcp和Udp的类组成了TCP/IP三层模型(请求响应层.应用协议层.传输层)的中间层-应用协议层,该层的类比位于最底层的Socket类提供了更高层次的抽象,它们封装 TCP 和 UDP ...
- linux下jdk与tomcat的安装与配置
Linux中jdk与tomcat的安装与配置 1.搭建环境: (1)Linux环境:CentOS6.1 (2)jdk-1.8 (3)tomcat-9.0 2.在Linux系统上创建目录 在usr/lo ...
- 2199. [HZOI 2016] 活动投票
★★ 输入文件:hztp.in 输出文件:hztp.out 简单对比 时间限制:0.5 s 内存限制:2 MB [题目描述] 衡中活动很多,人也很多,一次活动有n个学生参与投票,现已知 ...
- BackboneJS and UnderscoreJS
介绍 来自API(backbone能做什么?) 当我们开发含有大量Javascript的web应用程序时,首先你需要做的事情之一便是停止向DOM对象附加数据. 通过复杂多变的jQuery选择符和回调 ...