Advertisement
Guest User

SortTest

a guest
May 2nd, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import java.util.*;
  2. public class SortTest {
  3.  
  4.     static class Node {
  5.  
  6.         int data;
  7.  
  8.         Node(int d) { data = d; }
  9.  
  10.     }
  11.  
  12.  
  13.     static Comparator<Node> comp = new Comparator<Node>() {
  14.         public int compare(Node n, Node o) {
  15.             return n.data == o.data ? 0 :
  16.                    n.data >  o.data ? 1 : -1;
  17.         }
  18.         public boolean equals(Object o) {
  19.             return o == this;
  20.         }
  21.     };
  22.    
  23.     static Random random = new Random();
  24.  
  25.  
  26.     static Node[] randomArray(int len) {
  27.         Node[] arr = new Node[len];
  28.         for(int i = 0; i < arr.length; i++) arr[i] = new Node(random.nextInt());
  29.         return arr;
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.  
  34.         Node[] def1 = randomArray(100000);
  35.         Node[] def2 = randomArray(200000);
  36.         Node[] def3 = randomArray(400000);
  37.         Node[] def4 = randomArray(600000);
  38.         Node[] def5 = randomArray(1000000);
  39.  
  40.         Node[] tim1 = Arrays.copyOf(def1,def1.length);
  41.         Node[] tim2 = Arrays.copyOf(def2,def2.length);
  42.         Node[] tim3 = Arrays.copyOf(def3,def3.length);
  43.         Node[] tim4 = Arrays.copyOf(def4,def4.length);
  44.         Node[] tim5 = Arrays.copyOf(def5,def5.length);
  45.  
  46.         // default sort time
  47.         long then = System.currentTimeMillis();
  48.         Arrays.sort(def1,comp);
  49.         Arrays.sort(def2,comp);
  50.         Arrays.sort(def3,comp);
  51.         Arrays.sort(def4,comp);
  52.         Arrays.sort(def5,comp);
  53.         long now = System.currentTimeMillis();
  54.  
  55.         System.out.println("Time for default: " + (now - then) + "ms");
  56.  
  57.         then = System.currentTimeMillis();
  58.         TimSort.sort(tim1,comp);
  59.         TimSort.sort(tim2,comp);
  60.         TimSort.sort(tim3,comp);
  61.         TimSort.sort(tim4,comp);
  62.         TimSort.sort(tim5,comp);
  63.         now = System.currentTimeMillis();
  64.  
  65.         System.out.println("Time for timsort: " + (now - then) + "ms");
  66.  
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement