Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 95b0783

Browse files
author
javiluli
committed
Agrego 3 nuevos Algoritmos sort: bitonic, iterativeQuick y recursiveBubble
1 parent 0222c1f commit 95b0783

File tree

5 files changed

+175
-18
lines changed

5 files changed

+175
-18
lines changed

‎src/Ordenar/Algoritmos/Bitonic.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package Ordenar.Algoritmos;
2+
3+
import Ordenar.AdicionalesSorts;
4+
import Ordenar.ISort;
5+
import Principal.DibujarGraficos;
6+
import Principal.MainAplicacion;
7+
8+
public class Bitonic extends AdicionalesSorts implements ISort {
9+
public Bitonic(MainAplicacion m, int[] n) {
10+
this.m = m;
11+
sort(n);
12+
}
13+
14+
// @Override
15+
public void sort(int[] arr) {
16+
setInicio(System.currentTimeMillis());
17+
ordenar(arr, arr.length, 1);
18+
m.updateAnimaciones();
19+
DibujarGraficos.finSort = true;
20+
}
21+
22+
/*
23+
* The parameter dir indicates the sorting direction, ASCENDING or DESCENDING;
24+
* if (a[i] > a[j]) agrees with the direction, then a[i] and a[j] are
25+
* interchanged.
26+
*/
27+
void compAndSwap(int a[], int i, int j, int dir) {
28+
if ((a[i] > a[j] && dir == 1) || (a[i] < a[j] && dir == 0)) {
29+
// Swapping elements
30+
int temp = a[i];
31+
a[i] = a[j];
32+
a[j] = temp;
33+
m.updateAnimaciones();
34+
}
35+
}
36+
37+
void bitonicMerge(int a[], int low, int cnt, int dir) {
38+
if (cnt > 1) {
39+
int k = cnt / 2;
40+
for (int i = low; i < low + k; i++)
41+
compAndSwap(a, i, i + k, dir);
42+
bitonicMerge(a, low, k, dir);
43+
bitonicMerge(a, low + k, k, dir);
44+
m.updateAnimaciones();
45+
}
46+
}
47+
48+
void bitonicSort(int a[], int low, int cnt, int dir) {
49+
if (cnt > 1) {
50+
int k = cnt / 2;
51+
bitonicSort(a, low, k, 1);
52+
bitonicSort(a, low + k, k, 0);
53+
bitonicMerge(a, low, cnt, dir);
54+
}
55+
}
56+
57+
void ordenar(int a[], int N, int up) {
58+
bitonicSort(a, 0, N, up);
59+
}
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Ordenar.Algoritmos;
2+
3+
import Ordenar.AdicionalesSorts;
4+
import Ordenar.ISort;
5+
import Principal.DibujarGraficos;
6+
import Principal.MainAplicacion;
7+
8+
public class IterativeQuick extends AdicionalesSorts implements ISort {
9+
public IterativeQuick(MainAplicacion m, int[] n) {
10+
this.m = m;
11+
sort(n);
12+
}
13+
14+
// @Override
15+
public void sort(int[] arr) {
16+
setInicio(System.currentTimeMillis());
17+
qSort(arr, 0, arr.length - 1);
18+
DibujarGraficos.finSort = true;
19+
}
20+
21+
private int partition(int arr[], int low, int high) {
22+
int pivot = arr[high];
23+
int i = (low - 1);
24+
for (int j = low; j <= high - 1; j++) {
25+
if (arr[j] <= pivot) {
26+
i++;
27+
int temp = arr[i];
28+
arr[i] = arr[j];
29+
arr[j] = temp;
30+
}
31+
m.updateAnimaciones();
32+
}
33+
34+
int temp = arr[i + 1];
35+
arr[i + 1] = arr[high];
36+
arr[high] = temp;
37+
return i + 1;
38+
}
39+
40+
private void qSort(int arr[], int low, int high) {
41+
if (low < high) {
42+
int pi = partition(arr, low, high);
43+
qSort(arr, low, pi - 1);
44+
qSort(arr, pi + 1, high);
45+
}
46+
}
47+
}

‎src/Ordenar/Algoritmos/Quick.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public void quickSort(int array[], int izq, int der) {
3333
aux = array[i];
3434
array[i] = array[j];
3535
array[j] = aux;
36+
cambiosArray++;
3637
}
3738
accesoArray++;
38-
cambiosArray++;
3939
m.updateAnimaciones();
4040
}
4141

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Ordenar.Algoritmos;
2+
3+
import Ordenar.AdicionalesSorts;
4+
import Ordenar.ISort;
5+
import Principal.DibujarGraficos;
6+
import Principal.MainAplicacion;
7+
8+
public class RecursiveBubble extends AdicionalesSorts implements ISort {
9+
public RecursiveBubble(MainAplicacion m, int[] n) {
10+
this.m = m;
11+
sort(n);
12+
}
13+
14+
// @Override
15+
public void sort(int[] arr) {
16+
setInicio(System.currentTimeMillis());
17+
bubbleSort(arr, arr.length);
18+
DibujarGraficos.finSort = true;
19+
}
20+
21+
public void bubbleSort(int arr[], int n) {
22+
if (n == 1)
23+
return;
24+
25+
for (int i = 0; i < n - 1; i++)
26+
if (arr[i] > arr[i + 1]) {
27+
int temp = arr[i];
28+
arr[i] = arr[i + 1];
29+
arr[i + 1] = temp;
30+
cambiosArray++;
31+
}
32+
accesoArray++;
33+
m.updateAnimaciones();
34+
bubbleSort(arr, n - 1);
35+
m.textos();
36+
}
37+
}

