-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
public class Insercion { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
public class Seleccion { | ||
|
||
public static void sort(int[] nums) { | ||
|
||
} | ||
} |