Skip to content

Commit

Permalink
Bubble sort y binary search
Browse files Browse the repository at this point in the history
  • Loading branch information
jandion committed Feb 26, 2024
1 parent 015edff commit 3595c44
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Complejidad/src/EjemplosComplejidad.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class EjemplosComplejidad {

public static void main(String[] args) {

int N = 10000;

int[][] numeros = new int[N][N];
for(int i =0; i<N; i++) {
for(int j = 0; j<N; j++) {
numeros[i][j]=(int)(Math.random()*N*10);
}
}
long t = System.currentTimeMillis();
int k =N;
//algoritmo
for(int i =0; i<N; i++) {
for(int j = 0; j<N; j++) {
if(numeros[i][j] == k) {
System.out.println("Encontrado");
break;
}
}
}

System.out.println(System.currentTimeMillis()-t);
}
}
23 changes: 23 additions & 0 deletions Ordenacion/src/Burbuja.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

public class Burbuja {

public static void sort(int[] nums) {
int contador = 0;
boolean haySwap = true;
while(haySwap) {
haySwap = false;
// lo de la burbuja
//recorremos el array
for (int i = 1; i < nums.length; i++) {
// comprobamos si estan desordenados
contador ++;
if (nums[i-1] > nums[i]) { // TODO: y si fueran String
// detectamos que no están ordenados
PruebasOrdenacion.swap(nums, i-1, i);
haySwap = true;
}
}
}
System.out.println("he ordenado con "+ contador+" comparaciones");
}
}
21 changes: 21 additions & 0 deletions Ordenacion/src/GeneradorDatos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.Arrays;
import java.util.Random;

public class GeneradorDatos {

private static Random r = new Random(23);


public static void main(String[] args) {
int[] nums = arrayEnteros(10);
System.out.println(Arrays.toString(nums));
}

public static int[] arrayEnteros(int l) {
int[] arr = new int[l];
for (int i = 0; i < arr.length; i++) {
arr[i] = r.nextInt(100);
}
return arr;
}
}
4 changes: 4 additions & 0 deletions Ordenacion/src/Insercion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

public class Insercion {

}
69 changes: 69 additions & 0 deletions Ordenacion/src/PruebasOrdenacion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.Arrays;

public class PruebasOrdenacion {


public static void main(String[] args) {
int[] nums = GeneradorDatos.arrayEnteros(1000);
imprimeArray(nums);
Burbuja.sort(nums);
imprimeArray(nums);

System.out.println("Está el numero 100: "+ buscaNumero(nums, 100));
}

public static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static void swap(String[] nombres, int i, int j) {
String temp = nombres[i];
nombres[i] = nombres[j];
nombres[j] = temp;
}


public static void imprimeArray(int[] nums) {
System.out.println(Arrays.toString(nums));
}

public static boolean buscaNumero(int[] nums, int buscado) {
// como se hacía en prog
// for (int i = 0; i < nums.length; i++) {
// if (buscado == nums[i]) {
// return true;
// }
// }
// return false;

// como lo hacemos ahora

return busquedaBinaria(nums, buscado, 0, nums.length-1);

}

private static boolean busquedaBinaria(int[] nums, int buscado, int inferior, int superior) {
// donde miramos?
int pos = (superior+inferior)/2;

if(superior == inferior+1 ) {
return (nums[superior] == buscado) || (nums[inferior] == buscado);
}

if(nums[pos] == buscado) {
return true;
} else if( nums[pos] < buscado ) {
//busco por la derecha
int n_sup = superior;
int n_inf = pos;
return busquedaBinaria(nums, buscado, n_inf, n_sup);
} else {
//busco por la izquierda
int n_sup = pos;
int n_inf = inferior;
return busquedaBinaria(nums, buscado, n_inf, n_sup);
}

}
}
7 changes: 7 additions & 0 deletions Ordenacion/src/Seleccion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

public class Seleccion {

public static void sort(int[] nums) {

}
}

0 comments on commit 3595c44

Please sign in to comment.