‎src/Principal/MainAplicacion.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,23 @@
3434
import Ordenar.Algoritmos.Gnome;
3535
import Ordenar.Algoritmos.Heap;
3636
import Ordenar.Algoritmos.Inserccion;
37+
import Ordenar.Algoritmos.IterativeQuick;
3738
import Ordenar.Algoritmos.Merge;
3839
import Ordenar.Algoritmos.OddEven;
3940
import Ordenar.Algoritmos.Pancake;
4041
import Ordenar.Algoritmos.Pigeonhole;
4142
import Ordenar.Algoritmos.Quick;
4243
import Ordenar.Algoritmos.RadixLSD;
44+
import Ordenar.Algoritmos.RecursiveBubble;
4345
import Ordenar.Algoritmos.Selection;
4446
import Ordenar.Algoritmos.Shell;
47+
import Ordenar.Algoritmos.Bitonic;
4548

4649
public class MainAplicacion extends AdicionalesSorts {
4750
// NOMBRES DE LOS ALGORITMOS
48-
final String[] nombreAlgoritmos = { "Bubble", "Bubble Optimized", "Cocktail", "Cycle", "Gnome", "Heap", "Insertion",
49-
"Merge", "Odd Even", "Pancake", "Pigeonhole", "Quick", "Radix LSD", "Selection", "Shell" };
51+
final String[] nombreAlgoritmos = { "Bitonic", "Bubble", "Bubble Optimized", "Cocktail", "Cycle", "Gnome", "Heap",
52+
"Insertion", "Merge", "Odd Even", "Pancake", "Pigeonhole", "Quick", "Radix LSD", "Recursive Bubble",
53+
"Selection", "Shell" };
5054
// DISEÑO GRAFICO DE LAS ANIMACIONES
5155
final static String[] nombreGrafico = { "Escalera", "Piramide horizontal", "Pixel", "Circulo", "Circunferencia",
5256
"Espiral" };
@@ -424,56 +428,65 @@ public void menuSorting() {
424428
textos();
425429
switch (seleccionAlgoritmo) {
426430
case 0:
427-
sorts = new Bubble(mainApp, barras.arrayPrincipal);
431+
sorts = new Bitonic(mainApp, barras.arrayPrincipal);
428432
break;
429433
case 1:
430-
sorts = new BubbleOptimized(mainApp, barras.arrayPrincipal);
434+
sorts = new Bubble(mainApp, barras.arrayPrincipal);
431435
break;
432436
case 2:
433-
sorts = new Cocktail(mainApp, barras.arrayPrincipal);
437+
sorts = new BubbleOptimized(mainApp, barras.arrayPrincipal);
434438
break;
435439
case 3:
436-
sorts = new Cycle(mainApp, barras.arrayPrincipal);
440+
sorts = new Cocktail(mainApp, barras.arrayPrincipal);
437441
break;
438442
case 4:
439-
sorts = new Gnome(mainApp, barras.arrayPrincipal);
443+
sorts = new Cycle(mainApp, barras.arrayPrincipal);
440444
break;
441445
case 5:
442-
sorts = new Heap(mainApp, barras.arrayPrincipal);
446+
sorts = new Gnome(mainApp, barras.arrayPrincipal);
443447
break;
444448
case 6:
445-
sorts = new Inserccion(mainApp, barras.arrayPrincipal);
449+
sorts = new Heap(mainApp, barras.arrayPrincipal);
446450
break;
447451
case 7:
448-
sorts = new Merge(mainApp, barras.arrayPrincipal);
452+
sorts = new Inserccion(mainApp, barras.arrayPrincipal);
449453
break;
450454
case 8:
451-
sorts = new OddEven(mainApp, barras.arrayPrincipal);
455+
sorts = new IterativeQuick(mainApp, barras.arrayPrincipal);
452456
break;
453457
case 9:
454-
sorts = new Pancake(mainApp, barras.arrayPrincipal);
458+
sorts = new Merge(mainApp, barras.arrayPrincipal);
455459
break;
456460
case 10:
457-
sorts = new Pigeonhole(mainApp, barras.arrayPrincipal);
461+
sorts = new OddEven(mainApp, barras.arrayPrincipal);
458462
break;
459463
case 11:
460-
sorts = new Quick(mainApp, barras.arrayPrincipal);
464+
sorts = new Pancake(mainApp, barras.arrayPrincipal);
461465
break;
462466
case 12:
463-
sorts = new RadixLSD(mainApp, barras.arrayPrincipal);
467+
sorts = new Pigeonhole(mainApp, barras.arrayPrincipal);
464468
break;
465469
case 13:
466-
sorts = new Selection(mainApp, barras.arrayPrincipal);
470+
sorts = new Quick(mainApp, barras.arrayPrincipal);
467471
break;
468472
case 14:
473+
sorts = new RadixLSD(mainApp, barras.arrayPrincipal);
474+
break;
475+
case 15:
476+
sorts = new RecursiveBubble(mainApp, barras.arrayPrincipal);
477+
break;
478+
case 16:
479+
sorts = new Selection(mainApp, barras.arrayPrincipal);
480+
break;
481+
case 17:
469482
sorts = new Shell(mainApp, barras.arrayPrincipal);
470483
break;
471484
default:
472485
barras.desordenarArray();
473486
break;
474487
}
475488

476-
if (seleccionAlgoritmo < comboBoxTipoSort.getWidth())
489+
if (DibujarGraficos.finSort)
477490
sorts = new FinSort(mainApp, barras.arrayPrincipal);
478491
}
479492
reinicio();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /