Created
April 11, 2014 07:27
-
-
Save fsarradin/10446720 to your computer and use it in GitHub Desktop.
Sort algo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Ignore; | |
import org.junit.Test; | |
import java.util.*; | |
public class SortTest { | |
@Test | |
public void testCollectionsSort() { | |
List<Integer> values = getValues(); | |
long start = System.nanoTime(); | |
Collections.sort(values); | |
long end = System.nanoTime(); | |
long time = end - start; | |
System.out.println("time: " + time / 1e9); | |
} | |
@Ignore | |
public void testSortedSet() { | |
List<Integer> values = getValues(); | |
long start = System.nanoTime(); | |
TreeSet<Integer> sorted = new TreeSet<Integer>(); | |
for (Integer value : values) { | |
sorted.add(value); | |
} | |
long end = System.nanoTime(); | |
long time = end - start; | |
System.out.println("time: " + time / 1e9); | |
} | |
@Test | |
public void testInsertion() { | |
List<Integer> values = getValues(); | |
long start = System.nanoTime(); | |
insert(values); | |
long end = System.nanoTime(); | |
long time = end - start; | |
System.out.println("time: " + time / 1e9); | |
} | |
private <T extends Comparable<? super T>> void insert(List<T> values) { | |
Object[] objects = values.toArray(); | |
insert(objects); | |
ListIterator<T> i = values.listIterator(); | |
for (int j = 0; j < objects.length; j++) { | |
i.next(); | |
i.set((T) objects[j]); | |
} | |
} | |
private void insert(Object[] values) { | |
for (int i = 1; i < values.length; i++) { | |
Comparable x = ((Comparable) values[i]); | |
int j = i; | |
while (j > 0 && ((Comparable) values[j - 1]).compareTo(x) > 0) { | |
values[j] = values[j - 1]; | |
j--; | |
} | |
values[j] = x; | |
} | |
} | |
private List<Integer> getValues() { | |
List<Integer> values = new ArrayList<Integer>(); | |
for (int i = 0; i < 10000000; i++) { | |
values.add(i); | |
} | |
return values; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment