package com.logntw.eva.sort; import java.util.Arrays; import java.util.Comparator; import junit.framework.Assert; import org.junit.Test; public class PartialSortExample { private final static Integer[] DATA = new Integer[] { 71227,98228,54930,20714,89524, 90584,60890,81227,79199,77893, 56307,87751,64345,89181,39535, 51032,20430,35697,23129,60004, 40419,98562,91508,78504,59604, 72939,86801,65671,56251,82943, 33783,38073,18636,75911,73376, 40635,80909,83294,58487,20199, 68609,38687,90465,89168,19415, 95444,64276,51914,96299,78463, 95009,94056,23396,75571,76706, 29050,34859,11851,49165,86506, 18806,27162,71554,51170,70173, 42112,44986,12791,98933,35510, 27661,22880,57217,24343,70518, 85539,78964,55031,50893,43795, 95641,88476,30036,47695,40973, 25722,17840,74927,64111,52049, 88703,35316,89837,33919,89528, 70018,48226,29540,33903,58420, 77071,45227,52765,65037,20888, 79850,53348,24363,16738,63649, 76694,29383,81690,84967,96859, 27705,84301,87945,12060,47373, 49225,42661,67741,64066,12896, 98098,34418,79970,61311,26290, 52478,76837,47091,95370,67066, 13412,31257,21760,82920,60766, 24911,79944,98189,36267,45435, 57317,13535,84409,40588,96614, 29836,83516,71748,16393,83473, 67991,88874,48432,54000,85564, 84935,73989,21322,68652,53231, 40504,57056,92368,49565,73122, 74164,16492,98090,81849,44641, 61477,71403,58254,85632,35867, 24626,24565,56121,29307,77037, 52529,71192,31294,36075,65275, 90626,73677,95255,55426,41171, 12059,92218,35684,43169,80471, }; private final static Comparator COMPARATOR = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1.compareTo(o2); } }; @Test public void testSortHelper2() throws Exception { Integer[] data1 = DATA.clone(); Arrays.sort(data1, COMPARATOR); final Integer[] data2 = DATA.clone(); SortHelper.partialSort(data2, COMPARATOR, 0, 16); int len = data1.length; for (int i = 0; i < 16; i++) Assert.assertEquals(data1[i], data2[i]); test: { for (int i = 16; i < len; i++) if (data1[i] != data2[i]) break test; Assert.assertTrue(false); } } }