Advertisement
Aldin-SXR

algorithm comparison main()

Mar 25th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. public static void main(String[] args) {
  2.     int[] elements1 = new int[100000];
  3.     Random random = new Random();
  4.        
  5.     /* Generate random elements */
  6.     for (int i = 0; i < elements1.length; i++) {
  7.         elements1[i] = random.nextInt(1000);
  8.     }
  9.    
  10.     /* Create "clones" of the original array, for same sorting conditions */
  11.     int[] elements2 = elements1.clone();
  12.     int[] elements3 = elements1.clone();
  13.     int[] elements4 = elements1.clone();
  14.    
  15.     /* Bubble sort */
  16.     long start = System.currentTimeMillis();
  17.     BubbleSort.sort(elements1);
  18.     System.out.println("bubble sort:\t" + (System.currentTimeMillis() - start) + " ms");
  19.    
  20.     /* Selection sort */
  21.     start = System.currentTimeMillis();
  22.     SelectionSort.sort(elements2);
  23.     System.out.println("selection sort:\t" + (System.currentTimeMillis() - start) + " ms");
  24.    
  25.     /* Insertion sort */
  26.     start = System.currentTimeMillis();
  27.     InsertionSort.sort(elements3);
  28.     System.out.println("insertion sort:\t" + (System.currentTimeMillis() - start) + " ms");
  29.    
  30.     /* Shell sort */
  31.     start = System.currentTimeMillis();
  32.     ShellSort.sort(elements4);
  33.     System.out.println("Shell sort:\t" + (System.currentTimeMillis() - start) + " ms");
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement