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

Practica 1 #195

Open
wants to merge 11 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
33 changes: 33 additions & 0 deletions ago-dic-2024/practicas/Alondra Tristan/Parcial 1/ejercicio_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.util.Scanner;

public class ejercicio_1 {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print("Num medicinas: ");
int n = s.nextInt();
String[] medicinas = new String[n];

System.out.print("Medicinas: ");
for (int i = 0; i < n; i++) {
medicinas[i] = s.toString();
}

//Insertion sort
for (int i = 1; i < medicinas.length; i++) {
String elem_actual = medicinas[i];
int j = i - 1;
while (j >= 0 && medicinas[j] > elem_actual) {
medicinas[j + 1] = medicinas[j];
j--;
}
medicinas[j + 1] = elem_actual;
}




}

}
39 changes: 39 additions & 0 deletions ago-dic-2024/practicas/Alondra Tristan/Parcial 1/ejercicio_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.Scanner;

public class ejercicio_2 {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print("Num medicinas: ");
int n = s.nextInt();
String[] medicinas = new String[n];
int[] pas = new int[n];

System.out.print("Medicinas y pastillas sobrantes: ");
for (int i = 0; i < n; i++) {
medicinas[i] = s.toString();
pas[i]=s.nextInt();
}

//Insertion sort
for (int i = 1; i < pas.length; i++) {
int elem_actual = pas[i];
int j = i - 1;
while (j >= 0 && pas[j] < elem_actual) {
pas[j + 1] = pas[j];
j--;
}
pas[j + 1] = elem_actual;
}

for (int i = 0; i < n; i++) {
System.out.print(medicinas[i]);
if (i < n - 1) {
System.out.print(" ");
}
}

}//end main

}//end class
70 changes: 70 additions & 0 deletions ago-dic-2024/practicas/Alondra Tristan/Parcial 2/ejercicio_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.Scanner;

public class OrdenarColores {
class Color {
public String nombre;
public int numero;

Color(String nombre, int numero) {
this.nombre = nombre;
this.numero = numero;
}
}

public static void quickSort(Color[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);

// Ordenar los elementos antes y después de la partición
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

public static int partition(Color[] arr, int low, int high) {
Color pivot = arr[high];
int i = (low - 1);

for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
Color temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

Color 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);

System.out.print("Introduce el color y el num: ");
int n = Integer.parseInt(scanner.nextLine());
Color[] colores = new Color[n];

// Leer los colores y almacenarlos en un arreglo
for (int i = 0; i < n; i++) {
String[] entrada = scanner.nextLine().split(" ");
String nombre = entrada[0];
int numero = Integer.parseInt(entrada[1]);
colores[i] = new Color(nombre, numero);
}

// Ordenar los numeros usando Quick Sort
quickSort(colores, 0, n - 1);

for (Color color : colores) {
System.out.print(color.nombre + " ");
}

scanner.close();

}//end main

}//end class
55 changes: 55 additions & 0 deletions ago-dic-2024/practicas/Alondra Tristan/Parcial 2/ejercicio_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.Scanner;

public class OrdenarNumerosCountingSort {
class CountingSort {
void countSort(int array[], int size) {
int[] output = new int[size + 1];

int max = array[0];
for (int i = 1; i < size; i++) {
if (array[i] > max)
max = array[i];
}
int[] count = new int[max + 1];

for (int i = 0; i < max; ++i) {
count[i] = 0;
}


for (int i = 0; i < size; i++) {
count[array[i]]++;
}

for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}

for (int i = size - 1; i >= 0; i--) {
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
}


for (int i = 0; i < size; i++) {
array[i] = output[i];
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Numeros que Juan quiere ordenar: ");
int n = scanner.nextInt();
int[] CountingSort = new int[n];

System.out.print("Numeros ordenados: ");
for (int i = 0; i < n; i++) {
CountingSort[i] = scanner.nextInt();
}


}//end main

}//end class counting sort
}//end class
14 changes: 14 additions & 0 deletions ago-dic-2024/practicas/Alondra Tristan/Practica 1/practica.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.Scanner;

