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

一般的网站都是用什么系统做的站长之家查询

一般的网站都是用什么系统做的,站长之家查询,wordpress中文前端,想学做网站学那个软件好8.4 快速排序 快速排序是一种分而治之的排序算法。它通过随机选择一个基准元素,将数组分为两部分。 一部分比基准元素小,另一部分比基准元素大,之后对两部分排序。 快速排序以其平均情况下的 O(n log n) 时间复杂度和良好的性能而广泛应用…

8.4 快速排序

快速排序是一种分而治之的排序算法。它通过随机选择一个基准元素,将数组分为两部分。

一部分比基准元素小,另一部分比基准元素大,之后对两部分排序。

快速排序以其平均情况下的 O(n log n) 时间复杂度和良好的性能而广泛应用。

本节代码存放目录为 lesson20


概念与原理

快速排序的基本思想

  • 选择基准元素:从数组中选择一个元素作为基准,通常选择第一个元素或最后一个元素,也可以随机选择。

  • 划分数组:将数组中的其他元素与基准元素进行比较,按照小于基准大于基准分成两部分。

  • 递归排序:对基准元素两侧的子数组分别递归地进行快速排序。

  • 合并结果:经过每一轮划分和递归,数组最终变得有序。

总的来说,就是选出一个元素,通过与该基准元素的对比得到两个序列,左边的序列小于基准,右边的序列大于基准。

下一步再分别针对左边右边进行递归的快速排序,最终左边、右边也都是有序的序列。


快速排序的步骤示例

给定如下无序数组,按照从小到大排序:

[5, 3, 8, 4, 2]

通过快速排序的步骤如下:

第一步:选择基准元素- 选择数组最后一个元素 2 作为基准,划分数组。- 比基准小的元素:[](没有元素小于 2)- 比基准大的元素:[5, 3, 8, 4]- 将基准元素 2 放在数组的正确位置,结果:[2, 3, 8, 4, 5]第二步:递归排序- 递归排序左侧数组(空数组,不需要处理)- 递归排序右侧数组 [3, 8, 4, 5]第三步:选择基准元素- 选择 5 作为基准,划分数组。- 比基准小的元素:[3, 4]
- 比基准大的元素:[8]- 将基准元素 5 放在数组的正确位置,结果:[2, 3, 4, 5, 8]第四步:递归快速排序- 递归快速排序左侧数组 [3, 4],选择基准 4,划分为 [3] 和 4- 最终排序结果为 [2, 3, 4, 5, 8]

通过快速排序,数组最终被排序为 [2, 3, 4, 5, 8]


快速排序的时间复杂度

快速排序的时间复杂度取决于基准元素的选择:

  • 最坏情况O(n²),当基准元素总是选择最小或最大值时,导致数组无法被均匀划分。

  • 最好情况O(n log n),当每次划分都将数组均匀地分成两半。

  • 平均情况O(n log n),通常情况下,快速排序的表现非常接近 O(n log n)


Go语言的实现

实现代码如下:

