Valeri173

Domashnoto na Krasi 3

Jan 8th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main5
  4. {
  5.  
  6.  
  7.     public static double[] QuickSort(double[] arr) {
  8.  
  9.         int elements = arr.length;
  10.  
  11.         for(int j=0; j<elements; j++)
  12.         {
  13.  
  14.             int current_position=0;   //position of pivot element
  15.             double temp;
  16.  
  17.             for(int i=1; i<elements; i++) //Partitioning loop
  18.             {
  19.                 if(arr[i] <= arr[0])
  20.                 {
  21.                     current_position++;
  22.                     temp = arr[i];
  23.                     arr[i] = arr[current_position];
  24.                     arr[current_position] = temp;
  25.  
  26.                 }
  27.             }
  28.  
  29.  
  30.             temp = arr[0];
  31.             arr[0] = arr[current_position];
  32.             arr[current_position] = temp;
  33.  
  34.  
  35.         }
  36.         return arr;
  37.     }
  38.  
  39.  
  40.  
  41.     // MAIN //
  42.     public static void main(String args[]) {
  43.  
  44.         Scanner sc = new Scanner(System.in);
  45.         System.out.print("Input the number of elements : ");
  46.         int N = sc.nextInt();
  47.  
  48.         double arr[] = new double[N];
  49.         for (int a = 0; a < N; a++) {
  50.             System.out.printf("Input the [%d] element : ", a);
  51.             arr[a] = sc.nextInt();
  52.         }
  53.         System.out.println();
  54.  
  55.         double arr1[] = new double[N];
  56.         for (int b = 0; b < N; b++) {
  57.             System.out.printf("Input the [%d] element : ", b);
  58.             arr1[b] = sc.nextInt();
  59.         }
  60.         System.out.println();
  61.  
  62.         double arr2[] = new double[N];
  63.         for (int c = 0; c < N; c++) {
  64.             arr2[c] = arr[c] / arr1[c];
  65.         }
  66.  
  67.         System.out.println();
  68.  
  69.  
  70.  
  71.         // Print before sorting...
  72.         System.out.println("Before Sorting : ");
  73.         for(double i:arr2)
  74.         {
  75.             System.out.print(i);
  76.             System.out.print(" ");
  77.         }
  78.         System.out.println();
  79.  
  80.         QuickSort (arr2);
  81.  
  82.         // Print after sorting...
  83.         System.out.println("After Sorting : ");
  84.         for(double i:arr2)
  85.         {
  86.             System.out.print(i);
  87.             System.out.print(" ");
  88.         }
  89.  
  90.         sc.close();
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment