Skip to content

Instantly share code, notes, and snippets.

@user0able
Last active April 14, 2021 16:57
Show Gist options
  • Save user0able/75ce95265a17905f10b3d5cf95cff16f to your computer and use it in GitHub Desktop.
Save user0able/75ce95265a17905f10b3d5cf95cff16f to your computer and use it in GitHub Desktop.
shell short
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author 17659498-5
* Escribe un algoritmo que permita ingresar valores positivos
* en el rango [10 a 100] a un vector de
* 10 posiciones y ordene este vector por el método Shell Sort
*/
public class Problema01 {
public static void main(String[] args) throws IOException {
BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
int[] vec = new int[5];
int val, c = 0;
// Llenar el arreglo
do {
System.out.print("Ingresa valores positivos en el rango [10 a 100]: ");
val = Integer.parseInt(entrada.readLine());
if(val > 10 && val < 100){
vec[c] = val;
c++;
} else {
System.out.println("Ingrese numeros del rango");
}
} while (c < vec.length);
// Metodo Shell Sort
int n = vec.length;
int inc = n;
do {
inc = inc/2;
for (int a = 0; a < inc; a++) {
for (int i = inc + a; i < n; i+=inc) {
int j = i;
while (j - inc >= 0 && vec[j] < vec[j-inc]) {
int aux = vec[j];
vec[j] = vec[j-inc];
vec[j - inc] = aux;
j -= inc;
}
}
}
} while (inc > 1);
// Imprime arreglo
for (int i = 0; i < vec.length; i++) {
System.out.print(vec[i]+", ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment