/// <exception cref="System.Exception"/> public virtual void SortAllEqual(IndexedSorter sorter) { int Sample = 500; int[] values = new int[Sample]; Arrays.Fill(values, 10); TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(values); sorter.Sort(s, 0, Sample); int[] check = s.GetSorted(); Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays .ToString(check), Arrays.Equals(values, check)); // Set random min/max, re-sort. Random r = new Random(); int min = r.Next(Sample); int max = (min + 1 + r.Next(Sample - 2)) % Sample; values[min] = 9; values[max] = 11; System.Console.Out.WriteLine("testAllEqual setting min/max at " + min + "/" + max + "(" + sorter.GetType().FullName + ")"); s = new TestIndexedSort.SampleSortable(values); sorter.Sort(s, 0, Sample); check = s.GetSorted(); Arrays.Sort(values); Assert.True(check[0] == 9); Assert.True(check[Sample - 1] == 11); Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays .ToString(check), Arrays.Equals(values, check)); }
/// <exception cref="System.Exception"/> public virtual void SortSingleRecord(IndexedSorter sorter) { int Sample = 1; TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(Sample); int[] values = s.GetValues(); sorter.Sort(s, 0, Sample); int[] check = s.GetSorted(); Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays .ToString(check), Arrays.Equals(values, check)); }
/// <exception cref="System.Exception"/> public virtual void SortSequential(IndexedSorter sorter) { int Sample = 500; int[] values = new int[Sample]; for (int i = 0; i < Sample; ++i) { values[i] = i; } TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(values); sorter.Sort(s, 0, Sample); int[] check = s.GetSorted(); Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays .ToString(check), Arrays.Equals(values, check)); }
/// <exception cref="System.Exception"/> public virtual void SortRandom(IndexedSorter sorter) { int Sample = 256 * 1024; TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(Sample); long seed = s.GetSeed(); System.Console.Out.WriteLine("sortRandom seed: " + seed + "(" + sorter.GetType(). FullName + ")"); int[] values = s.GetValues(); Arrays.Sort(values); sorter.Sort(s, 0, Sample); int[] check = s.GetSorted(); Assert.True("seed: " + seed + "\ndoesn't match\n", Arrays.Equals (values, check)); }
/// <exception cref="System.Exception"/> public virtual void SortSorted(IndexedSorter sorter) { int Sample = 500; int[] values = new int[Sample]; Random r = new Random(); long seed = r.NextLong(); r.SetSeed(seed); System.Console.Out.WriteLine("testSorted seed: " + seed + "(" + sorter.GetType(). FullName + ")"); for (int i = 0; i < Sample; ++i) { values[i] = r.Next(100); } Arrays.Sort(values); TestIndexedSort.SampleSortable s = new TestIndexedSort.SampleSortable(values); sorter.Sort(s, 0, Sample); int[] check = s.GetSorted(); Assert.True(Arrays.ToString(values) + "\ndoesn't match\n" + Arrays .ToString(check), Arrays.Equals(values, check)); }
private static void SortInternal(IndexedSortable s, int p, int r, Progressable rep , int depth) { if (null != rep) { rep.Progress(); } while (true) { if (r - p < 13) { for (int i = p; i < r; ++i) { for (int j = i; j > p && s.Compare(j - 1, j) > 0; --j) { s.Swap(j, j - 1); } } return; } if (--depth < 0) { // give up alt.Sort(s, p, r, rep); return; } // select, move pivot into first position Fix(s, (int)(((uint)(p + r)) >> 1), p); Fix(s, (int)(((uint)(p + r)) >> 1), r - 1); Fix(s, p, r - 1); // Divide int i_1 = p; int j_1 = r; int ll = p; int rr = r; int cr; while (true) { while (++i_1 < j_1) { if ((cr = s.Compare(i_1, p)) > 0) { break; } if (0 == cr && ++ll != i_1) { s.Swap(ll, i_1); } } while (--j_1 > i_1) { if ((cr = s.Compare(p, j_1)) > 0) { break; } if (0 == cr && --rr != j_1) { s.Swap(rr, j_1); } } if (i_1 < j_1) { s.Swap(i_1, j_1); } else { break; } } j_1 = i_1; // swap pivot- and all eq values- into position while (ll >= p) { s.Swap(ll--, --i_1); } while (rr < r) { s.Swap(rr++, j_1++); } // Conquer // Recurse on smaller interval first to keep stack shallow System.Diagnostics.Debug.Assert(i_1 != j_1); if (i_1 - p < r - j_1) { SortInternal(s, p, i_1, rep, depth); p = j_1; } else { SortInternal(s, j_1, r, rep, depth); r = i_1; } } }