-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaker.java
41 lines (40 loc) · 1.38 KB
/
shaker.java
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
import java.util.Scanner;
import java.util.Random;
class shaker {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Random random = new Random();
System.out.println("Enter how long you want your list to be");
int length = input.nextInt();
System.out.println("Enter the range (from 0 to this value)");
int range = input.nextInt();
int[] array = new int[length];
for (int i = 0; i < length; i++) {
array[i] = random.nextInt(range);
}
int begin = 0;
int end = length - 1;
while (begin < end) {
for (int i = begin; i < end; i++) {
if (array[i] > array[i + 1]) {
int value = array[i];
array[i] = array[i + 1];
array[i + 1] = value;
}
}
end--;
for (int i = end; i > begin; i--) {
if (array[i] < array[i - 1]) {
int value = array[i];
array[i] = array[i - 1];
array[i - 1] = value;
}
}
begin++;
}
System.out.println("The list of sorted numbers from least to greatest");
for (int item : array) {
System.out.print(item + " ");
}
}
}