def _sort(a, lo, hi, d): if lo >= hi: return lt = lo i = lo gt = hi c = String3WayQuickSort.char_at(a[lo], d) while i < gt: cmp = c - String3WayQuickSort.char_at(a[i], d) if cmp > 0: util.exchange(a, i, lt) i += 1 lt += 1 elif cmp < 0: util.exchange(a, i, gt) gt -= 1 else: i += 1 String3WayQuickSort._sort(a, lo, lt - 1, d) if c >= 0: String3WayQuickSort._sort(a, lt, gt, d + 1) String3WayQuickSort._sort(a, gt + 1, hi, d)
def sort(a, lo=None, hi=None): if lo is None: lo = 0 if hi is None: hi = len(a) - 1 if lo >= hi: return if hi - lo + 1 < ThreeWayQuickSort.CUTOFF: InsertionSort.sort(a, lo, hi) return i = lo lt = lo gt = hi while i <= gt: if util.less(a[i], a[lo]): util.exchange(a, i, lt) i += 1 lt += 1 elif util.less(a[lo], a[i]): util.exchange(a, i, gt) gt -= 1 else: i += 1 ThreeWayQuickSort.sort(a, lo, lt - 1) ThreeWayQuickSort.sort(a, gt + 1, hi)
def swim(self, k): while k > 1: parent = k // 2 if less(self.pq[k], self.pq[parent]): exchange(self.pq, k, parent) k = parent else: break
def sort(a): N = len(a) for i in range(N): k = i for j in range(i + 1, N): if util.less(a[j], a[k]): k = j util.exchange(a, i, k)
def del_min(self): index = self.pq[1] exchange(self.pq, 1, self.N) self.qp[self.pq[1]] = 1 self.qp[self.pq[self.N]] = self.N self.N -= 1 self.sink(1) return index
def swim(self, k): while k > 1: parent = k // 2 if less(self.keys[self.pq[k]], self.keys[self.pq[parent]]): exchange(self.pq, k, parent) self.qp[self.pq[k]] = k self.qp[self.pq[parent]] = parent k = parent else: break
def sink(a, k, n): while k * 2 <= n: child = 2 * k if child < n and util.greater(a[HeapSort.index(child+1)], a[HeapSort.index(child)]): child = child + 1 if util.greater(a[HeapSort.index(child)], a[HeapSort.index(k)]): util.exchange(a, HeapSort.index(child), HeapSort.index(k)) k = child else: break
def sink(tmp, k, n): while k * 2 <= n: child = k * 2 if child < n and less(tmp[child + 1], tmp[child]): child = child + 1 if less(tmp[child], tmp[k]): exchange(tmp, child, k) k = child else: break
def sort(a): n = len(a) print(a) for k in range(n // 2, 0, -1): HeapSort.sink(a, k, n) while n > 1: util.exchange(a, HeapSort.index(1), HeapSort.index(n)) n -= 1 HeapSort.sink(a, 1, n)
def sort(a, lo=None, hi=None): if lo is None: lo = 0 if hi is None: hi = len(a) - 1 for i in range(lo, hi + 1): for j in reversed(range(1, i + 1)): if util.less(a[j], a[j - 1]): util.exchange(a, j, j - 1) else: break
def sink(self, k): while 2 * k <= self.N: child = 2 * k if child < self.N and less(self.keys[self.pq[child]], self.keys[self.pq[child+1]]): child = child+1 if less(self.keys[self.pq[child]], self.keys[self.pq[k]]): exchange(self.pq, k, child) self.qp[self.pq[k]] = k self.qp[self.pq[child]] = child k = child else: break
def iterate(self): tmp = [0] * (self.N + 1) for k in range(self.N + 1): tmp[k] = self.pq[k] n = self.N while n >= 1: key = tmp[1] exchange(tmp, 1, n) n -= 1 self.sink(tmp, 1, n) yield key
def del_min(self): if self.is_empty(): return None key = self.pq[1] exchange(self.pq, 1, self.N) self.N -= 1 if self.N == len(self.pq) // 4: self.resize(len(self.pq) // 2) self.sink(self.pq, 1, self.N) return key
def sort(a): N = len(a) H = 0 while H < N // 3: H = 3 * H + 1 for h in reversed(range(1, H)): for i in range(h, N): for j in range(i, h - 1, -h): if util.less(a[j], a[j - h]): util.exchange(a, j, j - h) else: break
def partition(a, lo, hi): i = lo j = hi while True: while not util.less(a[lo], a[i]): i += 1 if i >= hi: break while util.less(a[lo], a[j]): j -= 1 if j <= lo: break if i >= j: break util.exchange(a, i, j) util.exchange(a, lo, j) return j
def test_exchange(self): a = [2, 4, 5] exchange(a, 0, 1) self.assertEqual(4, a[0]) self.assertEqual(2, a[1])
def shuffle(a): for i in range(1, len(a)): r = randint(0, i) exchange(a, r, i)