-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderTest.java
More file actions
176 lines (137 loc) · 3.54 KB
/
OrderTest.java
File metadata and controls
176 lines (137 loc) · 3.54 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package trabalhoGrauB1;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
public class OrderTest {
private static Scanner input;
private static LinkedList<Integer> list;
public static void main(String[] args) {
input = new Scanner(System.in);
list = new LinkedList<>();
int opcao;
boolean sair = false;
do {
System.out.print("1: Inserir | 2: Listar | 3: Remover | 4: Ordenar | 9: Sair");
System.out.print("\nInforme uma opção acima: ");
opcao = input.nextInt();
System.out.println("");
switch (opcao) {
case 1:
insertElements();
break;
case 2:
showElements();
break;
case 3:
removeElement();
break;
case 4:
orderElements();
break;
case 9:
sair = true;
break;
default:
System.out.print("Opção inválida!");
break;
}
System.out.println("\n");
} while (!sair);
System.out.print("Programa encerrado.");
}
public static void randomList(LinkedList<Integer> list)
{
Random random = new Random();
System.out.print("Quantos Números deseja gerar: ");
int num = input.nextInt();
for (int i = 0; i < num; i++) {
list.add(random.nextInt(1500));
}
System.out.print(num + " gerados com sucesso.");
}
public static void insertElements() {
int opcao;
do {
System.out.print("1: Randomico | 2: Informar | 3: Voltar");
System.out.print("\nInforme uma opção acima: ");
opcao = input.nextInt();
if (opcao < 1 || opcao > 3) {
System.out.println("\nOpção inválida!\n");
}
} while (opcao < 1 || opcao > 3);
if (opcao == 1) {
randomList(list);
}
if (opcao == 2) {
customList(list);
}
}
public static void customList(LinkedList<Integer> list) {
System.out.print("Quantos Números deseja informar: ");
int num = input.nextInt();
for (int i = 0; i < num; i++) {
System.out.print((i + 1) + ": ");
list.add(input.nextInt());
}
}
public static void showElements() {
if (list.isEmpty()) {
System.out.println("A Lista está vazia.");
} else {
for(int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + ", ");
}
}
}
public static void removeElement()
{
int index;
boolean getOut = false;
if (list.isEmpty()) {
System.out.println("A Lista está vazia.");
return;
}
do {
for(int i = 0; i < list.size(); i++) {
System.out.print(i + ":" + list.get(i) + ", ");
}
System.out.print("\nInforme o índice que deseja remover. (índice:valor): ");
index = input.nextInt();
try {
int removed = list.remove(index);
System.out.print("Item " + removed + " foi removido da lista com sucesso.");
getOut = true;
} catch(IndexOutOfBoundsException exception) {
System.out.println("Índice inválido.");
}
} while(getOut != true);
}
public static void orderElements() {
int option;
Ordenar ordenar = new Ordenar();
do {
System.out.println("1: Quick Sort | 2: Bubble Sort. | 3: Merge Sort | 4: Selection Sort | 5: Insertion Sort | 6: Voltar");
System.out.print("Selecione um algoritmo para ordenação: ");
option = input.nextInt();
switch (option) {
case 1:
ordenar.quickSort(list);
break;
case 2:
ordenar.bubbleSort(list);
break;
case 3:
ordenar.mergeSort(list);
break;
case 4:
ordenar.selectionSort(list);
break;
case 5:
ordenar.insertionSort(list);
break;
default:
System.out.println("Opção inválida!\n");
}
} while(option < 1 || option > 6);
}
}