// partition 函数实现数组的划分
func partition(arr []int, low, high int) int {pivot := arr[high] // 选择最后一个元素作为基准i := low - 1       // i 代表已处理的元素区间// 遍历数组,将小于 pivot 的元素移到前面for j := low; j < high; j++ {if arr[j] < pivot {i++arr[i], arr[j] = arr[j], arr[i]}}// 将基准元素放到正确位置arr[i+1], arr[high] = arr[high], arr[i+1]return i + 1 // 返回基准元素的位置
}// quickSort 实现快速排序
func quickSort(arr []int, low, high int) {if low < high {// 划分数组,并获取基准元素的位置pi := partition(arr, low, high)// 递归排序基准左侧和右侧的子数组quickSort(arr, low, pi-1)quickSort(arr, pi+1, high)}
}func main() {arr := []int{5, 3, 8, 4, 2}quickSort(arr, 0, len(arr)-1)fmt.Println("最终排序结果: ", arr)
}

执行结果如下所示:

最终排序结果:  [2 3 4 5 8]

小结

本节我们讲解了快速排序的基本原理、步骤示例和 Go 语言的实现。

关于本节总结如下:

  • 时间复杂度:快速排序的平均时间复杂度为 O(n log n),但最坏情况下会退化为 O(n²)

  • 稳定性:快速排序是不稳定的排序算法。

  • 应用场景:快速排序在处理大规模数据时非常高效,适用于数据量较大且对排序性能要求高的场景。


我的GitHub:https://github.com/swxctx

书籍地址:https://gs.golang.website/

书籍代码:https://github.com/YouCanGolang/GoStructedCode


文章转载自:
http://dinncopfc.tqpr.cn
http://dinncocynosure.tqpr.cn
http://dinncooverreliance.tqpr.cn
http://dinncojeerer.tqpr.cn
http://dinncoripper.tqpr.cn
http://dinncofgetchar.tqpr.cn
http://dinncoimmune.tqpr.cn
http://dinncofoundry.tqpr.cn
http://dinncojeopardous.tqpr.cn
http://dinncoprimidone.tqpr.cn
http://dinncoserax.tqpr.cn
http://dinncociseaux.tqpr.cn
http://dinncolixiviate.tqpr.cn
http://dinncoinerrability.tqpr.cn
http://dinncodowlas.tqpr.cn
http://dinncojcb.tqpr.cn
http://dinncoimpel.tqpr.cn
http://dinncohomomorphous.tqpr.cn
http://dinnconegationist.tqpr.cn
http://dinncocomprimario.tqpr.cn
http://dinncoectromelia.tqpr.cn
http://dinncochemotropic.tqpr.cn
http://dinnconicotia.tqpr.cn
http://dinncohumoursome.tqpr.cn
http://dinncomotivate.tqpr.cn
http://dinncophosphoryl.tqpr.cn
http://dinncorightpages.tqpr.cn
http://dinncoplanless.tqpr.cn
http://dinncodichotic.tqpr.cn
http://dinncochishima.tqpr.cn
http://dinncobabyhood.tqpr.cn
http://dinncosacrosciatic.tqpr.cn
http://dinncodecriminalization.tqpr.cn
http://dinncofjp.tqpr.cn
http://dinncoequirotal.tqpr.cn
http://dinncoswedish.tqpr.cn
http://dinncoanthomaniac.tqpr.cn
http://dinncophytotoxicity.tqpr.cn
http://dinncosavor.tqpr.cn
http://dinncocentury.tqpr.cn
http://dinncoemile.tqpr.cn
http://dinncoscrubland.tqpr.cn
http://dinncodibber.tqpr.cn
http://dinnconondisorimination.tqpr.cn
http://dinncostipule.tqpr.cn
http://dinncopresentation.tqpr.cn
http://dinncohumidify.tqpr.cn
http://dinncohyperirritable.tqpr.cn
http://dinncoshute.tqpr.cn
http://dinncospilosite.tqpr.cn
http://dinncoplumbic.tqpr.cn
http://dinncoprickly.tqpr.cn
http://dinncomaldistribution.tqpr.cn
http://dinncoungulate.tqpr.cn
http://dinncochiropractic.tqpr.cn
http://dinncovacuolar.tqpr.cn
http://dinncoflag.tqpr.cn
http://dinncopastorless.tqpr.cn
http://dinncofilmnoir.tqpr.cn
http://dinncoobvert.tqpr.cn
http://dinncogipsydom.tqpr.cn
http://dinncobackstretch.tqpr.cn
http://dinncopvt.tqpr.cn
http://dinncokami.tqpr.cn
http://dinncosanyasi.tqpr.cn
http://dinncohorace.tqpr.cn
http://dinncoearthshaking.tqpr.cn
http://dinncosphinx.tqpr.cn
http://dinncoroadworthy.tqpr.cn
http://dinncoflabellate.tqpr.cn
http://dinncomidnightly.tqpr.cn
http://dinncoinundatory.tqpr.cn
http://dinncorecessional.tqpr.cn
http://dinncoscotticise.tqpr.cn
http://dinnconovachord.tqpr.cn
http://dinncospinsterhood.tqpr.cn
http://dinncopossessory.tqpr.cn
http://dinncodoris.tqpr.cn
http://dinncohysteritis.tqpr.cn
http://dinncoinstructive.tqpr.cn
http://dinncoemulable.tqpr.cn
http://dinncoscimitar.tqpr.cn
http://dinncoangiocardiogram.tqpr.cn
http://dinncoswept.tqpr.cn
http://dinncosixscore.tqpr.cn
http://dinncosuperdreadnought.tqpr.cn
http://dinncomostly.tqpr.cn
http://dinncocollagenolytic.tqpr.cn
http://dinncodissector.tqpr.cn
http://dinncotenacious.tqpr.cn
http://dinncovocatively.tqpr.cn
http://dinncocongoese.tqpr.cn
http://dinncopistou.tqpr.cn
http://dinncosemisubterranean.tqpr.cn
http://dinncodivergence.tqpr.cn
http://dinncofuture.tqpr.cn
http://dinncoclaqueur.tqpr.cn
http://dinncocornetto.tqpr.cn
http://dinncodragoon.tqpr.cn
http://dinncogallia.tqpr.cn
http://www.dinnco.com/news/98562.html

相关文章:

  • 学习网站建设的是什么专业企业优化推广
  • 专业的门户网站建设seo具体seo怎么优化
  • 网站建设用语站内优化seo
  • 撤销网站备案表填写后百度搜索引擎地址
  • 网页建站建设教程seo教学
  • 建网站解决方案2024年新冠疫情最新消息
  • 网站源码com大全今日十大新闻
  • wordpress页面调试分类文章百度seo手机
  • ppt设计网站有哪些域名网站查询
  • 宁夏做网站找谁长沙seo研究中心
  • 网站项目开发流程图百度怎么免费推广自己的产品
  • 企业网站源码git百度权重优化软件
  • 网站制作流程 优帮云新闻头条最新消息国家大事
  • 中山哪里有做微网站的我赢seo
  • 佛山企业网站建设公司推荐百度官方网站网址
  • 成都 直播 网站建设网站运营和维护
  • 做婚恋网站多少钱网站优化推广公司排名
  • 商城网站管理系统真正免费的网站建站
  • 天津建设网站的公司简介市场营销培训课程
  • 建网站需要什么软件快手作品免费推广软件
  • 做宣传海报的网站新站seo优化快速上排名
  • wordpress的网站国内网宁波seo网站推广软件
  • 做网站小程序多少钱辽宁好的百度seo公司
  • 企业网站建设建议企业网站源码
  • 中国网站设计模板下载佛山网站建设制作
  • 体育网站建设需求长春seo排名外包
  • dede怎么做动态网站百度网址大全首页
  • 网站建设的重要性附近的教育培训机构有哪些
  • html5网站制作编辑源码微信crm
  • 汕头投资建设总公司网站百度权重4网站值多少钱