Samorokimetal

Partition of Arrays

Jan 9th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class PartitionOfArrays {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scan = new Scanner(System.in);
  9.         System.out.printf("Input the number of elements : ");
  10.         int N = Integer.parseInt(scan.nextLine());
  11.         double[] Arr1 = new double[N];
  12.         double[] Arr2 = new double[N];
  13.         double[] Arr3 = new double[N];
  14.  
  15.         System.out.println();
  16.  
  17.         for (int i = 0; i <= Arr1.length - 1; i++) {
  18.             System.out.printf("Input element [%d] of Arr1: ", i);
  19.             Arr1[i] = Integer.parseInt(scan.nextLine());
  20.         }
  21.         System.out.println();
  22.  
  23.         for (int j = 0; j <= Arr2.length - 1; j++) {
  24.             System.out.printf("Input element [%d] of Arr2: ", j);
  25.             Arr2[j] = Integer.parseInt(scan.nextLine());
  26.  
  27.             Arr3[j] = Arr1[j] / Arr2[j];
  28.             Arr3[j] = Arr3[j] * 100;
  29.             Arr3[j] = Math.round(Arr3[j]);
  30.             Arr3[j] = Arr3[j] / 100;
  31.         }
  32.  
  33.         scan.close();
  34.         System.out.println();
  35.         System.out.println("Results before sort: " + Arrays.toString(Arr3));
  36.  
  37.         for (int k = 1; k < Arr3.length; k++) {
  38.             double key = Arr3[k];
  39.             int a = k - 1;
  40.  
  41.             while((a > -1) && (Arr3[a] > key)) {
  42.                 Arr3[a + 1] = Arr3 [a];
  43.                 a--;
  44.             }
  45.  
  46.             Arr3[a+1] = key;
  47.         }
  48.         System.out.print("Results after sort: " + Arrays.toString(Arr3));
  49.     }
  50. }
Add Comment
Please, Sign In to add comment