-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrayBi.java
151 lines (127 loc) · 4.8 KB
/
arrayBi.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
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
import java.util.Scanner;
import java.util.Arrays;
class arrayBi {
public static void main(String[] args) {
int op;
double[][] array = fillArray(createArray());
do {
menu();
op = introducirOp();
switch (op) {
case 1: array = fillArray(createArray()); break;
case 2: System.out.println("Array:\n"+Arrays.deepToString(array)); break;
case 3: insertionSort(array); break;
case 4: shellSort(array); break;
case 5: bubbleSort(array); break;
case 6: binarySearch(array); break;
default: System.out.println("Opcion no valida"); break;
}
} while (op!=7);
}
public static void menu() {
System.out.println("\n\t\033[4mMenu\033[0m\n");
System.out.println(" 1. Crear Array");
System.out.println(" 2. Mostrar Array");
System.out.println(" Ordenar:");
System.out.println(" 3. Insertion Sort");
System.out.println(" 4. Shell Sort");
System.out.println(" 5. Bubble Sort");
System.out.println(" 6. Busqueda binaria");
System.out.println(" 7. Salir\n");
}
public static int introducirOp() {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Introduce una opcion: ");
return scanner.nextInt();
} catch (java.util.InputMismatchException e) { return introducirOp(); }
}
public static double[][] createArray() {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Introduce el numero alumnos: ");
int alumnos = scanner.nextInt();
System.out.print("Introduce el numero de notas por alumno: ");
int notas = scanner.nextInt();
double[][] array = new double[alumnos][notas+1];
return array;
} catch (java.util.InputMismatchException e) { return createArray(); }
}
public static double[][] fillArray(double[][] array) {
double total = 0;
for (int a = 0 ; a < array.length ; a++) {
for (int n = 0 ; n < array[a].length-1 ; n++) {
array[a][n] = (int) (Math.random() * 1001) / 100.0;
total += array[a][n];
}
//calcular la media y asignarlo a la ultima posicion del array anidado
array[a][array[a].length-1] = Math.round(total/(array[a].length-1)*100)/100.0;
total = 0;
}
return array;
}
public static void sort(double[][] array) {
Arrays.sort(array, (a, b) -> Double.compare(a[a.length-1], b[b.length-1]));
}
public static void insertionSort(double[][] array) {
int y = array[0].length-1; // ultima posicion de cada array (media)
double[] temp;
for (int x = 0 ; x<array.length-1 ; x++ ) { // mira por cada array[]
while (array[x][y] > array[x+1][y]) { // mira la ultima posicion de cada array
temp = array[x];
array[x] = array[x+1];
array[x+1] = temp;
x = 0;
}
}
}
public static void shellSort(double[][] array) {
int y = array[0].length-1;
double[] temp;
int gap = array.length/2;
while (gap > 1) {
for (int x = 0 ; x<array.length-gap ; x++ ) {
if (array[x][y] > array[x+gap][y]) {
temp = array[x];
array[x] = array[x+gap];
array[x+gap] = temp;
}
}
gap = gap/2;
}
if (gap == 1) insertionSort(array);
}
public static void bubbleSort(double[][] array) {
int y = array[0].length-1;
double[] temp;
int n = array.length;
while (n>1) {
for (int x = 0 ; x<n-1 ; x++) {
if (array[x][y] > array[x+1][y]) {
temp = array[x];
array[x] = array[x+1];
array[x+1] = temp;
}
}
n--;
}
}
public static void binarySearch(double[][] array) {
Scanner scanner = new Scanner(System.in);
int y = array[0].length - 1;
int l = 0;
int r = array.length-1;
System.out.print("\nIntroduce la nota media a buscar: ");
double n = scanner.nextDouble();
while (!(l>r)) {
int m = (l+r) / 2;
if (array[m][y] > n) r = m - 1;
else if (array[m][y] < n) l = m + 1;
else if (array[m][y] == n) {
System.out.println("Array["+m+"] con nota media "+n+": "+Arrays.toString(array[m]));
break;
}
if (l>r) System.out.println("Busqueda fallida D:");
}
}
}