feat(Go-Tool):新增lomute划分算法效率单测,结果:多次查找,元素多的情况下由末尾开始查找效率更高
This commit is contained in:
@@ -51,7 +51,7 @@ func (bitStore *BitStore) IsGearReceive(gear int) bool {
|
||||
return false
|
||||
}
|
||||
position := bitStore.getGearPosition(gear, length)
|
||||
return (bitStore.GearPickList[length-1] & (2 << position)) > 0
|
||||
return (bitStore.GearPickList[length-1] & (1 << position)) > 0
|
||||
}
|
||||
|
||||
//获取对应档位奖励
|
||||
@@ -59,7 +59,7 @@ func (bitStore *BitStore) ReceiveByGear(gear int) ([]int, map[int]bool) {
|
||||
bitStore.checkIfReceive(gear) //校验是否被领取了
|
||||
length := bitStore.dynamicGrowth(gear) //数组进行动态增长
|
||||
position := bitStore.getGearPosition(gear, length) //获取档位在数组中的位数位置
|
||||
bitStore.GearPickList[length-1] = bitStore.GearPickList[length-1] + (2 << uint(position))
|
||||
bitStore.GearPickList[length-1] = bitStore.GearPickList[length-1] + (1 << uint(position))
|
||||
return bitStore.GearPickList, bitStore.FindAllGearMap()
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func (bitStore *BitStore) fullListDeal(listSize int, length int) map[int]bool {
|
||||
resultMap := make(map[int]bool, 0)
|
||||
for i := 0; i < length; i++ {
|
||||
for j := 0; j < bit && bitStore.MaxGear > i*bit+j; j++ {
|
||||
resultMap[i*bit+j+1] = (bitStore.GearPickList[i] & (2 << uint(j))) > 0
|
||||
resultMap[i*bit+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0
|
||||
}
|
||||
}
|
||||
return resultMap
|
||||
@@ -101,7 +101,7 @@ func (bitStore *BitStore) shortListDeal(listSize, length int) map[int]bool {
|
||||
resultMap := make(map[int]bool, 0)
|
||||
for i := 0; i < listSize; i++ {
|
||||
for j := 0; j < bit; j++ {
|
||||
resultMap[i*bit+j+1] = (bitStore.GearPickList[i] & (2 << uint(j))) > 0
|
||||
resultMap[i*bit+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0
|
||||
}
|
||||
}
|
||||
for i := listSize; i < length; i++ {
|
||||
|
||||
@@ -6,6 +6,11 @@
|
||||
|
||||
package search
|
||||
|
||||
var (
|
||||
times = 0 //用来计算一次划分进行了多少次的比较操作
|
||||
charge = 0 //实际进行元素交换的次数
|
||||
)
|
||||
|
||||
//参数说明:
|
||||
//list :数组
|
||||
//start : 开始比较的数字下标
|
||||
@@ -17,19 +22,25 @@ func LomutoPartition(list []int, start int, end int) int {
|
||||
for i := start + 1; i <= end; i++ {
|
||||
if list[i] < compareValue {
|
||||
moveSubscript++
|
||||
charge++
|
||||
list[moveSubscript], list[i] = list[i], list[moveSubscript]
|
||||
}
|
||||
}
|
||||
charge++
|
||||
list[moveSubscript], list[start] = list[start], list[moveSubscript] //交换元素
|
||||
return moveSubscript
|
||||
}
|
||||
|
||||
func LomutoQuiteSelect(list []int, pointPosition int) int {
|
||||
func LomutoQuiteSelect(list []int, pointPosition int) (int, int, int) {
|
||||
positionCheck(pointPosition, len(list)) //下标校验
|
||||
times = 0
|
||||
charge = 0
|
||||
|
||||
nowPosition := LomutoPartition(list, 0, len(list)-1)
|
||||
times++
|
||||
//循环进行lomuto划分处理,知道找到我们需要的那个位置的元素
|
||||
for nowPosition != pointPosition-1 {
|
||||
times++
|
||||
if nowPosition > pointPosition-1 {
|
||||
nowPosition = LomutoPartition(list, 0, nowPosition)
|
||||
} else {
|
||||
@@ -37,7 +48,7 @@ func LomutoQuiteSelect(list []int, pointPosition int) int {
|
||||
}
|
||||
}
|
||||
|
||||
return list[nowPosition]
|
||||
return list[nowPosition], times, charge
|
||||
}
|
||||
|
||||
func positionCheck(pointPosition int, len int) {
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user