-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUtils.java
More file actions
45 lines (37 loc) · 1.03 KB
/
ArrayUtils.java
File metadata and controls
45 lines (37 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.pslin.algorithms.sort;
import java.util.Random;
/**
* @author plin
*/
public class ArrayUtils {
public static int[] createArray(int length) {
int[] numbers = new int[length];
for(int i=0; i < numbers.length; i++) {
numbers[i] = i;
}
return numbers;
}
public static void shuffle(int[] numbers) {
Random rnd = new Random();
for (int i = numbers.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
int a = numbers[index];
numbers[index] = numbers[i];
numbers[i] = a;
}
}
public static boolean isSorted(int[] numbers) {
for(int i=0; i < numbers.length-1; i++) {
if(numbers[i] > numbers[i+1]) {
return false;
}
}
return true;
}
public static void swap(int[] numbers, int i, int j) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}