func siftDownByFreq(data []literalNode, lo, hi, first int) {
	root := lo
	for {
		child := 2*root + 1
		if child >= hi {
			break
		}
		if child+1 < hi && (data[first+child].freq == data[first+child+1].freq && data[first+child].literal < data[first+child+1].literal || data[first+child].freq < data[first+child+1].freq) {
			child++
		}
		if data[first+root].freq == data[first+child].freq && data[first+root].literal > data[first+child].literal || data[first+root].freq > data[first+child].freq {
			return
		}
		data[first+root], data[first+child] = data[first+child], data[first+root]
		root = child
	}
}
siftDownByFreq implements the heap property on data[lo, hi).
first is an offset into the array where the root of the heap lies.
package flate
func sortByFreq(data []literalNode) {
	n := len(data)
	quickSortByFreq(data, 0, n, maxDepth(n))
}
func quickSortByFreq(data []literalNode, a, b, maxDepth int) {
	for b-a > 12 {  
		if maxDepth == 0 {
			heapSort(data, a, b)
			return
		}
		maxDepth--
		mlo, mhi := doPivotByFreq(data, a, b)
		if mlo-a < b-mhi {
			quickSortByFreq(data, a, mlo, maxDepth)
			a = mhi  
		} else {
			quickSortByFreq(data, mhi, b, maxDepth)
			b = mlo  
		}
	}
	if b-a > 1 {
		for i := a + 6; i < b; i++ {
			if data[i].freq == data[i-6].freq && data[i].literal < data[i-6].literal || data[i].freq < data[i-6].freq {
				data[i], data[i-6] = data[i-6], data[i]
			}
		}
		insertionSortByFreq(data, a, b)
	}
}