当前位置: 首页 > news >正文

南昌有哪些企业网站北京专业建设

南昌有哪些企业网站,北京专业建设,深圳网站优化指导,建筑工程网课代字幕go语言sort.Sort(data Interface) 排序接口自定义排序实现,golang里面的sort包中的Sort方法底层使用的是 pdqsort的一个快速排序算法, 我们可以将要排序的对象实现Interface接口后直接丢个这个函数即可自动按照我们指定的方式进行数据快速排序。 sort函…

go语言sort.Sort(data Interface) 排序接口自定义排序实现,golang里面的sort包中的Sort方法底层使用的是 pdqsort的一个快速排序算法, 我们可以将要排序的对象实现Interface接口后直接丢个这个函数即可自动按照我们指定的方式进行数据快速排序。

sort函数原型和排序接口Interface说明

sort.Sort() 这里的参数是一个接口, 即我们只需要在我们自定义的类型里面实现这个Interface这个接口里面定义的Len Less Swap方法 然后将类型变量传进sor就会自动版我们进行排序.
函数原型:func Sort(data Interface)
Sort排序data。它调用1次data.Len确定长度,调用O(n*log(n))次data.Less和data.Swap。本函数不能保证排序的稳定性(即不保证相等元素的相对次序不变)。

排序接口
type Interface interface {
    // Len方法返回集合中的元素个数
    Len() int
    // Less方法报告索引i的元素是否比索引j的元素小
    Less(i, j int) bool
    // Swap方法交换索引i和j的两个元素
    Swap(i, j int)
}
一个满足sort.Interface接口的(集合)类型可以被本包的函数进行排序。方法要求集合中的元素可以被整数索引。

废话不多少,直接上代码:

go对象自定义排序实现源码

可自定义要排序的字段和排序方式

package mainimport ("fmt""sort"
)// 用于存放Hero数据
type Hero struct {Name  string  `json:"name"`Score float64 `json:"score"`
}// 用于存放Hero切片
type heroData struct {data []Heroby   func(h1 *Hero, h2 *Hero) bool
}// 自定义一个排序函数类型 By
type By func(h1 *Hero, h2 *Hero) bool// 自定义排序方法,
func (b By) MySort(hs []Hero) {hdata := &heroData{data: hs, by: b} // 排序对象初始化放这里sort.Sort(hdata)                    // 调用Sort进行排序
}func (h heroData) Len() int {return len(h.data)
}
func (h heroData) Less(i, j int) bool {// return h.data[i].Score < h.data[j].Score // 这里是固定使用Score排序,return h.by(&h.data[i], &h.data[j]) // 将排序字段放入到函数里面 外面需要怎么排序,这几传递一个函数来就可以
}
func (h heroData) Swap(i, j int) {h.data[i], h.data[j] = h.data[j], h.data[i]
}func main() {// 普通方式,固定排序字段// var h1 = heroData{data: []Hero{{Name: "John", Score: 99.8}, {Name: "Alice", Score: 80}, {Name: "Bob", Score: 45}}}// sort.Sort(h1)// fmt.Printf("%v \n", h1.data)// 准备数据data := []Hero{{Name: "John", Score: 99.8}, {Name: "Alice", Score: 80}, {Name: "Bob", Score: 45}, {Name: "Jack", Score: 100}}// 可自定义排序方法var nameSort = func(h1 *Hero, h2 *Hero) bool { return h1.Name < h2.Name }var scoreSort = func(h1 *Hero, h2 *Hero) bool { return h1.Score < h2.Score }//排序By(nameSort).MySort(data)fmt.Printf("按照Name排序:%v\n", data)By(scoreSort).MySort(data)fmt.Printf("按照Score排序: %v\n", data)
}

基本数据类型的排序代码

可自定义排序的方式 从大到小或者从小到大

package mainimport ("fmt""sort"
)/*
sort.Sort() 这里的参数是一个接口, 即我们只需要在我们自定义的类型里面实现这个Interface这个接口里面定义的Len Less Swap方法 然后将类型变量传进sor就会自动版我们进行排序.
函数原型:func Sort(data Interface)
Sort排序data。它调用1次data.Len确定长度,调用O(n*log(n))次data.Less和data.Swap。本函数不能保证排序的稳定性(即不保证相等元素的相对次序不变)。排序接口
type Interface interface {// Len方法返回集合中的元素个数Len() int// Less方法报告索引i的元素是否比索引j的元素小Less(i, j int) bool// Swap方法交换索引i和j的两个元素Swap(i, j int)
}
一个满足sort.Interface接口的(集合)类型可以被本包的函数进行排序。方法要求集合中的元素可以被整数索引。*/type MyDemo struct {data []intby   func(l int, r int) bool
}// 自定义一个函数数据类型 By
type By func(v1 int, v2 int) bool// 将这个MySort函数绑定到自定义类型By上面
func (b By) MySort(arr []int) {dd := MyDemo{data: arr, by: b}// 调用sort包里面的Sort方法sort.Sort(dd)
}// Len方法返回集合中的元素个数
func (m MyDemo) Len() int {return len(m.data)
}// Less方法报告索引i的元素是否比索引j的元素小
func (m MyDemo) Less(i, j int) bool {//return m.data[i] < m.data[j]return m.by(m.data[i], m.data[j]) // 将数据交给一个函数去比较
}// Swap方法交换索引i和j的两个元素
func (m MyDemo) Swap(i, j int) {//使用中间变量交换 传统方法// tmp := m.data[i]// m.data[i] = m.data[j]// m.data[j] = tmp// 利用go的批量赋值特性 直接交换m.data[i], m.data[j] = m.data[j], m.data[i]
}func main() {var mm = MyDemo{data: []int{1, 8, 99, 3, 6, 7, 8, 13, 199, 200, 20, 2}}fmt.Println("排序前:", mm)// 将这个匿名函数传递进去,每次数据比较的时候都会被调用mm.by = func(l, r int) bool {return l > r // 这里可以控制从大到小 > 还是从小到大 < 排序}// 排序sort.Sort(mm)fmt.Println("排序后:", mm)//排序后: {[1 2 3 6 7 8 8 13 20 99 199 200]}fmt.Println("------------------------------------------------")// 要排序的int切片arr := []int{90, 567, 1, 9, 8, 99, 3, 6, 7, 8, 13, 199, 200, 20, 2}// 定义用于排序的函数 注意,如果 这里的函数入参改为结构体, 就可以对结构体内的字段进行排序small2bigSort := func(v1, v2 int) bool { return v1 < v2 } // 从小到大排序函数big2smallSort := func(v1, v2 int) bool { return v1 > v2 } // 从大到小 排序函数By(small2bigSort).MySort(arr)fmt.Println("small2bigSort=", arr) // small2bigSort= [1 2 3 6 7 8 8 9 13 20 90 99 199 200 567]By(big2smallSort).MySort(arr)fmt.Println("big2smallSort=", arr) // big2smallSort= [567 200 199 99 90 20 13 9 8 8 7 6 3 2 1]
}

