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(洗牌)的更多相关文章

  1. Shuffle(洗牌)

    Shuffle(洗牌)    图    map        1.Map Task的输出k v,一开始会进入溢写缓冲区中,对数据做处理,比如分区.排序等操作.        2.有几个Map Task ...

  2. 文本数据挖掘---课后作业shuffle函数洗牌C++

    题目: 代码如下:#include <iostream> #include <random> #include <algorithm> #include <v ...

  3. 闲话shuffle(洗牌)算法

    工作中经常会用到洗牌算法,看到这篇文章不错,原文摘自:http://www.atatech.org/article/detail/11821/928  作者:子仲   场景 洗牌算法的应用场景其实很多 ...

  4. [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 ...

  5. uva 10710 - Chinese Shuffle(完美洗牌)

    option=com_onlinejudge&Itemid=8&category=474&page=show_problem&problem=1651"> ...

  6. [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 ...

  7. js 随机数 洗牌算法

    function shuffle(arr){ var len = arr.length; for(var i = 0;i<len -1;i++) { var idx = Math.floor(M ...

  8. java集合--模拟斗地主发牌洗牌

    import java.util.*; /** * @Date: 2020/6/17 19:53 */public class Test04 { public static void main(Str ...

  9. [LeetCode] Shuffle an Array 数组洗牌

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

随机推荐

  1. Django day 37 网站视频的播放,购物车接口,优惠券表分析

    一:网站视频的播放, 二:购物车接口, 三:优惠券表分析

  2. Winform执行CMD命令

    1.首先分享CmdHelper类: using System; using System.Collections.Generic; using System.Text; using System.Di ...

  3. $割点割顶tarjan$

    原题 #include <bits/stdc++.h> using namespace std; typedef long long LL; inline LL read () { LL ...

  4. liunx 用户切换 su sudo

    liunx 用户操作#useradd test#passwd test 用户身份切换su 切换用户 需要知道切换用户的密码1.su [-lm] [-c命令] [username] #su -login ...

  5. Git学习笔记(0)-错误汇总

    一.LF will be replaced by CRLF 1.发现问题 $ git add welcome.txt warning: LF will be replaced by CRLF in w ...

  6. plc学习笔记

    防抖编程代码备份 如果定时到了还未检测到下降沿,则认为信号有效.这一端程序主要是针对现场装料杯在输送过程中由于传送带原因,电眼时常产生无效停机信号,需要过滤,因此需要在电眼检测中添加防抖功能 2018 ...

  7. 什么 是JUnit?

    JUnit详解: http://www.cnblogs.com/eggbucket/archive/2012/02/02/2335697.html

  8. Selenium学习第二天,了解Selenium工作模式与学习Selenium需要具备的知识与工具。

    Selenium学习网站: 1.http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2014/0408/207237.html——好像是对API的 ...

  9. QQ应用前景与范围文档

    QQ软件 前景与范围文档       当前版本: 版本1 作   者: 李飞 完成日期: 2013年11月3日 1.  业务需求 1.1 应用背景 20世纪后期网络的应用和21世纪的飞速发展,网络已经 ...

  10. Server 2008 R2 事件查看器实现日志分析

    在 windows server 2008 R2 中,可以通过点击 "开始" -> "管理工具" -> "事件查看器" ,来打开 ...