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. Java多线程(五)停止线程 在沉睡中停止线程

    在沉睡中停止线程会抛出异常 public class SleepInterruptDemo extends Thread { public void run() { super.run(); try ...

  2. 关于ListView的注意点

    解决ListView的一些常见问题: 1.listview在拖动的时候背景图片消失变成黑色背景,等到拖动完毕我们自己的背景图片才显示出来 解决:在XML中加入 android:scrollingCac ...

  3. 2017杭电多校第六场1011Classes

    传送门 Classes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  4. 二分搜索 HDOJ 2289 Cup

    题目传送门 /* 二分搜索:枚举高度,计算体积与给出的比较. */ #include <cstdio> #include <algorithm> #include <cs ...

  5. Android内存管理(15)SparseArray系列代替HashMap系列

    参考: https://liuzhichao.com/p/832.html http://www.2cto.com/kf/201311/255640.html 1,简介: SparseArray是an ...

  6. LN : leetcode 238 Product of Array Except Self

    lc 238 Product of Array Except Self 238 Product of Array Except Self Given an array of n integers wh ...

  7. Laravel5.1学习笔记21 EloquentORM 集合

    Eloquent: Collections Introduction Available Methods Custom Collections Introduction All multi-resul ...

  8. Android 将Bitmap对象保存为png图片文件

    输入:Bitmap对象.保存的路径.保存的文件名 注意路径的最后要带上  '/' 符号 private void saveBitmap(Bitmap bitmap,String path, Strin ...

  9. 什么是vuejs之重新认识vuejs

    什么是vuejs? 1.它是一个轻量级的mvvm框架 2.数据驱动+组件化的前端开发 3.github超过25k+的stat数,社区完善 4.vuejs更轻量,gzip后大小只有20k+ 5.vuej ...

  10. magento category Ids Name

    如何获取产品的分类的名称 和ids 1.对于产品的分类ids 的获取      $this->getProduct()->getCategoryIds() 2.对应产品的分类的Name 的 ...