总结: 在上面的示例中我们使用了2种方式来传递排序函数, 1是直接以匿名函数的方式将函数先赋值给结构体字段,然后再通过方法调用, 另外一种方式是定义了一个函数变量, 然后将MySort这个方法绑定到了自定义函数上来实现调用系统的pdqsort实现快速排序。

pdqsort快速排序函数源码 参考


// pdqsort sorts data[a:b].
// The algorithm based on pattern-defeating quicksort(pdqsort), but without the optimizations from BlockQuicksort.
// pdqsort paper: https://arxiv.org/pdf/2106.05123.pdf
// C++ implementation: https://github.com/orlp/pdqsort
// Rust implementation: https://docs.rs/pdqsort/latest/pdqsort/
// limit is the number of allowed bad (very unbalanced) pivots before falling back to heapsort.
func pdqsort(data Interface, a, b, limit int) {const maxInsertion = 12var (wasBalanced    = true // whether the last partitioning was reasonably balancedwasPartitioned = true // whether the slice was already partitioned)for {length := b - aif length <= maxInsertion {insertionSort(data, a, b)return}// Fall back to heapsort if too many bad choices were made.if limit == 0 {heapSort(data, a, b)return}// If the last partitioning was imbalanced, we need to breaking patterns.if !wasBalanced {breakPatterns(data, a, b)limit--}pivot, hint := choosePivot(data, a, b)if hint == decreasingHint {reverseRange(data, a, b)// The chosen pivot was pivot-a elements after the start of the array.// After reversing it is pivot-a elements before the end of the array.// The idea came from Rust's implementation.pivot = (b - 1) - (pivot - a)hint = increasingHint}// The slice is likely already sorted.if wasBalanced && wasPartitioned && hint == increasingHint {if partialInsertionSort(data, a, b) {return}}// Probably the slice contains many duplicate elements, partition the slice into// elements equal to and elements greater than the pivot.if a > 0 && !data.Less(a-1, pivot) {mid := partitionEqual(data, a, b, pivot)a = midcontinue}mid, alreadyPartitioned := partition(data, a, b, pivot)wasPartitioned = alreadyPartitionedleftLen, rightLen := mid-a, b-midbalanceThreshold := length / 8if leftLen < rightLen {wasBalanced = leftLen >= balanceThresholdpdqsort(data, a, mid, limit)a = mid + 1} else {wasBalanced = rightLen >= balanceThresholdpdqsort(data, mid+1, b, limit)b = mid}}
}

http://www.yayakq.cn/news/392091/

相关文章:

  • 网站搭建工作怎么样网易企业邮箱可以保存多少邮件
  • 网站栏目推介怎么做主题在wordpress
  • 阳江做网站公司潍坊知名网站建设怎么收费
  • 网站链接怎么做跳转郑州网站设计的公司
  • 网站建设伍金手指下拉3贴吧网站开发需求分析
  • 南安建设局网站wordpress seo插件哪个好
  • 汉中站网站建设 软件有哪些
  • 介绍小说的网站模板下载地址设计网站公司收费
  • 网站建设证书网上国网推广多少钱一个户
  • 黑色网站欣赏邢台最新通知今天
  • jsp做的网站可以用的泊头市网站建设公司
  • 网站建设z亿玛酷1负责网络设计用什么软件
  • 公众号软文素材南京专业网站优化公司
  • 河南新站关键词排名优化外包百度集团网站建设方案
  • 辛集网站建设哪家好济南网站制
  • 冷库 东莞网站建设免费咨询医生回答在线妇科
  • 手机网站制作 费怎么做分录什么是营销型网站呢
  • 行业协会网站建设的方案做企业网站的代码
  • 网站留言板模版市场推广专员
  • 网站兼容手机浏览器怎样用dw做新闻发布网站
  • 郑州做网站外包的公司sem seo什么意思
  • 如何免费开个人网站电子商务网站建设与管理程序设计题
  • 湖北省网站备案最快几天电子商务网站建设规划书的内容
  • 单页面网站跳出率邯郸房产网站
  • 网站建站 优化推广商标注册多少钱
  • 开远市新农村数字建设网站建设网站 容量
  • 顺德高端网站济南企业上云网站建设
  • 网站建设仟首先金手指12工业企业利润加快回升
  • 宿迁市建设局投诉网站计算机大二建设网站
  • 农业建设项目管理信息系统网站网站建设需要哪些素材