Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class SortTest {
- static class Node {
- int data;
- Node(int d) { data = d; }
- }
- static Comparator<Node> comp = new Comparator<Node>() {
- public int compare(Node n, Node o) {
- return n.data == o.data ? 0 :
- n.data > o.data ? 1 : -1;
- }
- public boolean equals(Object o) {
- return o == this;
- }
- };
- static Random random = new Random();
- static Node[] randomArray(int len) {
- Node[] arr = new Node[len];
- for(int i = 0; i < arr.length; i++) arr[i] = new Node(random.nextInt());
- return arr;
- }
- public static void main(String[] args) {
- Node[] def1 = randomArray(100000);
- Node[] def2 = randomArray(200000);
- Node[] def3 = randomArray(400000);
- Node[] def4 = randomArray(600000);
- Node[] def5 = randomArray(1000000);
- Node[] tim1 = Arrays.copyOf(def1,def1.length);
- Node[] tim2 = Arrays.copyOf(def2,def2.length);
- Node[] tim3 = Arrays.copyOf(def3,def3.length);
- Node[] tim4 = Arrays.copyOf(def4,def4.length);
- Node[] tim5 = Arrays.copyOf(def5,def5.length);
- // default sort time
- long then = System.currentTimeMillis();
- Arrays.sort(def1,comp);
- Arrays.sort(def2,comp);
- Arrays.sort(def3,comp);
- Arrays.sort(def4,comp);
- Arrays.sort(def5,comp);
- long now = System.currentTimeMillis();
- System.out.println("Time for default: " + (now - then) + "ms");
- then = System.currentTimeMillis();
- TimSort.sort(tim1,comp);
- TimSort.sort(tim2,comp);
- TimSort.sort(tim3,comp);
- TimSort.sort(tim4,comp);
- TimSort.sort(tim5,comp);
- now = System.currentTimeMillis();
- System.out.println("Time for timsort: " + (now - then) + "ms");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement