Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main6
- {
- public static void QuickSort(double[] arr, int low, int high) {
- if(arr == null || arr.length == 0)
- return;
- if(low >= high)
- return;
- //ВЗИМАМЕ ЕЛЕМЕНТА, С КОЙТО СЕ СРАВНЯВАТ ЧИСЛАТА
- int middle = low + (high - low) / 2;
- double pivot = arr[middle];
- //ПРАВИМ ЛЯВОТО ЧИСЛО ДА Е ПО-МАЛКО ОТ СРАВНЯВАЩОТО ЧИСЛО pivot И ДЯСНОТО ЧИСЛО ДА Е ПО-ГОЛЯМО ОТ СРАВНЯВАЩОТО ЧИСЛО pivot
- int i = low, j = high;
- while (i <= j){
- while (arr[i] < pivot){
- i++;
- }
- while (arr[j] > pivot){
- j--;
- }
- if(i <= j){
- double temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- i++;
- j--;
- }
- }
- //СОРТИРАНЕ НА ДВЕТЕ ЧАСТИ НА МАСИВА
- if(low < j)
- QuickSort(arr, low, j);
- if(high > i)
- QuickSort(arr, i, high);
- }
- // MAIN //
- public static void main(String args[]) {
- Scanner sc = new Scanner(System.in);// ВЪВЕЖДАНЕ НА ОБЕКТ
- System.out.print("Input the number of elements : ");
- int N = sc.nextInt();//БРОЯ НА ЕЛЕМЕНТИТЕ В МАСИВА
- double arr[] = new double[N];//ВЪВЕЖДАНЕ НА ПЪРВИ МАСИВ
- for (int a = 0; a < N; a++) {
- System.out.printf("Input the [%d] element : ", a);
- arr[a] = sc.nextInt();
- }
- System.out.println();
- double arr1[] = new double[N];//ВЪВЕЖДАНЕ НА ВТОРИ МАСИВ
- for (int b = 0; b < N; b++) {
- System.out.printf("Input the [%d] element : ", b);
- arr1[b] = sc.nextInt();
- }
- System.out.println();
- double arr2[] = new double[N];//ВЪВЕЖДАНЕ НА ТРЕТИ МАСИВ
- for (int c = 0; c < N; c++) {
- arr2[c] = arr[c] / arr1[c];//РЕЗУЛТАТА НА ТРЕТИЯ МАСИВ
- }
- System.out.println();
- System.out.print("Before Sorting: ");
- System.out.println(Arrays.toString(arr2));// ПРИНТИРАНЕ НА ТРЕТИЯ МАСИВ arr2 ПРЕДИ СОРТИРАНЕТО
- //ИЗРАЗЯВАНЕ НА НУЖНИТЕ ЕЛЕМЕНТИ ЗА СОРТИРАЩИЯ МЕТОД QuickSort
- int low = 0;
- int high = arr2.length - 1;
- QuickSort(arr2, low, high);// ИЗПЪЛНЯВАНЕ НА СОРТИРАШИЯ МЕТОД QuickSort
- System.out.print("After Sorting: ");
- System.out.println(Arrays.toString(arr2));// ПРИНТИРАНЕ НА ТРЕТИЯ МАСИВ arr2 СЛЕД СОРТИРАНЕТО
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment