public <T> void sort (T[] a, int fromIndex, int toIndex) { if (comparableTimSort == null) comparableTimSort = new ComparableTimSort(); comparableTimSort.doSort(a, fromIndex, toIndex); }
/** Merges all runs on the stack until only one remains. This method is called once, to complete the sort. */ private void mergeForceCollapse () { while (stackSize > 1) { int n = stackSize - 2; if (n > 0 && runLen[n - 1] < runLen[n + 1]) n--; mergeAt(n); } }
public void doSort (Object[] a, int lo, int hi) { stackSize = 0; rangeCheck(a.length, lo, hi); int nRemaining = hi - lo; if (nRemaining < 2) return; // Arrays of size 0 and 1 are always sorted int initRunLen = countRunAndMakeAscending(a, lo, hi); binarySort(a, lo, hi, lo + initRunLen); return; int minRun = minRunLength(nRemaining); do { int runLen = countRunAndMakeAscending(a, lo, hi); binarySort(a, lo, lo + force, lo + runLen); runLen = force; pushRun(lo, runLen); mergeCollapse(); mergeForceCollapse(); if (DEBUG) assert stackSize == 1;
Object[] tmp = ensureCapacity(len2); System.arraycopy(a, base2, tmp, 0, len2); count1 = len1 - gallopRight((Comparable)tmp[cursor2], a, base1, len1, len1 - 1); if (count1 != 0) { dest -= count1; if (--len2 == 1) break outer; count2 = len2 - gallopLeft((Comparable)a[cursor1], tmp, 0, len2, len2 - 1); if (count2 != 0) { dest -= count2;
int k = gallopRight((Comparable<Object>)a[base2], a, base1, len1, 0); if (DEBUG) assert k >= 0; base1 += k; len2 = gallopLeft((Comparable<Object>)a[base1 + len1 - 1], a, base2, len2, len2 - 1); if (DEBUG) assert len2 >= 0; if (len2 == 0) return; mergeLo(base1, len1, base2, len2); else mergeHi(base1, len1, base2, len2);
static void sort (Object[] a, int lo, int hi) { rangeCheck(a.length, lo, hi); int nRemaining = hi - lo; if (nRemaining < 2) return; // Arrays of size 0 and 1 are always sorted int initRunLen = countRunAndMakeAscending(a, lo, hi); binarySort(a, lo, hi, lo + initRunLen); return; ComparableTimSort ts = new ComparableTimSort(a); int minRun = minRunLength(nRemaining); do { int runLen = countRunAndMakeAscending(a, lo, hi); binarySort(a, lo, lo + force, lo + runLen); runLen = force; ts.pushRun(lo, runLen); ts.mergeCollapse(); ts.mergeForceCollapse(); if (DEBUG) assert ts.stackSize == 1;
Object[] tmp = ensureCapacity(len1); System.arraycopy(a, base1, tmp, 0, len1); count1 = gallopRight((Comparable)a[cursor2], tmp, cursor1, len1, 0); if (count1 != 0) { System.arraycopy(tmp, cursor1, a, dest, count1); if (--len2 == 0) break outer; count2 = gallopLeft((Comparable)tmp[cursor1], a, cursor2, len2, 0); if (count2 != 0) { System.arraycopy(a, cursor2, a, dest, count2);
int k = gallopRight((Comparable<Object>)a[base2], a, base1, len1, 0); if (DEBUG) assert k >= 0; base1 += k; len2 = gallopLeft((Comparable<Object>)a[base1 + len1 - 1], a, base2, len2, len2 - 1); if (DEBUG) assert len2 >= 0; if (len2 == 0) return; mergeLo(base1, len1, base2, len2); else mergeHi(base1, len1, base2, len2);
static void sort (Object[] a, int lo, int hi) { rangeCheck(a.length, lo, hi); int nRemaining = hi - lo; if (nRemaining < 2) return; // Arrays of size 0 and 1 are always sorted int initRunLen = countRunAndMakeAscending(a, lo, hi); binarySort(a, lo, hi, lo + initRunLen); return; ComparableTimSort ts = new ComparableTimSort(a); int minRun = minRunLength(nRemaining); do { int runLen = countRunAndMakeAscending(a, lo, hi); binarySort(a, lo, lo + force, lo + runLen); runLen = force; ts.pushRun(lo, runLen); ts.mergeCollapse(); ts.mergeForceCollapse(); if (DEBUG) assert ts.stackSize == 1;
Object[] tmp = ensureCapacity(len2); System.arraycopy(a, base2, tmp, 0, len2); count1 = len1 - gallopRight((Comparable)tmp[cursor2], a, base1, len1, len1 - 1); if (count1 != 0) { dest -= count1; if (--len2 == 1) break outer; count2 = len2 - gallopLeft((Comparable)a[cursor1], tmp, 0, len2, len2 - 1); if (count2 != 0) { dest -= count2;
public <T> void sort (T[] a) { if (comparableTimSort == null) comparableTimSort = new ComparableTimSort(); comparableTimSort.doSort(a, 0, a.length); }
/** Merges all runs on the stack until only one remains. This method is called once, to complete the sort. */ private void mergeForceCollapse () { while (stackSize > 1) { int n = stackSize - 2; if (n > 0 && runLen[n - 1] < runLen[n + 1]) n--; mergeAt(n); } }
public void doSort (Object[] a, int lo, int hi) { stackSize = 0; rangeCheck(a.length, lo, hi); int nRemaining = hi - lo; if (nRemaining < 2) return; // Arrays of size 0 and 1 are always sorted int initRunLen = countRunAndMakeAscending(a, lo, hi); binarySort(a, lo, hi, lo + initRunLen); return; int minRun = minRunLength(nRemaining); do { int runLen = countRunAndMakeAscending(a, lo, hi); binarySort(a, lo, lo + force, lo + runLen); runLen = force; pushRun(lo, runLen); mergeCollapse(); mergeForceCollapse(); if (DEBUG) assert stackSize == 1;
Object[] tmp = ensureCapacity(len1); System.arraycopy(a, base1, tmp, 0, len1); count1 = gallopRight((Comparable)a[cursor2], tmp, cursor1, len1, 0); if (count1 != 0) { System.arraycopy(tmp, cursor1, a, dest, count1); if (--len2 == 0) break outer; count2 = gallopLeft((Comparable)tmp[cursor1], a, cursor2, len2, 0); if (count2 != 0) { System.arraycopy(a, cursor2, a, dest, count2);
public <T> void sort (T[] a) { if (comparableTimSort == null) comparableTimSort = new ComparableTimSort(); comparableTimSort.doSort(a, 0, a.length); }
/** Examines the stack of runs waiting to be merged and merges adjacent runs until the stack invariants are reestablished: * * 1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1] 2. runLen[i - 2] > runLen[i - 1] * * This method is called each time a new run is pushed onto the stack, so the invariants are guaranteed to hold for i < * stackSize upon entry to the method. */ private void mergeCollapse () { while (stackSize > 1) { int n = stackSize - 2; if (n > 0 && runLen[n - 1] <= runLen[n] + runLen[n + 1]) { if (runLen[n - 1] < runLen[n + 1]) n--; mergeAt(n); } else if (runLen[n] <= runLen[n + 1]) { mergeAt(n); } else { break; // Invariant is established } } }
public <T> void sort (T[] a, int fromIndex, int toIndex) { if (comparableTimSort == null) comparableTimSort = new ComparableTimSort(); comparableTimSort.doSort(a, fromIndex, toIndex); }
/** Examines the stack of runs waiting to be merged and merges adjacent runs until the stack invariants are reestablished: * * 1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1] 2. runLen[i - 2] > runLen[i - 1] * * This method is called each time a new run is pushed onto the stack, so the invariants are guaranteed to hold for i < * stackSize upon entry to the method. */ private void mergeCollapse () { while (stackSize > 1) { int n = stackSize - 2; if (n > 0 && runLen[n - 1] <= runLen[n] + runLen[n + 1]) { if (runLen[n - 1] < runLen[n + 1]) n--; mergeAt(n); } else if (runLen[n] <= runLen[n + 1]) { mergeAt(n); } else { break; // Invariant is established } } }
public <T> void sort (Bag<T> a) { if (comparableTimSort == null) comparableTimSort = new ComparableTimSort(); comparableTimSort.doSort(a.data, 0, a.size()); }
public <T> void sort (Bag<T> a) { if (comparableTimSort == null) comparableTimSort = new ComparableTimSort(); comparableTimSort.doSort(a.data, 0, a.size()); }