Вы находитесь на странице: 1из 10

 ALGORTIMO DE LA BURBUJA

import java.util.Scanner;
public class bubleSort {
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
System.out.println("Digite la cantidad de numeros a ordenar: ");
int n = leer.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Digite el Numero " + (i+1) + ": ");
A[i] = leer.nextInt();
}
BubbleSort(A);
System.out.println("\nNumeros Ordenados:\n");
for (int i = 0; i < n; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static void BubbleSort(int[] A){
int aux = 0;
for(int i=0;i<A.length;i++){
for(int j=A.length-1;j > i;j--){
if(A[j] < A[j-1]){
aux = A[j];
A[j] = A[j-1];
A[j-1] = aux;
}
}
}
}

 ALGORITMOS COUNTINGSORT
import java.util.Scanner;
public class countingSort {
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
System.out.println("Digite la cantidad de numeros a ordenar: ");
int n = leer.nextInt();
int[] A = new int[n+1];
int k = 0;
for (int i = 0; i < n; i++) {
System.out.println("Digite el Numero " + (i+1) + ": ");
A[i] = leer.nextInt();
if(k<A[i])
k = A[i];
}
A[n] = 0;//se añade un cero al final
k++;
A = CountingSort(A,k);
System.out.println("\nNumeros Ordenados:\n");
//empieza en 1 para no mostrar el cero que se añadió
for (int i = 1; i < A.length; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static int[] CountingSort(int[] A, int k){

int[] B = new int[A.length];


int[] C = new int[k];

for(int i=0;i<k;i++){
C[i] = 0;
}
for(int j=1;j<A.length;j++){
C[A[j-1]]++;
}
for(int i=1;i<k;i++){
C[i] += C[i-1];
}
for(int j=A.length-1;j>0;j--) {
B[C[A[j-1]]] = A[j-1];
C[A[j-1]]--;
}
return B;
}
}

 ALGORITMOS QUICKSORT
import java.util.Scanner;
public class quickSort {

public static void main(String[] args) {


Scanner leer = new Scanner(System.in);
System.out.println("Digite la cantidad de numeros a ordenar: ");
int n = leer.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Digite el Numero " + (i + 1) + ": ");
A[i] = leer.nextInt();
}
A = QuickSort(A, 0, n-1);
System.out.println("\nNumeros Ordenados:\n");
for (int i = 0; i < A.length; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
public static int[] QuickSort(int[] A, int p, int r) {
if (p < r) {
A = partition(A, p, r);
int q1 = q;
A = QuickSort(A, p, q1 + 1);
A = QuickSort(A, q1 + 1, r);
}
return A;
}
public static int[] partition(int[] A, int p, int r) {
int x = A[r];
int i = p - 1;
int aux = 0;
for (int j = p; j < r; j++) {
if (A[j] <= x) {
i++;
aux = A[j];
A[j] = A[i];
A[i] = aux;
}
}
aux = A[i];
A[i] = A[r];
A[r] = aux;
q = i;
return A;
}
public static int q = 0;
}

 ALGORITMO DE HEAPSORT
import java.util.Scanner;
public class heapSort {
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
System.out.println("Digite la cantidad de numeros a ordenar: ");
int n = leer.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Digite el Numero " + (i + 1) + ": ");
A[i] = leer.nextInt();
}
HeapSort(A);
System.out.println("\nNumeros Ordenados:\n");
for (int i = n-1; i >= 0; i--) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}

public static void HeapSort(int[] A) {


int ultimoP = padre(A.length - 1);
int hijoD, hijoI, aux;
for (int i = 0; i <= ultimoP; i++) {
hijoI = (2 * i) + 1;
hijoD = (2 * i) + 2;
if (A[i] > A[hijoI]) {
aux = A[i];
A[i] = A[hijoI];
A[hijoI] = aux;
}
if (hijoD < A.length && A[hijoI] > A[hijoD]) {
if (A[i] > A[hijoD]) {
aux = A[i];
A[i] = A[hijoD];
A[hijoD] = aux;
verificarP(i, A);
}
}
}
int pos = 0;
boolean continuar;
for (int i = A.length - 1; i >= 0; i--) {
aux = A[0];
A[0] = A[i];
A[i] = aux;
pos = 0;
hijoI = (2 * pos) + 1;
continuar = true;
while (hijoI < i && continuar) {
hijoD = (2 * pos) + 2;
if (hijoD < i && A[hijoD] < A[hijoI]) {
if (A[pos] > A[hijoD]) {
aux = A[pos];
A[pos] = A[hijoD];
A[hijoD] = aux;
pos = hijoD;
} else {
continuar = false;
}
} else {
if (A[pos] > A[hijoI]) {
aux = A[pos];
A[pos] = A[hijoI];
A[hijoI] = aux;
pos = hijoI;
} else {
continuar = false;
}
}
hijoI = (2 * pos) + 1;
}
}
}
public static int padre(int i) {
int Pi = 0;
Pi = (i % 2 == 0) ? ((i - 2) / 2) : ((i - 1) / 2);
return Pi;
}
public static void verificarP(int i, int[] A) {
int Pi = padre(i);
if (Pi >= 0) {
if (A[Pi] > A[i]) {
int aux = A[i];
A[i] = A[Pi];
A[Pi] = aux;
verificarP(Pi, A);
}
}
}
}

 ALGORITMO MERGESORT
import java.util.Scanner;
public class mergeSort {
public static void merge(int[] A, int p, int q, int r) {
int n1= q - p + 1;
int n2 = r - q;
int L[] = new int[n1+1];
int R[] = new int[n2+1];
for(int i=0;i<n1;i++){
L[i] = A[p+i];
}
for(int j=0;j<n2;j++){
R[j] = A[q+j+1];
}
L[n1] = Integer.MAX_VALUE;
R[n2] = Integer.MAX_VALUE;
int i=0;
int j=0;
for(int k=p;k<=r;k++){
if(L[i]<R[j]){
A[k] = L[i];
i++;
}
else{
A[k] = R[j];
j++;
}
}
}
public static void merge_sort(int[] A, int p, int r) {
int q;
if (p < r) {
q = (int) (p + r) / 2;
merge_sort(A, p, q);
merge_sort(A, q + 1, r);
merge(A, p, q, r);
}
}
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
System.out.println("Digite la cantidad de numeros a ordenar: ");
int n = leer.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Digite el Numero " + (i+1) + ": ");
A[i] = leer.nextInt();
}
merge_sort(A, 0, n - 1);
System.out.println("\nNumeros Ordenados:\n");
for (int i = 0; i < n; i++) {
System.out.print("\t" + A[i]);
}
System.out.println("");
}
}

 ALGORITMO DE SELECCIÓN
