-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrangement.java
More file actions
279 lines (245 loc) · 9.4 KB
/
Arrangement.java
File metadata and controls
279 lines (245 loc) · 9.4 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package mx.ipn.esfm.progII;
import java.util.Scanner;
/**
*
* @author Jesus
*/
public class Main {
public static void main(String[] args) {
int lista[]=new int[20],j;
int n = lista.length;
Heapsort ob = new Heapsort();
for (int i = 0; i < 20; i++) {
lista[i] =(int) (Math.random()*99);
}
System.out.format("Este programa utiliza los metodos de ordenamiento sobre un arreglo de enteros");
System.out.format("\nElija una de las siguientes opciones:");
System.out.format("\n\t1)Intercambio directo\n\t2)Seleccion directa\n\t3)Inserción directa\n\t4)Shellsort\n\t5)Heapsort\n\t6)Quicksort\n\t7)Mergesort\n\t8)Salir\n");
System.out.format("Su opcion: ");
Scanner xleer= new Scanner(System.in);
j=xleer.nextInt();
switch(j){
case 1: System.out.println(" -SIN ORDENAR-");
mostrarN(lista,200,200);
System.out.println(" -ORDENANDO- ");
ordenaBurbuja(lista);
break;
case 2: System.out.println(" -SIN ORDENAR-");
mostrarN(lista,200,200);
System.out.println(" -ORDENANDO- ");
seleccion(lista);
break;
case 3: System.out.println(" -SIN ORDENAR-");
mostrarN(lista,200,200);
System.out.println(" -ORDENANDO- ");
insercionDirecta(lista);
break;
case 4: System.out.println(" -SIN ORDENAR-");
mostrarN(lista,200,200);
System.out.println(" -ORDENANDO- ");
shell(lista);
break;
case 5: System.out.println(" -SIN ORDENAR-");
mostrarN(lista,200,200);
System.out.println(" -ORDENANDO- ");
ob.sort(lista);
break;
case 6: System.out.println(" -SIN ORDENAR-");
mostrarN(lista,200,200);
System.out.println(" -ORDENANDO- ");
quicksort(lista,0,19);
mostrarN(lista,201,201);
break;
case 7: System.out.println(" -SIN ORDENAR-");
mostrarN(lista,200,200);
System.out.println(" -ORDENANDO- ");
mergeSort(lista,n);
mostrarN(lista,201,201);
break;
}
System.out.println("FIN DEL PROGRAMA");
}
static void ordenaBurbuja(int ArrayN[]) {
for (int i = 0; i < ArrayN.length - 1; i++) {
for (int j = 0; j < ArrayN.length - 1; j++) {
mostrarBur(ArrayN,ArrayN[j]);
System.out.println();
if (ArrayN[j] > ArrayN[j + 1]) {
int temp = ArrayN[j + 1];
ArrayN[j + 1] = ArrayN[j];
ArrayN[j] = temp;
}
}
}
mostrarBur(ArrayN,200);
}
static void mostrarN(int ArrayN[], int r, int s) {
for (int i = 0; i < 50; i++) System.out.println();
System.out.println("|--------------------------------------|");
for (int i = 0; i < 4; i++){
for(int j = 5*i; j < 5*(i+1); j++){
if(r==ArrayN[j] || s==ArrayN[j]) System.out.format("|||%2d|||", ArrayN[j]);
else System.out.format("| %2d |", ArrayN[j]);
}
System.out.println();
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
}
}
static void mostrarBur(int ArrayN[], int r) {
for (int i = 0; i < 200; i++) System.out.println();
System.out.println("|--------------------------------------|");
for (int i = 0; i < 4; i++){
for(int j = 5*i; j < 5*(i+1); j++){
if(r==ArrayN[j]) System.out.format("|||%2d|||", ArrayN[j]);
else System.out.format("| %2d |", ArrayN[j]);
}
System.out.println();
}
try{
Thread.sleep(100);
}catch(InterruptedException e){
}
}
static void mostrarT(int ArrayN[], int r) {
System.out.println("|------|");
for(int j = 0; j < ArrayN.length; j++){
if(r==ArrayN[j]) System.out.format("|||%2d|||", ArrayN[j]);
else System.out.format("| %2d |", ArrayN[j]);
System.out.println();
}
try{
Thread.sleep(1500);
}catch(InterruptedException e){
}
for (int i = 0; i < 50; i++) System.out.println();
}
// Seleccion directa
public static void seleccion(int A[]) {
int i, j, menor, pos, tmp;
for (i = 0; i < A.length - 1; i++) {
menor = A[i];
pos = i;
for (j = i + 1; j < A.length; j++){
if (A[j] < menor) {
menor = A[j];
pos = j;
}
}
if (pos != i){
mostrarN(A,A[i],A[pos]);
tmp = A[i];
A[i] = A[pos];
A[pos] = tmp;
mostrarN(A,A[i],A[pos]);
System.out.println();
}
}
mostrarN(A,200,200);
}
public static void insercionDirecta(int A[]){
int p, j;
int aux;
for (p = 1; p < A.length; p++){
aux = A[p];
j = p - 1;
while ((j >= 0) && (aux < A[j])){
mostrarN(A,A[j+1],A[j]);
A[j + 1] = A[j];
j--;
if(j>=0)mostrarN(A,A[j+1],A[j]);
}
A[j + 1] = aux;
mostrarN(A,aux,A[j+1]);
}
mostrarN(A,200,200);
}
public static void shell(int A[]) {
int salto, aux, i;
boolean cambios;
for (salto = A.length / 2; salto != 0; salto /= 2) {
cambios = true;
while (cambios) {
cambios = false;
for (i = salto; i < A.length; i++)
{
if (A[i - salto] > A[i]) {
mostrarN(A,A[i],A[i - salto]);
aux = A[i];
A[i] = A[i - salto];
A[i - salto] = aux;
mostrarN(A,A[i],A[i - salto]);
cambios = true;
}
}
}
}
mostrarN(A,200,200);
}
public static void quicksort(int A[], int izq, int der) {
int pivote=A[izq];
int i=izq; // i realiza la búsqueda de izquierda a derecha
int j=der; // j realiza la búsqueda de derecha a izquierda
int aux;
while(i < j){ // mientras no se crucen las búsquedas
while(A[i] <= pivote && i < j) i++; // busca elemento mayor que pivote
while(A[j] > pivote) j--; // busca elemento menor que pivote
if (i < j) {
aux= A[i]; // los intercambia
mostrarN(A,A[j],A[i]);
A[i]=A[j];
A[j]=aux;
mostrarN(A,A[j],A[i]);
}
}
A[izq]=A[j]; // se coloca el pivote en su lugar de forma que tendremos
A[j]=pivote; // los menores a su izquierda y los mayores a su derecha
if(izq < j-1)
quicksort(A,izq,j-1); // ordenamos subarray izquierdo
if(j+1 < der)
quicksort(A,j+1,der); // ordenamos subarray derecho
}
public static void mergeSort(int[] a, int n) {
if (n < 2) {
return;
}
int mid = n / 2;
int[] l = new int[mid];
int[] r = new int[n - mid];
for (int i = 0; i < mid; i++) {
l[i] = a[i];
}
for (int i = mid; i < n; i++) {
r[i - mid] = a[i];
}
mergeSort(l, mid);
mergeSort(r, n - mid);
merge(a, l, r, mid, n - mid);
mostrarT(a,201);
}
public static void merge(
int[] a, int[] l, int[] r, int left, int right) {
int i = 0, j = 0, k = 0;
while (i < left && j < right) {
if (l[i] <= r[j]) {
a[k++] = l[i++];
}
else {
a[k++] = r[j++];
}
}
while (i < left) {
a[k++] = l[i++];
}
while (j < right) {
a[k++] = r[j++];
}
}
}