Valeri173

Domashnoto na Krasi 4

Jan 21st, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.02 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Main6
  5. {
  6.  
  7.  
  8.     public static void QuickSort(double[] arr, int low, int high) {
  9.          if(arr == null || arr.length == 0)
  10.              return;
  11.          if(low >= high)
  12.              return;
  13.  
  14.          //ВЗИМАМЕ ЕЛЕМЕНТА, С КОЙТО СЕ СРАВНЯВАТ ЧИСЛАТА
  15.          int middle = low + (high - low) / 2;
  16.          double pivot = arr[middle];
  17.  
  18.          //ПРАВИМ ЛЯВОТО ЧИСЛО ДА Е ПО-МАЛКО ОТ СРАВНЯВАЩОТО ЧИСЛО pivot И ДЯСНОТО ЧИСЛО ДА Е ПО-ГОЛЯМО ОТ СРАВНЯВАЩОТО ЧИСЛО pivot
  19.          int i = low, j = high;
  20.          while (i <= j){
  21.              while (arr[i] < pivot){
  22.                  i++;
  23.              }
  24.              while (arr[j] > pivot){
  25.                  j--;
  26.              }
  27.              if(i <= j){
  28.                  double temp = arr[i];
  29.                  arr[i] = arr[j];
  30.                  arr[j] = temp;
  31.                  i++;
  32.                  j--;
  33.              }
  34.          }
  35.          //СОРТИРАНЕ НА ДВЕТЕ ЧАСТИ НА МАСИВА
  36.          if(low < j)
  37.              QuickSort(arr, low, j);
  38.          if(high > i)
  39.              QuickSort(arr, i, high);
  40.     }
  41.  
  42.  
  43.  
  44.     // MAIN //
  45.     public static void main(String args[]) {
  46.  
  47.         Scanner sc = new Scanner(System.in);// ВЪВЕЖДАНЕ НА ОБЕКТ
  48.         System.out.print("Input the number of elements : ");
  49.         int N = sc.nextInt();//БРОЯ НА ЕЛЕМЕНТИТЕ В МАСИВА
  50.  
  51.         double arr[] = new double[N];//ВЪВЕЖДАНЕ НА ПЪРВИ МАСИВ
  52.         for (int a = 0; a < N; a++) {
  53.             System.out.printf("Input the [%d] element : ", a);
  54.             arr[a] = sc.nextInt();
  55.         }
  56.         System.out.println();
  57.  
  58.         double arr1[] = new double[N];//ВЪВЕЖДАНЕ НА ВТОРИ МАСИВ
  59.         for (int b = 0; b < N; b++) {
  60.             System.out.printf("Input the [%d] element : ", b);
  61.             arr1[b] = sc.nextInt();
  62.         }
  63.         System.out.println();
  64.  
  65.         double arr2[] = new double[N];//ВЪВЕЖДАНЕ НА ТРЕТИ МАСИВ
  66.         for (int c = 0; c < N; c++) {
  67.             arr2[c] = arr[c] / arr1[c];//РЕЗУЛТАТА НА ТРЕТИЯ МАСИВ
  68.         }
  69.         System.out.println();
  70.  
  71.         System.out.print("Before Sorting: ");
  72.         System.out.println(Arrays.toString(arr2));// ПРИНТИРАНЕ НА ТРЕТИЯ МАСИВ arr2 ПРЕДИ СОРТИРАНЕТО
  73.  
  74.         //ИЗРАЗЯВАНЕ НА НУЖНИТЕ ЕЛЕМЕНТИ ЗА СОРТИРАЩИЯ МЕТОД QuickSort
  75.         int low = 0;
  76.         int high = arr2.length - 1;
  77.  
  78.         QuickSort(arr2, low, high);// ИЗПЪЛНЯВАНЕ НА СОРТИРАШИЯ МЕТОД QuickSort
  79.  
  80.         System.out.print("After Sorting: ");
  81.         System.out.println(Arrays.toString(arr2));// ПРИНТИРАНЕ НА ТРЕТИЯ МАСИВ arr2 СЛЕД СОРТИРАНЕТО
  82.         sc.close();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment