-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortTester.java
56 lines (48 loc) · 1.48 KB
/
SortTester.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.Scanner;
public class SortTester {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a space separated list of numbers to sort or type QUIT to exit:");
String numbers = keyboard.nextLine();
while (!numbers.equalsIgnoreCase("quit")) {
int[] array = makeArray(numbers);
array = makeArray(numbers);
System.out.println("Sorted array using Selection Sort algorithm:");
Sort.selectionSort(array);
println(array);
System.out.println("Please enter a space separated list of numbers to sort or type QUIT to exit:");
numbers = keyboard.nextLine();
}
keyboard.close();
}
public static int[] makeArray(String str) {
if (!str.trim().isEmpty()) {
String[] strings = str.trim().split("[ ]+");
int[] result = new int[strings.length];
for (int i = 0; i < result.length; i++)
result[i] = Integer.parseInt(strings[i]);
return result;
}
else {
return new int[0];
}
}
public static boolean equals(int[] array1, int[] array2) {
boolean equal = true;
if (array1.length != array2.length)
equal = false;
int i = 0;
while (equal && i < array1.length) {
equal = (array1[i] == array2[i]);
}
return equal;
}
public static void println(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i == array.length - 1)
System.out.println(array[i]);
else
System.out.print(array[i] + " ");
}
}
}