Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alfonso #202

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions ago-dic-2024/Alfonso_Hernandez/Parcial_2_1.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
58 changes: 58 additions & 0 deletions ago-dic-2024/Alfonso_Hernandez/Parcial_2_2.java
Original file line number Diff line number Diff line change
@@ -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 + " ");
}
}
}
23 changes: 23 additions & 0 deletions ago-dic-2024/Alfonso_Hernandez/Practica_1.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
54 changes: 54 additions & 0 deletions ago-dic-2024/Alfonso_Hernandez/Practica_2.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
34 changes: 34 additions & 0 deletions ago-dic-2024/Alfonso_Hernandez/Practica_3.java
Original file line number Diff line number Diff line change
@@ -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] + " ");
}
}
}
85 changes: 85 additions & 0 deletions ago-dic-2024/Alfonso_Hernandez/Practica_4.java
Original file line number Diff line number Diff line change
@@ -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();
}
}


Loading