HayCZ

izapr_cv05

Mar 23rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. package izapr_cv05;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Izapr_cv05 {
  6.  
  7.     public static void main(String[] args) {
  8.        int[] pole = new int[20];
  9.        pole = naplnPole(pole, 1, 100);
  10.        vypisPole(pole);
  11.        vypisPole(vytridPole(pole));
  12.        
  13.     }
  14.      public static void vypisPole(int pole[]){
  15.         System.out.println("" + Arrays.toString(pole));
  16.     }
  17.     public static int generujCislo(int min, int max){
  18.         return (int) Math.round(Math.random() * (max - min) + min);
  19.     }
  20.     public static int[] naplnPole(int pole[], int min, int max){
  21.         for (int i = 0; i < pole.length; i++) {
  22.             pole[i] = generujCislo(min,max);
  23.         }
  24.      return pole;  
  25.     }
  26.     public static int[] vytridPole(int pole[]){
  27.             int prvek, j;
  28.             for (int i = 1; i <pole.length; i++) {
  29.               prvek = pole[i]; //Načtení prvku z pole dle aktuálního indexu.
  30.               j = i - 1; //index - 1
  31.               while ((j >= 0) && (pole[j] > prvek)) { //Pokud je J >= 0 and pole[j] > načtený prvek)
  32.                 pole[j + 1] = pole[j]; // Pole +1 = předchozí (přesun)
  33.                 j--; //j odečtení
  34.               }
  35.               pole[j + 1] = prvek; //Uložení prvku.
  36.             }
  37.         return pole;    
  38.     }
  39.    
  40. }
Advertisement
Add Comment
Please, Sign In to add comment