// Quicksort, loosely following Bentley and McIlroy,
// ``Engineering a Sort Function,'' SP&E November 1993.
// medianOfThree moves the median of the three values data[m0], data[m1], data[m2] into data[m1].
2374
func medianOfThree(data Interface, m1, m0, m2 int) {
	// sort 3 elements
	if data.Less(m1, m0) {
		data.Swap(m1, m0)
	}
	// data[m0] <= data[m1]
	if data.Less(m2, m1) {
		data.Swap(m2, m1)
		// data[m0] <= data[m2] && data[m1] < data[m2]
		if data.Less(m1, m0) {
			data.Swap(m1, m0)
		}
	}
	// now data[m0] <= data[m1] <= data[m2]
}
57155
func MedianAmount(s []dcrutil.Amount) dcrutil.Amount {
	if len(s) == 0 {
		return 0
	}

	sort.Sort(dcrutil.AmountSorter(s))

	middle := len(s) / 2

	if len(s) == 0 {
		return 0
	} else if (len(s) % 2) != 0 {
		return s[middle]
	}
	return (s[middle] + s[middle-1]) / 2
}
681217
func (s *ScriptSort) NestedSort(nestedSort *NestedSort) *ScriptSort {
	s.nestedSort = nestedSort
	return s
}
1122303
func average(name string, fun func(), benchSize int) {
	numThreads := runtime.GOMAXPROCS(-1)
	wp := new(sync.WaitGroup)
	iter := float64(benchSize)
	start := float64(time.Now().UTC().UnixNano())
	// Run function across multiple goroutines
	for i := 0; i < benchSize; i += benchSize / numThreads {
		wp.Add(1)
		go func() {
			defer wp.Done()
			for j := 0; j < benchSize/numThreads; j++ {
				fun()
			}
		}()
	}
	wp.Wait()
	end := float64(time.Now().UTC().UnixNano())
	fmt.Printf("%s %d: %d ns/iter, %d iter/sec\n", name, int(benchSize), int((end-start)/iter), int(1000000000/((end-start)/iter)))
}
464365
func SortDedup(a [][]byte) [][]byte {
	if len(a) < 2 {
		return a
	}

	Sort(a)

	i, j := 0, 1
	for j < len(a) {
		if !bytes.Equal(a[j-1], a[j]) {
			a[i] = a[j-1]
			i++
		}
		j++
	}
	a[i] = a[j-1]
	i++
	return a[:i]
}
491645
func LazySort(inst FixedDataGrid, direction SortDirection, attrs []AttributeSpec) (FixedDataGrid, error) {
	// Run the sort operation
	sortInstructions := createSortSpec(inst, attrs)

	// Build the row -> row mapping
	_, rows := inst.Size()      // Get the total row count
	rowArr := make([]int, rows) // Create an array of positions
	for i := 0; i < len(rowArr); i++ {
		rowArr[i] = i
	}
	for i := range sortInstructions {
		r1 := rowArr[sortInstructions[i].r1]
		r2 := rowArr[sortInstructions[i].r2]
		// Swap
		rowArr[sortInstructions[i].r1] = r2
		rowArr[sortInstructions[i].r2] = r1
	}
	if direction == Descending {
		for i, j := 0, rows-1; i < j; i, j = i+1, j-1 {
			tmp := rowArr[i]
			rowArr[i] = rowArr[j]
			rowArr[j] = tmp
		}
	}
	// Create a mapping dictionary
	rowMap := make(map[int]int)
	for i, a := range rowArr {
		if i == a {
			continue
		}
		rowMap[i] = a
	}
	// Create the return structure
	ret := NewInstancesViewFromRows(inst, rowMap)
	return ret, nil
}
28398
func (b *Sortable) Comparator(k string, less StringComparator) SortComparator {
	return func(i, j int) bool {
		return less(b.Data[i].Get(k), b.Data[j].Get(k))
	}
}
216946
func (c *TasksListCall) Sort(sort string) *TasksListCall {
	c.urlParams_.Set("sort", sort)
	return c
}
1063835
func MedianOfMedians(list SortSlicer) int {
	n := list.Len() / 5
	for i := 0; i < n; i++ {
		left := i * 5
		sub := list.Slice(left, min(left+5, list.Len()-1))
		Select(sub, 2)
		list.Swap(i, left+2)
	}
	Select(list.Slice(0, min(n, list.Len()-1)), min(list.Len(), n/2))
	return n / 2
}
566422
func NewSortedSlice(opts ...int) (ss SortedSlice) {
	l, c := 0, 0
	if len(opts) >= 1 {
		l = opts[0]
	}
	if len(opts) >= 2 {
		c = opts[1]
	}
	ss.s = make([]Sortable, l, c)
	return
}
808539
func Sort(infos []Info, newer func(a, b *Info) bool) {
	if newer == nil {
		newer = NewerByCreated
	}
	sort.Sort(&infoSort{infos: infos, newer: newer})
}
1230266
func sortInterfaceSlice(in []interface{}) []interface{} {
	a := []string{}
	b := []interface{}{}
	for _, v := range in {
		a = append(a, v.(string))
	}

	sort.Strings(a)

	for _, v := range a {
		b = append(b, v)
	}

	return b
}
473736
func Sort(data interface{}, identifier ...func(interface{}) int) {
	val := reflect.ValueOf(data)
	identifier = append(identifier, DefaultID)
	sortable := byInt{
		Data:       val,
		Indices:    make([]int, val.Len()),
		Identifier: identifier[0],
	}
	for i := 0; i < val.Len(); i++ {
		sortable.Indices[i] = i
	}
	sort.Sort(sortable)
	return
}
1004882
func (l sortEntries) qSort() {
	if len(l) > 1 {
		p := partition(l)
		l[:p].qSort()
		l[p+1:].qSort()
	}
}
145811
func IntroSort(data sort.Interface) {
	if data.Len() <= 1 {
		return
	}
	newListIntroSorter(data).Sort(0, data.Len())
}
935821
func insertionSort_func(data lessSwap, a, b int) {
	for i := a + 1; i < b; i++ {
		for j := i; j > a && data.Less(j, j-1); j-- {
			data.Swap(j, j-1)
		}
	}
}
1283687
func newTimSorter(arr sort.Interface, maxTempSlots int) *TimSorter {
	return &TimSorter{
		Sorter:       newSorter(arr),
		runEnds:      make([]int, 1+STACKSIZE),
		maxTempSlots: maxTempSlots,
	}
}