gpt4 book ai didi

sorting - 如何按值对 map[int]int 进行排序?

转载 作者:数据小太阳 更新时间:2023-10-29 03:41:04 25 4
gpt4 key购买 nike

<分区>

我的插入和快速排序不适用于 map[uint64]uint64 值。谁能帮忙?提前致谢。想要按值排序 map “aint”。

如有详细请追问。我会改进这个问题。再次感谢。

package main

import (
"sort"
"fmt"
"time"
"runtime"
"math/rand"
)
func main() {

runtime.GOMAXPROCS(runtime.NumCPU())
start := time.Now()

//the map variable
aint := map[uint64]uint64{}

start = time.Now()
for i := uint64(0); i < 100000000; i++ {
aint[i+32132112313] = uint64(rand.Intn(13123123123)+2312423213) //random generation of input data
// aint = insertSort(aint,uint64(rand.Intn(13123123123)))
}
fmt.Printf("%d\n", aint[22] )

elapsed := time.Since(start)
fmt.Printf("Entry took %s %d\n", elapsed)

start = time.Now()
quicksort(aint)

// sort.Sort(sort.IntSlice(aint))

elapsed = time.Since(start)
fmt.Printf("Sorting took %s %d\n", elapsed)
}
func insertionsort(items []int) {
var n = len(items)
for i := 1; i < n; i++ {
j := i
for j > 0 {
if items[j-1] > items[j] {
items[j-1], items[j] = items[j], items[j-1]
}
j = j - 1
}
}
}


func quicksort(a map[uint64]uint64) map[uint64]uint64 {
if len(a) < 2 {
return a
}

left, right := uint64(0), uint64(len(a)-1)

pivot := Uint64() % uint64(len(a))

a[pivot], a[right] = a[right], a[pivot]
// does not work anymore from here onwards.
for uint64(i), _ := range a {
if a[i] < a[right] {
a[left], a[i] = a[i], a[left]
left++
}
}

a[left], a[right] = a[right], a[left]

quicksort(a[:left])
quicksort(a[left+1:])

return a
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com