Java random array sorting implementation code, java array sorting
Example 1
The Code is as follows:
Import java. lang. Math;
Import java. util. collections;
Class implements rayreverse
{
Public static void main (String args [])
{
Int a [] = new int [20];
For (int I = 0; I <= 15; I ++)
{
Using sca = new using (System. in );
System. out. println ("Please input array element 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 ();
}
}
Of course, the array here can be defined by yourself.
The more cycles, the better ..
Example 2
The Code is as follows:
Import java. util. Date;
Import java. util. Random;
/*
* Arrays are arranged immediately. Given an array, elements are arranged immediately. Currently, there are two main methods:
*/
Public class RandomSort {
Public static void main (String args []) {
Int data [] = new int };
// Int p [] = getRandom (1,-8,100 );
// Show (p );
Show (data );
PermuteBySort (data );
Show (data );
RandomizeInPlace (data );
Show (data );
}
/*
* Arrays are arranged randomly. Priority is used. Each array element A [I] corresponds to A priority P [I].
* Sort the Array Based on the priority.
*/
Private static void permuteBySort (int [] data)
{
Int l (www.111cn.net) en = data. length;
Int len3 = len * len;
Int P [] = getRandom (1, len3, len );
// Bubble sort
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;
}
}
}
}
/*
* Element A [I] is randomly selected from element A [I] To element 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;
}
}
/*
* Obtain n random numbers between a and B.
*/
Private static int [] getRandom (int a, int B, int n)
{
If (a> B)
{
Int temp =;
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) +;
}
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 ("===================== ");
}
}
From: http://www.111cn.net/jsp/Java/40825.htm
In java, how does one implement random array sorting?
Import java. lang. Math;
Import java. util. collections;
Class implements rayreverse
{
Public static void main (String args [])
{
Int a [] = new int [20];
For (int I = 0; I <= 15; I ++)
{
Using sca = new using (System. in );
System. out. println ("Please input array element 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 ();
}
}
Of course, the array here can be defined by yourself.
The more cycles, the better ..
Java implements array sorting and generates 20 integers at random in ascending order, and outputs the array before and after sorting in every 5 counts.
The question you asked is someone to help you !!!
I suggest you learn more about "Bubble Sorting "!!
This directly implements your function !!
Don't be so lazy !!