Advertisement
Guest User

xd

a guest
Nov 15th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class SzybkieSortowanie {
  4.  
  5.     public static void bubbleSort(int[] A,int p, int r) {
  6.         int temp = 0;
  7.  
  8.         for (int i = p; i <= r; i++) {
  9.             for (int j = i+1; j < r; j++) {
  10.                 if (A[i] > A[j]) {
  11.                     temp = A[j];
  12.                     A[j] = A[i];
  13.                     A[i] = temp;
  14.                 }
  15.             }
  16.         }
  17.     }
  18.  
  19.     public static int partition(int[] A, int p, int r) {
  20.         int x = A[r];
  21.         int i = p-1;
  22.  
  23.         for (int j = p; j<=r; j++) {
  24.             if(A[j]<=x) {
  25.                 i++;
  26.                 int temp = A[i];
  27.                 A[i]=A[j];
  28.                 A[j]=temp;
  29.             }
  30.         }
  31.  
  32.         if(i<r) {
  33.             return i;
  34.         }
  35.             return i-1;
  36.     }
  37.  
  38.     public static void quickSort(int[] A, int p, int r) {
  39.         if(p<r) {
  40.             int q = partition(A,p,r);
  41.             quickSort(A,p,q);
  42.             quickSort(A,q+1,r);
  43.         }
  44.     }
  45.  
  46.     public static void quickSort2(int[] A, int p, int r) {
  47.         int c = 10;
  48.         if(p<r) {
  49.             if(r - p + 1 < c) {
  50.                 bubbleSort(A,p,r);
  51.             } else {
  52.                 int q = partition(A,p,r);
  53.                 quickSort2(A,p,q);
  54.                 quickSort2(A,q+1,r);
  55.             }
  56.         }
  57.     }
  58.  
  59.     public static int[] randFill(int n) {
  60.         Random random = new Random();
  61.         int[] A = new int[n];
  62.         for (int i = 0; i < n; i++) {
  63.             A[i] = random.nextInt(n+1);
  64.         }
  65.  
  66.         return A;
  67.     }
  68.  
  69.     public static void main(String[] args) {
  70.         /*int[] A = {6,1,7,4,3,9,6,3,2,6,5,0,0,1,-8};
  71.         System.out.print("Przed sortowaniem: ");
  72.         for (int x : A) {
  73.             System.out.print(x + " ");
  74.         }
  75.         System.out.print("\nPo sortowaniu: ");
  76.         quickSort2(A,0,A.length-1);
  77.         for (int x : A) {
  78.             System.out.print(x+" ");
  79.         } */
  80.  
  81.         System.out.println("DANE LOSOWE");
  82.         System.out.println("  rozmiar tablicy   czas - algorytm 1   czas - algorytm 2\n");
  83.         for (int n = 10; n <= 25000; n*=2) {
  84.            int[] A = randFill(n);
  85.            int[] B = A.clone();
  86.  
  87.            long startTime1 = System.nanoTime();
  88.            quickSort(A,0,B.length-1);
  89.            long endTime1 = System.nanoTime();
  90.  
  91.            long startTime2 = System.nanoTime();
  92.            quickSort2(A,0,A.length-1);
  93.            long endTime2 = System.nanoTime();
  94.  
  95.            double totalTime1 = (endTime1-startTime1)/100000000.0;
  96.            double totalTime2 = (endTime2 - startTime2)/100000000.0;
  97.            System.out.format("%17d %19f %19f\n",n,totalTime1,totalTime2);
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement