-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
130 lines (119 loc) · 4.06 KB
/
Main.java
File metadata and controls
130 lines (119 loc) · 4.06 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] numArr = { 99, 5, 3, 8, 9, 50, -3, 8, -20, 9, 21 };
System.out.println("number Array before sorting");
printArray(numArr);
SelectionSort.sort(numArr);
System.out.println("number Array sorted");
printArray(numArr);
System.out.println("");
System.out.println("Try selecting the order and algorithm for the following arrays");
String[] sortingAlgortithms = { "1) Merge Sort in ascending order", "2) Merge Sort in descending order",
"3) Selection Sort in ascending order", "4)Selection sort in descending order" };
int[] numArray = { 1, 23, 3, 4, 2, -14, 10, 8, -20, 9, 21 };
runAlgorithm(numArray, sortingAlgortithms);
System.out.println("");
String[] stringArray = { "zc", "aba", "ab", "ba", "abc", "a", };
runAlgorithm(stringArray, sortingAlgortithms);
System.out.println("");
char[] charArray = { 'a', 'z', 'M', 'N', 'Z', 'A' };
runAlgorithm(charArray, sortingAlgortithms);
}
static void runAlgorithm(int[] arr, String[] sortingAlgortithms) {
Scanner sc = new Scanner(System.in);
printArray(arr);
System.out.println("How would you like the number array above to be sorted?");
for (String s : sortingAlgortithms) {
System.out.println(s);
}
String userInput = sc.next();
switch (userInput) {
case "1":
MergeSort.sort(arr, false);
break;
case "2":
MergeSort.sort(arr, true);
break;
case "3":
SelectionSort.sort(arr, false);
break;
case "4":
SelectionSort.sort(arr, true);
break;
default:
MergeSort.sort(arr);
}
printArray(arr);
}
static void runAlgorithm(char[] arr, String[] sortingAlgortithms) {
Scanner sc = new Scanner(System.in);
printArray(arr);
System.out.println("How would you like the number array above to be sorted?");
for (String s : sortingAlgortithms) {
System.out.println(s);
}
String userInput = sc.next();
switch (userInput) {
case "1":
MergeSort.sort(arr, false);
break;
case "2":
MergeSort.sort(arr, true);
break;
case "3":
SelectionSort.sort(arr, false);
break;
case "4":
SelectionSort.sort(arr, true);
break;
default:
MergeSort.sort(arr);
}
printArray(arr);
}
static void runAlgorithm(String[] arr, String[] sortingAlgortithms) {
Scanner sc = new Scanner(System.in);
printArray(arr);
System.out.println("How would you like the number array above to be sorted?");
for (String s : sortingAlgortithms) {
System.out.println(s);
}
String userInput = sc.next();
switch (userInput) {
case "1":
MergeSort.sort(arr, false);
break;
case "2":
MergeSort.sort(arr, true);
break;
case "3":
SelectionSort.sort(arr, false);
break;
case "4":
SelectionSort.sort(arr, true);
break;
default:
MergeSort.sort(arr);
}
printArray(arr);
}
static void printArray(int[] arr) {
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println("");
}
static void printArray(char[] arr) {
for (char i : arr) {
System.out.print(i + " ");
}
System.out.println("");
}
static void printArray(String[] arr) {
for (String i : arr) {
System.out.print(i + " ");
}
System.out.println("");
}
}