import java.util.*;
public class seleccion {
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
System.out.println("Teclee la cantidad de números a leer: ");
int n = leer.nextInt();
ArrayList<Integer> A = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
System.out.println("Escriba el Numero " + (i + 1) + ": ");
A.add(leer.nextInt());
}
int pos = 0;
do{
System.out.println("\nEscriba la posición del numero que le interesa: ");
pos = leer.nextInt();
if(pos > n || pos < 1){
System.out.println("\nLa Posicion Maxima es: " + n + " y la minima es 1");
}
}while(pos > n || pos < 1);
int numero = Seleccion(A, pos);
System.out.println("\nNumero: " + numero + "\n");
}
public static int Seleccion(ArrayList<Integer> A, int pos) {
int cont = 0;
boolean encontrado = false;
int al = 0;
int numero = 0;
int i = 0;
ArrayList<Integer> menores;
ArrayList<Integer> mayores;
int numb = 0;
do {
menores = new ArrayList<Integer>();
mayores = new ArrayList<Integer>();
al = (int) (Math.random() * A.size());
numero = A.get(al);
for (i = 0; i < A.size(); i++) {
if (A.get(i) <= numero) {
menores.add(A.get(i));
} else {
mayores.add(A.get(i));
}
}
if ((menores.size() + cont) < pos) {
cont += menores.size();
A = new ArrayList<Integer>();
A = mayores;
} else if ((menores.size() + cont) > pos) {
A = new ArrayList<Integer>();
A = menores;
} else if ((menores.size() + cont) == pos) {
A = new ArrayList<Integer>();
A = menores;
if (A.size() < 2) {
numb = menores.get(0);
encontrado = true;
}
}
} while (!encontrado);
return numb;
}
}

 ALGORITMO PRIM
import java.util.*;
public class Prim {
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
System.out.println("Cantidad de Nodos");
int nodosT = leer.nextInt();
int matriz[][] = new int[nodosT][nodosT];
System.out.println("A continuacion digite la relación que tiene\n"
+ "cada nodo con respecto a otro, sí dicha relacion no existe\n"
+ "digitar -1");
for (int i = 0; i < nodosT; i++) {
System.out.println("\nRelación del nodo " + (i + 1));
for (int j = 0; j < nodosT; j++) {
System.out.print("\nCon el nodo " + (j + 1) + ": ");
matriz[i][j] = leer.nextInt();
}
}
System.out.println("");
int menor;
ArrayList<String> conexiones = new ArrayList<String>();
for (int i = 0; i < nodosT; i++) {
menor = Integer.MAX_VALUE;
for (int j = 0; j < nodosT; j++) {
if (matriz[i][j] < menor && matriz[i][j] > -1) {
menor = matriz[i][j];
}
}
for (int j = 0; j < nodosT; j++) {
if (matriz[i][j] == menor) {
conexiones.add((i+1) + "-" + (j+1));
}
}
}
for(int i=0;i<conexiones.size();i++){
System.out.println(conexiones.get(i));
}
}
}

Вам также может понравиться