public class SumaDeEnteros {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int a = scanner.nextInt();
int b = scanner.nextInt();


System.out.println(a + b);
}
}
49 changes: 49 additions & 0 deletions ago-dic-2024/practicas/Alondra Tristan/Practica 2/practica_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package practicas;
import java.util.Scanner;

public class practica_2 {

public static void main(String[] args) {

int n= 10;
int m = 5;

System.out.println(n + " " + m);

int ni[] = {1, 3, 1, 10, 8, 4, 5, 6, 9, 5};

for (int i = 0; i < ni.length; i++) {
System.out.print(ni[i] + " ");
}

System.out.println();

//Insertion sort
for (int i = 1; i < ni.length; i++) {
int elem_actual = ni[i];
int j = i - 1;

while (j >= 0 && ni[j] > elem_actual) {
ni[j + 1] = ni[j];
j--;
}
ni[j + 1] = elem_actual;
}

int tiempo = 0;
int problemasResueltos = 0;

for (int i = 0; i < n; i++) {
if (tiempo + ni[i] <= m) {
tiempo += ni[i];
problemasResueltos++;
} else {
break;
}
}

System.out.print(problemasResueltos);

}//end main

}//end class
38 changes: 38 additions & 0 deletions ago-dic-2024/practicas/Alondra Tristan/Practica 3/pokemones.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.Scanner;

public class pokemones {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Numero de pokemones: ");
int n = scanner.nextInt();
int[] pokemones = new int[n];

System.out.print("Pokemones: ");
for (int i = 0; i < n; i++) {
pokemones[i] = scanner.nextInt();
}

//Bubble sort
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (pokemones[j] < pokemones[j + 1]) {
int temp = pokemones[j];
pokemones[j] = pokemones[j + 1];
pokemones[j + 1] = temp;
}
}
}

for (int i = 0; i < n; i++) {
System.out.print(pokemones[i]);
if (i < n - 1) {
System.out.print(" ");
}
}


}//end main

}//end class
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package practicas;
import java.util.Scanner;

public class OrdenarPalabrasMergeSort {

public static void mergeSort(String[] arr, int left, int right) {
if (left < right) {
int middle = left + (right - left) / 2;

// Ordenar la primera y segunda mitad
mergeSort(arr, left, middle);
mergeSort(arr, middle + 1, right);

// Combinar las mitades ordenadas
merge(arr, left, middle, right);
}
}

public static void merge(String[] arr, int left, int middle, int right) {
// Tamaños de los subarrays temporales
int n1 = middle - left + 1;
int n2 = right - middle;

// Crear arrays temporales
String[] leftArray = new String[n1];
String[] rightArray = new String[n2];

// Copiar los datos a los arrays temporales
for (int i = 0; i < n1; ++i)
leftArray[i] = arr[left + i];
for (int j = 0; j < n2; ++j)
rightArray[j] = arr[middle + 1 + j];

// Combinar los arrays temporales

int i = 0, j = 0;
int k = left;
while (i < n1 && j < n2) {
if (leftArray[i].compareTo(rightArray[j]) <= 0) {
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
}
k++;
}

// Copiar los elementos restantes de leftArray, si quedan
while (i < n1) {
arr[k] = leftArray[i];
i++;
k++;
}

// Copiar los elementos restantes de rightArray, si quedan
while (j < n2) {
arr[k] = rightArray[j];
j++;
k++;
}
}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Introduzca el numero y las palabras: ");
// Leer el número de palabras
int n = Integer.parseInt(scanner.nextLine());

// Leer las palabras y almacenarlas en un array
String[] palabras = scanner.nextLine().split(" ");

// Ordenar las palabras usando Merge Sort
mergeSort(palabras, 0, palabras.length - 1);

System.out.print("Palabras ordenadas: ");
// Imprimir las palabras ordenadas
System.out.println(String.join(" ", palabras));

scanner.close();

}

}
Loading