Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  java  >  正文 java数组随机排序实现代码

java数组随机排序实现代码

发布时间:2017-12-07   编辑:www.jquerycn.cn
jquery中文网为您提供java数组随机排序实现代码等资源,欢迎您收藏本站,我们将为您提供最新的java数组随机排序实现代码资源
目前常用的有两种方法,第一种就是元素A[i]对应一个优先级P[i],根据优先级作为键值来从新排序数组;第二种方法就是A[i]随机的跟A[i]到A[n]中的任意个元素进行交换,n为数组的长度,下面是用java实现一个简单实现.

例一

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3349')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3349>import java.lang.Math;
import java.util.Scanner;
class AarrayReverse
{
 public static void main(String args[])
 {
  int a[]=new int[20];
  for(int i=0;i<=15;i )
  {
   Scanner sca=new Scanner(System.in);
   System.out.println("请输数组元素a[" "]");
   a[i]=sca.nextInt();
  }
  for(int i=0;i<=100;i )
  {
   int middle=(int)(a.length/2*Math.random());
   int radius=(int)(middle*Math.random());
   int t;
   t=a[middle-radius 1];
   a[middle-radius 1]=a[middle radius 3];
   a[middle radius 3]=t;
  }
  for(int i=0;i<a.length;i )
  System.out.print(a[i] " ");
  System.out.println();
 }
}

当然这里的数组可以自己定义.
循环的次数越多越好..

例二

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6131')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6131>

import java.util.Date;
import java.util.Random;
/*
* 随即排列数组,给定一个数组,随即排列其中的元素,目前主要有两种方法
*/
public class RandomSort {

public static void main(String args[]){
int data[]=new int[]{1,42,51,62,8,94,23,13,40,5};
//int p[]=getRandom(1,-8,100);
//show(p);
show(data);
permuteBySort(data);
show(data);
randomizeInPlace(data);
show(data);
}

/*
* 随机排列数组,使用优先级方式,每个数组元素A[i] 对应一个优先级P[i],
* 然后依据优先级对数组进行排序
*/
private static void permuteBySort(int[] data)
{
int len=data.length;
int len3=len*len*len;
int P[]=getRandom(1,len3,len);

//冒泡排序
for(int i=len-1; i>0; i--)
{
for(int j=0; j<i ; j )
{
if(P[j]>P[j 1])
{
int temp=data[j];
data[j]=data[j 1];
data[j 1]=temp;

temp=P[j];
P[j]=P[j 1];
P[j 1]=temp;
}
}
}
}
/*
* 元素A[i]是从 元素A[i]到A[n]中随机选取的
*/
private static void randomizeInPlace(int[] data)
{
Date dt=new Date();
Random random=new Random(dt.getSeconds());
int len=data.length;
for(int i=0; i<len; i )
{
int pos=(int)(random.nextDouble()*(len-i 1) i)-1;
int temp=data[i];
data[i]=data[pos];
data[pos]=temp;
}
}

/*
* 获得在a到b之间的n个随机数
*/
private static int[] getRandom(int a,int b,int n)
{
if(a>b)
{
int temp=a;
a=b;
b=temp;
}

Date dt=new Date();
Random random=new Random(dt.getSeconds());
int res[]=new int[n];
for(int i=0; i<n; i )
{
res[i]=(int)(random.nextDouble()*(Math.abs(b-a) 1)) a;
}
return res;
}


private static void show(int[] data)
{
System.out.println("========================");
for(int i = 0; i < data.length; i )
{
System.out.print(data[i] " ");
}
System.out.println();
System.out.println("========================");
}
}

 

您可能感兴趣的文章:
php数组随机排序示例
JS随机快速排序的代码分享
php数组排序简单方法
Java 实例
java数组随机排序实现代码
javascript随机排序的例子
java数组降序和升序排序例子
javascript排序算法代码解析
php选择排序算法实现代码
php冒泡排序算法实现代码

[关闭]