算法导论Java实现-随机化数组的两种方式(5.3章节)
- package lhz.algorithm.chapter.five;
- /**
- * 随机数组两种实现,《算法导论》第五章第三节
- * 本文地址:http://mushiqianmeng.blog.51cto.com/3970029/734384
- * @author lihzh(苦逼coder)
- */
- public class RandomArray {
- private static int[] arrayOne = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
- 11, 12, 13 };
- private static int[] arrayTwo = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
- 11, 12, 13 };
- public static void main(String[] args) {
- //随机方式一,排列数组一
- permuteBySorting(arrayOne);
- //打印数组一
- printArray(arrayOne);
- System.out.println("");
- //随机方式二,排列数组二
- randomizeInPlace(arrayTwo);
- //打印数组二
- printArray(arrayTwo);
- }
- /**
- * 随机化数组方式一,采用优先级数组比照方式
- * PERMUTE-BY-SORTING(A)
- * 1 n ← length[A]
- * 2 for i ← 1 to n
- * 3 do P[i] =RANDOM(1, n^3)
- * 4 sort A, using P as sort keys
- * 5 return A
- */
- private static void permuteBySorting(int[] input) {
- int n = input.length;
- int[] p = new int[n];
- for (int i = 0; i < n; i++) {
- p[i] = randomValue(n);
- }
- mergeSort(input, p);
- /*
- *复杂度分析:
- *因为采用了合并排序,所以可知复杂度为:Θ(n lg n)
- */
- }
- /**
- * 随机化数组方式二,直接位置交换方式
- * 复杂度为:O(n)
- * RANDOMIZE-IN-PLACE(A)
- * 1 n ← length[A]
- * 2 for i ← to n
- * 3 do swap A[i] ↔ A[RANDOM(i, n)]
- * @param input
- */
- private static void randomizeInPlace(int[] input) {
- int n = input.length;
- for (int i = 0; i < n; i++) {
- int index = (int) (Math.random() * (n - i - 1) + i);
- int temp = input[index];
- input[index] = input[i];
- input[i] = temp;
- }
- }
- /**
- * 产生从1到n^3的随机数
- * @param n
- * @return
- */
- private static int randomValue(int n) {
- return (int) (Math.random() * (n * n * n - 1) + 1);
- }
- /**
- * 改写的合并排序,根据数组P中定义的优先级决定顺序。
- * @param array
- * @return
- */
- private static int[] mergeSort(int[] array, int[] keyArray) {
- // 如果数组的长度大于1,继续分解数组
- if (array.length > 1) {
- int leftLength = array.length / 2;
- int rightLength = array.length - leftLength;
- // 创建两个新的数组
- int[] left = new int[leftLength];
- int[] right = new int[rightLength];
- // 创建两个新的key数组
- int[] leftKey = new int[leftLength];
- int[] rightKey = new int[rightLength];
- // 将array中的值分别对应复制到两个子数组中
- for (int i = 0; i < leftLength; i++) {
- left[i] = array[i];
- leftKey[i] = keyArray[i];
- }
- for (int i = 0; i < rightLength; i++) {
- right[i] = array[leftLength + i];
- rightKey[i] = keyArray[leftLength + i];
- }
- // 递归利用合并排序,排序子数组
- left = mergeSort(left,leftKey);
- right = mergeSort(right,rightKey);
- // 设置初始索引
- int i = 0;
- int j = 0;
- for (int k = 0; k < array.length; k++) {
- // 如果左边数据索引到达边界则取右边的值
- if (i == leftLength && j < rightLength) {
- array[k] = right[j];
- j++;
- // 如果右边数组索引到达边界,取左数组的值
- } else if (i < leftLength && j == rightLength) {
- array[k] = left[i];
- i++;
- // 如果均未到达边界,则根据优先级数组中定义的顺序排序
- } else if (i < leftLength && j < rightLength) {
- if (leftKey[i] > rightKey[j]) {
- array[k] = right[j];
- j++;
- } else if (leftKey[i] < rightKey[j]){
- array[k] = left[i];
- i++;
- }
- }
- }
- }
- return array;
- }
- private static void printArray(int[] array) {
- for (int i : array) {
- System.out.print(i + " ");
- }
- }
- }
转载于:https://blog.51cto.com/mushiqianmeng/734384
算法导论Java实现-随机化数组的两种方式(5.3章节)相关推荐
- Java中HashMap遍历的两种方式
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- java读取csv文件的两种方式
java读取csv文件的两种方式 1.CsvReader读取 import com.csvreader.CsvReader; /*** CsvReader 读取* @param filePath* @ ...
- Java判断回文数(两种方式)
Java判断回文数(两种方式) 回文数是一个非常特殊的数,它从左边读和从右边读是一样的.例如12321 判断方式有两种. 方式一:对该数字进行分解,然后一一比对,拆解成 1 2 3 2 1,然后第一位 ...
- Java中Http连接的两种方式
在java中连接http,介绍两种方法,一种是java的HttpUrlConnection,另一种是apacha公司的httpClient,后者是第三方的类库需要从外部,导入,同时这也是第一次使用外部 ...
- 代码示例:Java中定义数组的三种方式
在Java中,数组的定义有三种方式,其基本语法格式如下: 数组类型[] 数组名 = new 数组类型[数组长度]; 数组类型[] 数组名 = new 数组类型[]{数组元素0,数组元素1,...}; ...
- Java中实现多线程的两种方式之间的区别
Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...
- java中实现同步的两种方式:syschronized和lock的区别和联系
转载自 http://www.cnblogs.com/xiohao/p/4151408.html Lock是java.util.concurrent.locks包下的接口,Lock 实现提供了比使用s ...
- java jobdetail_Spring创建JobDetail的两种方式
一.Spring创建JobDetail的两种方式 定时任务两种方式,Spring很好的封装使用Quartz的细节,第一种方式是利用SPring封装的Quartz类进行特定方法的实现,第二种是通过透明的 ...
- java中定义数组的3种方式
直接上代码和截图 public static void main(String[] args) {// 定义数组的3种方式// 第一种String[] role = new String[3];rol ...
最新文章
- mvc学习-编辑提交需要注意-mvc重点
- 深度学习(八)RBM受限波尔兹曼机学习-未完待续
- Linux漏洞CVE整理
- [Xcode 实际操作]六、媒体与动画-(6)使用UIBlurEffect给图片添加模糊效果
- 分布式日志收集系统Apache Flume的设计详细介绍
- python dataframe索引_pandas DataFrame 行列索引及值的获取的方法
- XMPP的简介和基本概念
- 华为方舟编译器是黑科技?
- Java程序员校招蚂蚁金服,大专生出身,做Java程序员真的没有春天吗
- shell 获取指定ip的丢包率
- 基于51单片机的交通灯原理图加代码
- 台式计算机分享无线网,台式机如何去共享wifi
- Python-伪数据构造神库Faker
- 服务器怎么组装,如何组装一台服务器?(最好标明下什么类型的)
- 漫谈直播:从零开始认识直播并快速搭建专属直播平台
- python math库 sqrt eval_假设 math 标准库已导入,那么表达式 eval('math.sqrt(4)') 的值为 _________ 。( ) (2.0分)_学小易找答案...
- 【数论】同余(五):多元线性同余方程
- 算法学习过程入门篇(2)-算法初步
- case/casez/casex 的区分与使用
- STM32——快速识别芯片引脚数