From 9ee61453be85c5b3bf479e3853143c234b02ca3e Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Mon, 23 Jan 2023 20:43:28 -0600 Subject: [PATCH 01/21] Primera Practica --- ene-jun-2023/Alfonso Hernandez/Practica_1.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Practica_1.java diff --git a/ene-jun-2023/Alfonso Hernandez/Practica_1.java b/ene-jun-2023/Alfonso Hernandez/Practica_1.java new file mode 100644 index 000000000..bb9872d59 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Practica_1.java @@ -0,0 +1,13 @@ +import java.util.*; + +public class Practica_1 { + + public static void main(String[] args) { + Scanner s = new Scanner(System.in); + int n1 = s.nextInt(); + int n2 = s.nextInt(); + + System.out.println(n1 + n2); + } + +} From 11c3ead5411904afc203915bb33cdfcaec0e160c Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Sun, 5 Feb 2023 21:13:43 -0600 Subject: [PATCH 02/21] Practica 2 --- ene-jun-2023/Alfonso Hernandez/Practica_2.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Practica_2.py diff --git a/ene-jun-2023/Alfonso Hernandez/Practica_2.py b/ene-jun-2023/Alfonso Hernandez/Practica_2.py new file mode 100644 index 000000000..c1a3105cb --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Practica_2.py @@ -0,0 +1,16 @@ +def pares(numeros): + numeros_pares = [] + + for n in numeros: + if n % 2 == 0: + numeros_pares.append(n) + + return numeros_pares + +arr = [3, 43, 2, 5, 4, 6, 5, 4, 334, 45, 23, 123, 323, 45, 34, 23, 43, 5, 46, 78] + +resultado = pares(arr) +ordenados = sorted(resultado, reverse=True) + +print(arr) +print(ordenados) \ No newline at end of file From 1249287769578729dc3cd350011dcd962c5cda0e Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Sat, 11 Feb 2023 09:49:06 -0600 Subject: [PATCH 03/21] Practica 3 --- .../Alfonso Hernandez/Practica_3.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Practica_3.java diff --git a/ene-jun-2023/Alfonso Hernandez/Practica_3.java b/ene-jun-2023/Alfonso Hernandez/Practica_3.java new file mode 100644 index 000000000..a6fb16846 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Practica_3.java @@ -0,0 +1,32 @@ +import java.util.Scanner; +public class Practica_3 { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + String[] medicines = new String[n]; + scanner.nextLine(); + for (int i = 0; i < n; i++) { + medicines[i] = scanner.nextLine(); + } + scanner.close(); + + sortMedicines(medicines); + + for (String medicine : medicines) { + System.out.println(medicine); + } + } + + private static void sortMedicines(String[] medicines) { + for (int i = 0; i < medicines.length - 1; i++) { + for (int j = i + 1; j < medicines.length; j++) { + if (medicines[i].length() > medicines[j].length()) { + String temp = medicines[i]; + medicines[i] = medicines[j]; + medicines[j] = temp; + } + } + } + } +} \ No newline at end of file From 8c7b9d85a76d93ee8a426cd8bb276ac10792100f Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Wed, 15 Feb 2023 09:45:54 -0600 Subject: [PATCH 04/21] Practica 4 --- .../Alfonso Hernandez/Practica_4.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Practica_4.java diff --git a/ene-jun-2023/Alfonso Hernandez/Practica_4.java b/ene-jun-2023/Alfonso Hernandez/Practica_4.java new file mode 100644 index 000000000..0c0ae70ca --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Practica_4.java @@ -0,0 +1,34 @@ +import java.util.Scanner; + +public class Practica_4 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + // Lectura del número de pokemons + int n = sc.nextInt(); + + // Lectura de la lista de pokemons + //pokemons = arreglo + + int[] pokemons = new int[n]; + for (int i = 0; i < n; i++) { + pokemons[i] = sc.nextInt(); + } + + // Ordenamiento burbuja + for (int i = 0; i < pokemons.length - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (pokemons[j] < pokemons[j+1]) { + int temp = pokemons[j]; + pokemons[j] = pokemons[j+1]; + pokemons[j+1] = temp; + } + } + } + + // Impresión de la lista ordenada + for (int i = 0; i < n; i++) { + System.out.print(pokemons[i] + " "); + } + } +} From 2aca3d7c8749ee601f7a9daa33f346360074b251 Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Mon, 6 Mar 2023 20:59:57 -0600 Subject: [PATCH 05/21] Examen Marge --- ene-jun-2023/Alfonso Hernandez/Examen_1.java | 33 +++++++++ ene-jun-2023/Alfonso Hernandez/Examen_2.java | 74 ++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Examen_1.java create mode 100644 ene-jun-2023/Alfonso Hernandez/Examen_2.java diff --git a/ene-jun-2023/Alfonso Hernandez/Examen_1.java b/ene-jun-2023/Alfonso Hernandez/Examen_1.java new file mode 100644 index 000000000..0816af553 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Examen_1.java @@ -0,0 +1,33 @@ +package Algoritmos_Ordenamiento_Busqueda; + +import java.util.Scanner; + +public class Examen_1 { + + public static void main(String args[]) { + + //Este codigo se realizo implementando el algoritmo de Selection Sort + Scanner sc = new Scanner(System.in); + String[] dinosaurios = sc.nextLine().split(" "); + sc.close(); + + int n = dinosaurios.length; + for (int i = 0; i < n - 1; i++) { + int minimo = i; + for (int j = i + 1; j < n; j++) { + if (dinosaurios[j].length() > dinosaurios[minimo].length()) { + minimo = j; + } + } + + String temp = dinosaurios[i]; + dinosaurios[i] = dinosaurios[minimo]; + dinosaurios[minimo] = temp; + } + + for (String elem : dinosaurios) { + System.out.println(elem); + } + + } +} diff --git a/ene-jun-2023/Alfonso Hernandez/Examen_2.java b/ene-jun-2023/Alfonso Hernandez/Examen_2.java new file mode 100644 index 000000000..0d6e9144c --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Examen_2.java @@ -0,0 +1,74 @@ +package Algoritmos_Ordenamiento_Busqueda; + +import java.util.*; + +public class Examen_2 { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int n = scanner.nextInt(); + Pelicula[] movies = new Pelicula[n]; + + for (int i = 0; i < n; i++) { + String title = scanner.next(); + int duration = scanner.nextInt(); + int minutos = scanner.nextInt(); + movies[i] = new Pelicula(title, duration, minutos); + } + + mergeSort(movies, 0, n - 1); + + for (Pelicula movie : movies) { + System.out.print(movie.title + " "); + } + } + + static void mergeSort(Pelicula[] movies, int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + mergeSort(movies, left, mid); + mergeSort(movies, mid + 1, right); + merge(movies, left, mid, right); + } + } + + static void merge(Pelicula[] movies, int left, int mid, int right) { + Pelicula[] temp = new Pelicula[right - left + 1]; + + int i = left, j = mid + 1, k = 0; + + while (i <= mid && j <= right) { + if (movies[i].duration < movies[j].duration) { + temp[k++] = movies[i++]; + } else { + temp[k++] = movies[j++]; + } + } + + while (i <= mid) { + temp[k++] = movies[i++]; + } + + while (j <= right) { + temp[k++] = movies[j++]; + } + + for (i = left; i <= right; i++) { + movies[i] = temp[i - left]; + } + } + + static class Pelicula { + String title; + int duration; + int quality; + + Pelicula(String title, int duration, int quality) { + this.title = title; + this.duration = duration; + this.quality = quality; + } + } +} + + From efee7e1cc09bf0a40d11e1d3fff64d0a4c9eacb9 Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Thu, 16 Mar 2023 13:13:24 -0600 Subject: [PATCH 06/21] Practica_6 --- .../Alfonso Hernandez/Practica_6.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Practica_6.java diff --git a/ene-jun-2023/Alfonso Hernandez/Practica_6.java b/ene-jun-2023/Alfonso Hernandez/Practica_6.java new file mode 100644 index 000000000..3c4127272 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Practica_6.java @@ -0,0 +1,83 @@ +package Algoritmos_Ordenamiento_Busqueda; + +import java.util.*; + +class Participante { + + String Nombre; + int Oro, Plata, Bronce; + + public Participante(String nombre, int oro, int plata, int bronce) { + this.Nombre = nombre; + this.Oro = oro; + this.Plata = plata; + this.Bronce = bronce; + } +} + +public class Practica_6 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + List concursantes = new ArrayList<>(); + for (int i = 0; i < n; i++) { + String nombre = sc.next(); + int oro = sc.nextInt(); + int plata = sc.nextInt(); + int bronce = sc.nextInt(); + Participante concursante = new Participante(nombre, oro, plata, bronce); + concursantes.add(concursante); + } + + quickSort(concursantes, 0, n - 1); + + for (Participante c : concursantes) { + System.out.println(c.Nombre); + } + } + + public static void quickSort(List concursantes, int inicio, int fin) { + if (inicio < fin) { + int indicePivote = particion(concursantes, inicio, fin); + quickSort(concursantes, inicio, indicePivote - 1); + quickSort(concursantes, indicePivote + 1, fin); + } + } + + public static int particion(List concursantes, int inicio, int fin) { + // Seleccionamos el último elemento como pivote + Participante pivote = concursantes.get(fin); + int indiceMenor = inicio - 1; + + for (int i = inicio; i < fin; i++) { + // Comparamos los concursantes según las reglas del problema + if (comparar(concursantes.get(i), pivote) <= 0) { + indiceMenor++; + intercambiar(concursantes, indiceMenor, i); + } + } + + intercambiar(concursantes, indiceMenor + 1, fin); + return indiceMenor + 1; + } + + public static int comparar(Participante c1, Participante c2) { + if (c1.Oro != c2.Oro) { + return Integer.compare(c2.Oro, c1.Oro); + } else if (c1.Plata != c2.Plata) { + return Integer.compare(c2.Plata, c1.Plata); + } else if (c1.Bronce != c2.Bronce) { + return Integer.compare(c2.Bronce, c1.Bronce); + } else { + return c1.Nombre.compareTo(c2.Nombre); + } + } + + public static void intercambiar(List concursantes, int i, int j) { + Participante temp = concursantes.get(i); + concursantes.set(i, concursantes.get(j)); + concursantes.set(j, temp); + } +} From f68b7deeed61208c4a0f4ea9f0773946ef9f24e3 Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Thu, 27 Apr 2023 19:35:28 -0600 Subject: [PATCH 07/21] Practica 7 --- .../Alfonso Hernandez/Practica_7.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Practica_7.java diff --git a/ene-jun-2023/Alfonso Hernandez/Practica_7.java b/ene-jun-2023/Alfonso Hernandez/Practica_7.java new file mode 100644 index 000000000..df4fd04ba --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Practica_7.java @@ -0,0 +1,80 @@ +package Algoritmos_Ordenamiento_Busqueda; + +import java.util.*; + +public class Practica_7 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); // número de medicinas + int q = sc.nextInt(); // número de peticiones + + // Leer los nombres de las medicinas + String[] medicinas = new String[n]; + for (int i = 0; i < n; i++) { + medicinas[i] = sc.next(); + } + + // Ordenar el arreglo de medicinas usando quicksort + quicksort(medicinas, 0, n - 1); + + // Para cada petición, buscar el nombre de la medicina usando binary search + for (int i = 0; i < q; i++) { + String nombreMedicina = sc.next(); + int posicion = binarySearch(medicinas, nombreMedicina); + System.out.println(posicion); + } + } + + // Algoritmo quicksort para ordenar un arreglo de strings + public static void quicksort(String[] arr, int low, int high) { + if (low < high) { + int pi = partition(arr, low, high); + + quicksort(arr, low, pi - 1); + quicksort(arr, pi + 1, high); + } + } + + // Algoritmo partition para quicksort + public static int partition(String[] arr, int low, int high) { + String pivot = arr[high]; + int i = low - 1; + + for (int j = low; j < high; j++) { + if (arr[j].compareTo(pivot) < 0) { + i++; + + String temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + String temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + + return i + 1; + } + + // Algoritmo binary search para buscar un string en un arreglo ordenado + public static int binarySearch(String[] arr, String x) { + int left = 0; + int right = arr.length - 1; + + while (left <= right) { + int mid = left + (right - left) / 2; + + if (arr[mid].equals(x)) { + return mid; + } else if (arr[mid].compareTo(x) < 0) { + left = mid + 1; + } else { + right = mid - 1; + } + } + + return -1; + } +} From 48c3906f648d02014c23fc214050cf6989ccc476 Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Mon, 8 May 2023 20:29:08 -0600 Subject: [PATCH 08/21] Segundo Examen --- ene-jun-2023/Alfonso Hernandez/Examen2_1.java | 36 ++++++++ ene-jun-2023/Alfonso Hernandez/Examen2_2.java | 89 +++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Examen2_1.java create mode 100644 ene-jun-2023/Alfonso Hernandez/Examen2_2.java diff --git a/ene-jun-2023/Alfonso Hernandez/Examen2_1.java b/ene-jun-2023/Alfonso Hernandez/Examen2_1.java new file mode 100644 index 000000000..7436963e3 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Examen2_1.java @@ -0,0 +1,36 @@ +package Examen2; + +import java.util.Arrays; +import java.util.Scanner; + +public class Examen2_1 { +//primer entero **n** representa el número de libros en la repisa de mate y el string **x** el nombre del libro que busca Toño. + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int l = sc.nextInt(); + String x = sc.next(); + String[] n = new String[l]; + for (int i = 0; i < l; i++) { + n[i] = sc.next(); + } + Arrays.sort(n); + int indice = busquedaBinaria(n, x); + System.out.println(indice); + } + + public static int busquedaBinaria(String[] arr, String elem) { + int inicio = 0; + int fin = arr.length - 1; + while (inicio <= fin) { + int mitad = (inicio + fin) / 2; + if (arr[mitad].equals(elem)) { + return mitad; + } else if (arr[mitad].compareTo(elem) < 0) { + inicio = mitad + 1; + } else { + fin = mitad - 1; + } + } + return -1; + } +} diff --git a/ene-jun-2023/Alfonso Hernandez/Examen2_2.java b/ene-jun-2023/Alfonso Hernandez/Examen2_2.java new file mode 100644 index 000000000..29ae9ebd3 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Examen2_2.java @@ -0,0 +1,89 @@ +package Examen2; + +import java.util.Scanner; + +public class Examen2_2 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + // Leer N y Q + int N = sc.nextInt(); + int Q = sc.nextInt(); + + // Leer las canciones + String[] canciones = new String[N]; + for (int i = 0; i < N; i++) { + canciones[i] = sc.next(); + } + + // Ordenar las canciones + quickSort(canciones, 0, N - 1); + + // Buscar las canciones solicitadas + for (int i = 0; i < Q; i++) { + String cancionBuscada = sc.next(); + int pos = binarySearch(canciones, cancionBuscada); + System.out.println(pos); + } + + // Imprimir el nuevo orden de la playlist + for (String cancion : canciones) { + System.out.print(cancion + " "); + } + } + + public static void quickSort(String[] arr, int inicio, int fin) { + if (inicio >= fin) { + return; + } + + int pivot = (inicio + fin) / 2; + pivot = partition(arr, inicio, fin, pivot); + + quickSort(arr, inicio, pivot - 1); + quickSort(arr, pivot + 1, fin); + } + + public static int partition(String[] arr, int inicio, int fin, int pivot) { + String valorPivot = arr[pivot]; + swap(arr, pivot, fin); + + int pos = inicio; + for (int i = inicio; i < fin; i++) { + if (arr[i].compareTo(valorPivot) < 0) { + swap(arr, i, pos); + pos++; + } + } + swap(arr, pos, fin); + return pos; + } + + public static void swap(String[] arr, int i, int j) { + String temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + + public static int binarySearch(String[] arr, String elem) { + int inicio = 0; + int fin = arr.length - 1; + + while (inicio <= fin) { + int mitad = (inicio + fin) / 2; + + if (arr[mitad].equals(elem)) { + return mitad; + } + + if (arr[mitad].compareTo(elem) > 0) { + fin = mitad - 1; + } else { + inicio = mitad + 1; + } + } + + return -1; + } +} From 0cdb2f43944ba25638e541a57b1525c2b72b1f60 Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Tue, 16 May 2023 22:19:52 -0600 Subject: [PATCH 09/21] Practica_5 --- .../Alfonso Hernandez/Practica_5.java | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Practica_5.java diff --git a/ene-jun-2023/Alfonso Hernandez/Practica_5.java b/ene-jun-2023/Alfonso Hernandez/Practica_5.java new file mode 100644 index 000000000..6c77ca1b6 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Practica_5.java @@ -0,0 +1,93 @@ +package Algoritmos_Ordenamiento_Busqueda; + +import java.util.*; + +public class Practica_5 { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int handsCount = scanner.nextInt(); + scanner.nextLine(); // Consumir la línea vacía + + for (int i = 0; i < handsCount; i++) { + String hand = scanner.nextLine(); + String[] cards = hand.split(" "); + mergeSort(cards, 0, cards.length - 1); + System.out.println(String.join(" ", cards)); + } + } + + private static void mergeSort(String[] array, int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + mergeSort(array, left, mid); + mergeSort(array, mid + 1, right); + merge(array, left, mid, right); + } + } + + private static void merge(String[] array, int left, int mid, int right) { + int n1 = mid - left + 1; + int n2 = right - mid; + + String[] leftArray = new String[n1]; + String[] rightArray = new String[n2]; + + for (int i = 0; i < n1; i++) { + leftArray[i] = array[left + i]; + } + + for (int j = 0; j < n2; j++) { + rightArray[j] = array[mid + j + 1]; + } + + int i = 0, j = 0; + int k = left; + + while (i < n1 && j < n2) { + if (compareCards(leftArray[i], rightArray[j]) <= 0) { + array[k] = leftArray[i]; + i++; + } else { + array[k] = rightArray[j]; + j++; + } + k++; + } + + while (i < n1) { + array[k] = leftArray[i]; + i++; + k++; + } + + while (j < n2) { + array[k] = rightArray[j]; + j++; + k++; + } + } + + private static int compareCards(String card1, String card2) { + if (card1.length() < 2 || card2.length() < 2) { + return 0; // Manejar caso de cadena inválida + } + + int valueComparison = compareCardValues(card1.charAt(0), card2.charAt(0)); + if (valueComparison != 0) { + return valueComparison; + } else { + return compareCardSuits(card1.charAt(1), card2.charAt(1)); + } + } + + private static int compareCardValues(char value1, char value2) { + String values = "A23456789TJQK"; + return values.indexOf(value1) - values.indexOf(value2); + } + + private static int compareCardSuits(char suit1, char suit2) { + String suits = "ECTD"; + return suits.indexOf(suit1) - suits.indexOf(suit2); + } +} From f05afb7764f2364b630f55f677f3a8ca4271d5bd Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Tue, 16 May 2023 22:21:11 -0600 Subject: [PATCH 10/21] Practica_8 --- .../Alfonso Hernandez/Practica_8.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Practica_8.java diff --git a/ene-jun-2023/Alfonso Hernandez/Practica_8.java b/ene-jun-2023/Alfonso Hernandez/Practica_8.java new file mode 100644 index 000000000..1077388b6 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Practica_8.java @@ -0,0 +1,83 @@ +package Algoritmos_Ordenamiento_Busqueda; + +import java.util.*; + +public class Practica_8 { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int N = sc.nextInt(); // Número de personas (nodos) + int E = sc.nextInt(); // Número de conexiones entre personas (aristas) + int Z = sc.nextInt(); // Identificador de Juan + + // Crear el grafo usando listas de adyacencia + List> grafo = new ArrayList<>(); + for (int i = 0; i < N; i++) { + grafo.add(new ArrayList<>()); + } + + // Leer las conexiones entre personas y construir el grafo + for (int i = 0; i < E; i++) { + int nodo1 = sc.nextInt(); + int nodo2 = sc.nextInt(); + grafo.get(nodo1).add(nodo2); + grafo.get(nodo2).add(nodo1); // El grafo es no dirigido + } + + // Encontrar el camino más corto usando BFS + List camino = encontrarCaminoMasCorto(grafo, Z); + + // Imprimir el camino más corto + System.out.println("------salida-----"); + for (int i = 0; i < camino.size(); i++) { + if (i > 0) { + System.out.print(" "); + } + System.out.print(camino.get(i)); + } + System.out.println(); + } + + private static List encontrarCaminoMasCorto(List> grafo, int destino) { + int N = grafo.size(); // Número de nodos en el grafo + boolean[] visitado = new boolean[N]; // Para marcar los nodos visitados + int[] distancia = new int[N]; // Para almacenar las distancias desde el nodo inicial + int[] padre = new int[N]; // Para almacenar los nodos antecesores en el camino + + Queue cola = new LinkedList<>(); + cola.add(0); // María es el nodo inicial + + visitado[0] = true; + distancia[0] = 0; + padre[0] = -1; + + while (!cola.isEmpty()) { + int nodoActual = cola.poll(); + + // Si hemos llegado al nodo destino, construir el camino y terminar + if (nodoActual == destino) { + List camino = new ArrayList<>(); + int nodo = destino; + while (nodo != -1) { + camino.add(nodo); + nodo = padre[nodo]; + } + Collections.reverse(camino); + return camino; + } + + // Recorrer los nodos adyacentes + for (int nodoAdyacente : grafo.get(nodoActual)) { + if (!visitado[nodoAdyacente]) { + visitado[nodoAdyacente] = true; + distancia[nodoAdyacente] = distancia[nodoActual] + 1; + padre[nodoAdyacente] = nodoActual; + cola.add(nodoAdyacente); + } + } + } + + return new ArrayList<>(); // No se encontró un camino al nodo destino + } +} From 2165510551fe9416a391dfe0415066b83f4f4942 Mon Sep 17 00:00:00 2001 From: alfonso24178 <18034152hernandez@gmail.com> Date: Fri, 9 Jun 2023 20:45:39 -0600 Subject: [PATCH 11/21] Extra --- .../Alfonso Hernandez/Extra_Orde.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 ene-jun-2023/Alfonso Hernandez/Extra_Orde.java diff --git a/ene-jun-2023/Alfonso Hernandez/Extra_Orde.java b/ene-jun-2023/Alfonso Hernandez/Extra_Orde.java new file mode 100644 index 000000000..4756b32b6 --- /dev/null +++ b/ene-jun-2023/Alfonso Hernandez/Extra_Orde.java @@ -0,0 +1,85 @@ +package Extra_Orde; + +import java.util.*; + +public class Extra_Orde { + public static void main(String[] args) { + //n y m representan la cantidad de libros y la cantidad de búsquedas a realizar, respectivamente. + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int m = scanner.nextInt(); + scanner.nextLine(); // Consumir el salto de línea después de leer m + + String[][] books = new String[n][2]; + + for (int i = 0; i < n; i++) { + String book = scanner.next(); + int pages = scanner.nextInt(); + books[i][0] = book; + books[i][1] = Integer.toString(pages); + } + + quicksort(books, 0, n - 1); + + // Imprimir la lista ordenada de libros + for (int i = 0; i < n; i++) { + System.out.print(books[i][0] + " "); + } + System.out.println(); + + // Realizar las búsquedas y mostrar los resultados + for (int i = 0; i < m; i++) { + String searchBook = scanner.next().trim(); + int position = busquedaBinaria(books, searchBook); + System.out.println(position); + } + + scanner.close(); + } + + static int partition(String[][] arr, int low, int high) { + int i = low - 1; + int pivot = Integer.parseInt(arr[high][1]); + + for (int j = low; j < high; j++) { + if (Integer.parseInt(arr[j][1]) <= pivot) { + i++; + String[] temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + String[] temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + + return i + 1; + } + + static void quicksort(String[][] arr, int low, int high) { + if (low < high) { + int pi = partition(arr, low, high); + quicksort(arr, low, pi - 1); + quicksort(arr, pi + 1, high); + } + } + + static int busquedaBinaria(String[][] arr, String target) { + int low = 0; + int high = arr.length - 1; + + while (low <= high) { + int mid = (low + high) / 2; + if (arr[mid][0].equals(target)) { + return mid; + } else if (arr[mid][0].compareTo(target) < 0) { + low = mid + 1; + } else { + high = mid - 1; + } + } + + return -1; + } +} \ No newline at end of file From 59964e3cf11aae09ab1165aa6827babfd6607bd0 Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Mon, 9 Sep 2024 14:42:22 -0600 Subject: [PATCH 12/21] Practica 1 --- .../Alfonso_Hernandez/Practica_1.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Practica_1.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Practica_1.java b/ago-dic-2024/Alfonso_Hernandez/Practica_1.java new file mode 100644 index 000000000..8442a6eea --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Practica_1.java @@ -0,0 +1,23 @@ +package algoritmos; + +import java.util.Scanner; + +public class Practica_1 { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + String input = scanner.nextLine(); + + + String[] numbers = input.split(" "); + + + int num1 = Integer.parseInt(numbers[0]); + int num2 = Integer.parseInt(numbers[1]); + int sum = num1 + num2; + + + System.out.println(sum); + scanner.close(); + } +} From 132e62c7ce7c7da86ff8e6b50bf005d101d34b3f Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Thu, 12 Sep 2024 10:46:47 -0600 Subject: [PATCH 13/21] Practica 2 --- .../Alfonso_Hernandez/Practica_2.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Practica_2.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Practica_2.java b/ago-dic-2024/Alfonso_Hernandez/Practica_2.java new file mode 100644 index 000000000..0df46b7a3 --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Practica_2.java @@ -0,0 +1,54 @@ +package algoritmos; + +import java.util.Scanner; + +public class Practica_2 { + + // Insertion Sort + public static void insertionSort(int[] arr) { + for (int i = 1; i < arr.length; i++) { + int key = arr[i]; + int j = i - 1; + // Mueve los elementos del arreglo que son mayores que la clave, a una posición adelante + while (j >= 0 && arr[j] > key) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int n = scanner.nextInt();//Tareas + int m = scanner.nextInt();//Tiempo + + // Leer el tiempo que toma resolver cada problema + int[] tiempos = new int[n]; + for (int i = 0; i < n; i++) { + tiempos[i] = scanner.nextInt(); + } + + // Ordenar los tiempos usando insertion sort + insertionSort(tiempos); + + // Problemas puede resolver David + int problemasResueltos = 0; + int tiempoUsado = 0; + + for (int i = 0; i < n; i++) { + if (tiempoUsado + tiempos[i] <= m) { + tiempoUsado += tiempos[i]; + problemasResueltos++; + } else { + break; + } + } + + // Imprimir el número de problemas que puede resolver + System.out.println(problemasResueltos); + + scanner.close(); + } +} From 0094412cde33187d800e9682f6576f9e61571540 Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Mon, 30 Sep 2024 16:38:49 -0600 Subject: [PATCH 14/21] Pratica 3 --- .../Alfonso_Hernandez/Practica_3.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Practica_3.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Practica_3.java b/ago-dic-2024/Alfonso_Hernandez/Practica_3.java new file mode 100644 index 000000000..f28a37e58 --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Practica_3.java @@ -0,0 +1,34 @@ +package algoritmos; + +import java.util.Scanner; + +public class Practica_3 { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int n = scanner.nextInt(); + + int[] pokemon = new int[n]; + + for (int i = 0; i < n; i++) { + pokemon[i] = scanner.nextInt(); + } + + // Bubble Sort + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (pokemon[j] < pokemon[j + 1]) { + + int temp = pokemon[j]; + pokemon[j] = pokemon[j + 1]; + pokemon[j + 1] = temp; + } + } + } + + for (int i = 0; i < n; i++) { + System.out.print(pokemon[i] + " "); + } + } +} From 8b363b1177f15c89688baefea04e2045874b5cd7 Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Mon, 7 Oct 2024 21:12:38 -0600 Subject: [PATCH 15/21] problema1 --- .../Alfonso_Hernandez/Primer_Problema.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Primer_Problema.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Primer_Problema.java b/ago-dic-2024/Alfonso_Hernandez/Primer_Problema.java new file mode 100644 index 000000000..b0e791503 --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Primer_Problema.java @@ -0,0 +1,46 @@ +package Parcial_1; + +import java.util.Scanner; + +public class Primer_Problema { + + public static void insertionSort(String[] arr) { + for (int i = 1; i < arr.length; i++) { + String key = arr[i]; + int j = i - 1; + + // Mover los elementos que son mayores que la clave a una posición adelante + while (j >= 0 && arr[j].compareTo(key) > 0) { + arr[j + 1] = arr[j]; + j = j - 1; + } + arr[j + 1] = key; + } + } + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + //Numero de documentos + int n = sc.nextInt(); + sc.nextLine(); + + // Nombres de los medicamentos + String[] medicinas = sc.nextLine().split(" "); + + // Ordenar alfabéticamente + insertionSort(medicinas); + + // Imprimir resultado + for (int i = 0; i < medicinas.length; i++) { + if (i > 0) System.out.print(" "); + System.out.print(medicinas[i]); + } + System.out.println(); + + sc.close(); + } +} + + From 93a2330eedc3c5f70d428c40d30ac0d85212815f Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Mon, 7 Oct 2024 21:14:00 -0600 Subject: [PATCH 16/21] problema2 --- .../Alfonso_Hernandez/Segundo_Problema.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Segundo_Problema.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Segundo_Problema.java b/ago-dic-2024/Alfonso_Hernandez/Segundo_Problema.java new file mode 100644 index 000000000..c82a17f3a --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Segundo_Problema.java @@ -0,0 +1,56 @@ +package Parcial_1; + +import java.util.Scanner; + +public class Segundo_Problema { + + // Bubble Sort + public static void bubbleSort(String[] nombres, int[] cantidades) { + String tempNombre; + int tempCantidad; + for (int j = 0; j < cantidades.length - 1; j++) { + for (int i = 0; i < cantidades.length - 1 - j; i++) { + // Ordenar de mayor a menor según la cantidad + if (cantidades[i] < cantidades[i + 1]) { + // Intercambiar las cantidades + tempCantidad = cantidades[i]; + cantidades[i] = cantidades[i + 1]; + cantidades[i + 1] = tempCantidad; + + + tempNombre = nombres[i]; + nombres[i] = nombres[i + 1]; + nombres[i + 1] = tempNombre; + } + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int n = scanner.nextInt(); + + // Leer los nombres y cantidades de pastillas + String[] nombres = new String[n]; + int[] cantidades = new int[n]; + + for (int i = 0; i < n; i++) { + nombres[i] = scanner.next(); + cantidades[i] = scanner.nextInt(); + } + + bubbleSort(nombres, cantidades); + + for (int i = 0; i < nombres.length; i++) { + System.out.print(" "); + System.out.print(nombres[i]); + } + + scanner.close(); + + } +} + + + From d2a9f41edb4c7805c37c0153fcd9fd32e768c174 Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Mon, 21 Oct 2024 21:14:54 -0600 Subject: [PATCH 17/21] Practica 4 --- .../Alfonso_Hernandez/Practica_4.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Practica_4.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Practica_4.java b/ago-dic-2024/Alfonso_Hernandez/Practica_4.java new file mode 100644 index 000000000..339fd2d1a --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Practica_4.java @@ -0,0 +1,85 @@ +package algoritmos; +import java.util.Scanner; + +public class Practica_4 { + + // Método principal para ordenar la lista usando Merge Sort + public static void mergeSort(String[] arr, int left, int right) { + if (left < right) { + int mid = (left + right) / 2; + + // Divide la lista en dos mitades + mergeSort(arr, left, mid); + mergeSort(arr, mid + 1, right); + + // Fusiona las dos mitades + merge(arr, left, mid, right); + } + } + + // Método para fusionar dos mitades + public static void merge(String[] arr, int left, int mid, int right) { + int n1 = mid - left + 1; + int n2 = right - mid; + + // Arrays temporales para almacenar las dos mitades + String[] leftArray = new String[n1]; + String[] rightArray = new String[n2]; + + // Copia los datos a los arrays temporales + for (int i = 0; i < n1; i++) { + leftArray[i] = arr[left + i]; + } + for (int i = 0; i < n2; i++) { + rightArray[i] = arr[mid + 1 + i]; + } + + // Índices iniciales de las dos mitades y del array fusionado + int i = 0, j = 0, k = left; + + // Ordena y fusiona los dos arrays temporales en el array original + while (i < n1 && j < n2) { + if (leftArray[i].compareTo(rightArray[j]) <= 0) { + arr[k] = leftArray[i]; + i++; + } else { + arr[k] = rightArray[j]; + j++; + } + k++; + } + + // Copia los elementos restantes de leftArray + while (i < n1) { + arr[k] = leftArray[i]; + i++; + k++; + } + + // Copia los elementos restantes de rightArray + while (j < n2) { + arr[k] = rightArray[j]; + j++; + k++; + } + } + + public static void main(String[] args) { + // Entrada de datos + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); // Número de palabras + sc.nextLine(); // Consumir la línea sobrante + String[] words = sc.nextLine().split(" "); // Lista de palabras + + // Llama al método mergeSort para ordenar la lista + mergeSort(words, 0, words.length - 1); + + // Imprime las palabras ordenadas + for (String word : words) { + System.out.print(word + " "); + } + sc.close(); + } +} + + From 0e8af4c1bf025303a1657da89fd5227e135f3a29 Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Fri, 25 Oct 2024 21:57:16 -0600 Subject: [PATCH 18/21] Practica 5 --- .../Alfonso_Hernandez/Practica_5.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Practica_5.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Practica_5.java b/ago-dic-2024/Alfonso_Hernandez/Practica_5.java new file mode 100644 index 000000000..582fe99f9 --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Practica_5.java @@ -0,0 +1,97 @@ +package algoritmos; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +class Libro { + + String nombre; + int paginas; + + public Libro(String nombre, int paginas) { + this.nombre = nombre; + this.paginas = paginas; + } +} + +public class Practica_5 { + + public static void mergeSort(List libros, int izq, int der) { + if (izq < der) { + int medio = (izq + der) / 2; + + // Dividir + mergeSort(libros, izq, medio); + mergeSort(libros, medio + 1, der); + + // Combinar + merge(libros, izq, medio, der); + } + } + + public static void merge(List libros, int izq, int medio, int der) { + int n1 = medio - izq + 1; + int n2 = der - medio; + + List izquierda = new ArrayList<>(n1); + List derecha = new ArrayList<>(n2); + + for (int i = 0; i < n1; i++) { + izquierda.add(libros.get(izq + i)); + } + for (int j = 0; j < n2; j++) { + derecha.add(libros.get(medio + 1 + j)); + } + + int i = 0, j = 0; + int k = izq; + + // Merge de subarreglos + while (i < n1 && j < n2) { + if (izquierda.get(i).paginas <= derecha.get(j).paginas) { + libros.set(k, izquierda.get(i)); + i++; + } else { + libros.set(k, derecha.get(j)); + j++; + } + k++; + } + + // Copiar elementos restantes + while (i < n1) { + libros.set(k, izquierda.get(i)); + i++; + k++; + } + while (j < n2) { + libros.set(k, derecha.get(j)); + j++; + k++; + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + int n = Integer.parseInt(scanner.nextLine()); + List libros = new ArrayList<>(); + + for (int i = 0; i < n; i++) { + String[] entrada = scanner.nextLine().split(" "); + String nombre = entrada[0]; + int paginas = Integer.parseInt(entrada[1]); + libros.add(new Libro(nombre, paginas)); + } + + mergeSort(libros, 0, libros.size() - 1); + + StringBuilder resultado = new StringBuilder(); + for (Libro libro : libros) { + resultado.append(libro.nombre).append(" "); + } + + System.out.println(resultado.toString().trim()); + } +} From d247200d76ff23f3564532ec4dbb7a807fb0e725 Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Fri, 8 Nov 2024 21:17:03 -0600 Subject: [PATCH 19/21] Practica_6 --- .../Alfonso_Hernandez/Practica_6.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Practica_6.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Practica_6.java b/ago-dic-2024/Alfonso_Hernandez/Practica_6.java new file mode 100644 index 000000000..ee261e128 --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Practica_6.java @@ -0,0 +1,59 @@ +package algoritmos; + +import java.util.Scanner; + +public class Practica_6{ + + // Función de Quick Sort + public static void quickSort(int[] arr, int low, int high) { + if (low < high) { + int pivotIndex = partition(arr, low, high); + quickSort(arr, low, pivotIndex - 1); + quickSort(arr, pivotIndex + 1, high); + } + } + + // Función para dividir el arreglo y encontrar el índice del pivote + public static int partition(int[] arr, int low, int high) { + int pivot = arr[high]; + int i = low - 1; + for (int j = low; j < high; j++) { + if (arr[j] <= pivot) { + i++; + // Intercambia arr[i] y arr[j] + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + // Coloca el pivote en su lugar correcto + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + return i + 1; + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Lectura de entrada + int n = scanner.nextInt(); + int[] colors = new int[n]; + for (int i = 0; i < n; i++) { + colors[i] = scanner.nextInt(); + } + + // Ordenamiento con Quick Sort + quickSort(colors, 0, n - 1); + + // Impresión del resultado + for (int i = 0; i < n; i++) { + System.out.print(colors[i]); + if (i < n - 1) { + System.out.print(" "); + } + } + scanner.close(); + } +} + From e5925be3843d4a432f38ea125c41feb1bbdca633 Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Mon, 11 Nov 2024 20:51:12 -0600 Subject: [PATCH 20/21] Ordenando --- .../Alfonso_Hernandez/Parcial_2_1.java | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Parcial_2_1.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Parcial_2_1.java b/ago-dic-2024/Alfonso_Hernandez/Parcial_2_1.java new file mode 100644 index 000000000..3e3cf5343 --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Parcial_2_1.java @@ -0,0 +1,93 @@ +package Parcial_2; + +import java.util.Scanner; + +public class Parcial_2_1 { + + // Mapa que define el orden de los colores del arcoíris + private static final String[] COLOR_ORDER = {"Rojo", "Anaranjado", "Amarillo", "Verde", "Indigo", "Violeta", "Azul"}; + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Leer el número de plumones + int n = Integer.parseInt(scanner.nextLine()); + String[] plumones = new String[n]; + + // Leer los plumones + for (int i = 0; i < n; i++) { + plumones[i] = scanner.nextLine(); + } + + // Ordenar los plumones utilizando Quick Sort + quickSort(plumones, 0, plumones.length - 1); + + // Imprimir los plumones ordenados + for (String plumon : plumones) { + System.out.println(plumon); + } + + scanner.close(); + } + + // Método de Quick Sort + private static void quickSort(String[] array, int low, int high) { + if (low < high) { + int pi = partition(array, low, high); + quickSort(array, low, pi - 1); + quickSort(array, pi + 1, high); + } + } + + // Método para particionar el array + private static int partition(String[] array, int low, int high) { + String pivot = array[high]; // Elegimos el último elemento como pivote + int i = (low - 1); // Índice del elemento más pequeño + + for (int j = low; j < high; j++) { + // Si el elemento actual es menor o igual que el pivote + if (comparePlumones(array[j], pivot) <= 0) { + i++; + // Intercambiamos array[i] y array[j] + String temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + + // Intercambiamos array[i + 1] y array[high] (o pivote) + String temp = array[i + 1]; + array[i + 1] = array[high]; + array[high] = temp; + + return i + 1; + } + + // Método para comparar plumones + private static int comparePlumones(String plumon1, String plumon2) { + String[] parts1 = plumon1.split("-"); + String[] parts2 = plumon2.split("-"); + + String color1 = parts1[0]; + String color2 = parts2[0]; + int dilution1 = Integer.parseInt(parts1[1]); + int dilution2 = Integer.parseInt(parts2[1]); + + int colorComparison = Integer.compare(getColorIndex(color1), getColorIndex(color2)); + if (colorComparison != 0) { + return colorComparison; + } + + return Integer.compare(dilution1, dilution2); + } + + // Método para obtener el índice de un color en el orden del arcoíris + private static int getColorIndex(String color) { + for (int i = 0; i < COLOR_ORDER.length; i++) { + if (COLOR_ORDER[i].equals(color)) { + return i; + } + } + return -1; + } +} From 895fcdf985951157b0809ed4ec7a59f3c7ede17c Mon Sep 17 00:00:00 2001 From: Alfonso Hernandez <18034152hernandez@gmail.com> Date: Mon, 11 Nov 2024 20:52:54 -0600 Subject: [PATCH 21/21] =?UTF-8?q?Ordenar=20N=C3=BAmeros=20con=20Counting?= =?UTF-8?q?=20Sort?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Alfonso_Hernandez/Parcial_2_2.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ago-dic-2024/Alfonso_Hernandez/Parcial_2_2.java diff --git a/ago-dic-2024/Alfonso_Hernandez/Parcial_2_2.java b/ago-dic-2024/Alfonso_Hernandez/Parcial_2_2.java new file mode 100644 index 000000000..024078337 --- /dev/null +++ b/ago-dic-2024/Alfonso_Hernandez/Parcial_2_2.java @@ -0,0 +1,58 @@ +package Parcial_2; + +import java.util.Scanner; + +public class Parcial_2_2 { + + public static void countingSort(int[] arr) { + if (arr.length == 0) { + return; + } + + // Encontrar el valor máximo en el arreglo + int max = arr[0]; + for (int num : arr) { + if (num > max) { + max = num; + } + } + + // Crear un array de conteo + int[] count = new int[max + 1]; + + // Contar las ocurrencias de cada número + for (int num : arr) { + count[num]++; + } + + // Reescribir el arreglo original con los números ordenados + int index = 0; + for (int i = 0; i < count.length; i++) { + while (count[i] > 0) { + arr[index++] = i; + count[i]--; + } + } + } + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + // Leer el número de elementos + int n = scanner.nextInt(); + int[] arr = new int[n]; + + // Leer los elementos del arreglo + for (int i = 0; i < n; i++) { + arr[i] = scanner.nextInt(); + } + + // Ordenar el arreglo usando Counting Sort + countingSort(arr); + + // Imprimir el arreglo ordenado + for (int num : arr) { + System.out.print(num + " "); + } + } +}