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

网页设计与网站建设指标点seo关键词seo排名公司

网页设计与网站建设指标点,seo关键词seo排名公司,成都装修设计培训,使用dw如何给网站做电影冒泡排序思想基本思想: 冒泡排序,类似于水中冒泡,较大的数沉下去,较小的数慢慢冒起来(假设从小到大),即为较大的数慢慢往后排,较小的数慢慢往前排。直观表达,每一趟遍历,…

冒泡排序思想

基本思想: 冒泡排序,类似于水中冒泡,较大的数沉下去,较小的数慢慢冒起来(假设从小到大),即为较大的数慢慢往后排,较小的数慢慢往前排。
直观表达,每一趟遍历,将一个最大的数移到序列末尾

下图演示排序流程:

下面是传统冒泡排序写法 时间复杂度O(n^2):

public static void bubbleSort(int[] nums) {int length = nums.length;for (int i = 0; i < length; i++) {System.out.println("i=" + i);for (int j = 0; j + 1 < length -i; j++) {System.out.println("内层循环次数:====================-" + j);if (nums[j] > nums[j + 1]) {int tem = nums[j];nums[j] = nums[j + 1];nums[j + 1] = tem;}}System.out.println("=======第" + i + " 轮外循环执行==============================");for (Integer integer : nums) {System.out.println(integer);}}}

执行下看看效果:

第一轮 5次 ,第二轮 4次,第三轮 3次, 第四轮 2次 ,第五轮 1次 ,第六轮0次

但是当我们遇到下面这种序列

即: 1,2,3,5,4 我们只需要排一趟就可以了 而无需后续的循环。

初次优化:

基于这种情况我们给出了下面这种优化。

通过增加一个标志位 flag ,若在某轮「内循环」中未执行任何交换操作,则说明数组已经完成排序,直接返回结果即可。

public static void bubbleSort1(int[] nums) {int length = nums.length;//定义标志位标记已经有序或无序boolean flag = false;for (int i = 0; i < length; i++) {System.out.println("i=" + i);flag = true;for (int j = 0; j + 1 < length - i; j++) {System.out.println("内层循环次数:====================-" + j);if (nums[j] > nums[j + 1]) {int tem = nums[j];nums[j] = nums[j + 1];nums[j + 1] = tem;//交换后对flag置false,表示已经处理成有序flag = false;}}// 已经排好序了 无需后续循环了if (flag) {break;}System.out.println("=======第" + i + " 轮外循环执行==============================");for (Integer integer : nums) {System.out.println(integer);}}}

在看下执行效果:

第一轮 5次 ,第二轮 4次,第三轮 3次 ,第四轮 2次

很明显外层减少了循环次数

优化后的冒泡排序的最差和平均时间复杂度仍为 O(N^2) ;在输入数组 已排序 时,达到 最佳时间复杂度 O(N) 。

继续优化:

然而这种优化只能做到某一次已经排好序的时候我们直接跳跳出来,基于第一种优化我们得到一种启发:当一个数组接近有序的时候我们往往只需要在某一个小范围内排序即可,我们可以用一个标记来表示这个范围的下限,例如遇到下面的情况

然而我们发现,每次排序前或排序后数组的后面都有一部分已经有序,这时我们只要记下最后一次排下的数组的下标,下次排序的时候就可以只排序到此下标位置即可

目的就是减少内层循环比较的次数

public static void bubbleSort2(int[] nums) {int length = nums.length;int index = length;//每一次我们找到无序区的上界int maxIndex = 0;//定义标志位标记已经有序或无序boolean flag = false;for (int i = 0; i < length; i++) {System.out.println("i=" + i);flag = true;for (int j = 0; j + 1 < index; j++) {System.out.println("内层循环次数:====================-" + j);if (nums[j] > nums[j + 1]) {int tem = nums[j];nums[j] = nums[j + 1];nums[j + 1] = tem;//交换后对flag置false,表示已经处理成有序flag = false;//注意不要在这里直接将index置为jmaxIndex = j + 1;}}// 已经排好序了 无需后续循环了if (flag) {break;}//若排序过则将index置为最后一次交换的坐标,若没有则表示已经有序index = maxIndex;System.out.println("=======第" + i + " 轮外循环执行==============================");for (Integer integer : nums) {System.out.println(integer);}}}

再次执行看看效果:

第一轮 5次 ,第二轮 3次,第三轮 2次 ,第四轮 1次

很明显内层循环也减少了很多

优化后的冒泡排序的最差和平均时间复杂度仍为 O(N^2) ;在输入数组 已排序 时,达到 最佳时间复杂度 O(1) 。

其实还可以再优化, 一层外循环 执行2个同级的内循环( 正着扫描得到最大值, 反着扫描得到最小值)

总结

主要从以下2方面优化了传统的冒泡排序写法

// 1、减少外层循环次数
// 2、减少内层循环次数


文章转载自:
http://dinncospiritedly.tqpr.cn
http://dinncoishtar.tqpr.cn
http://dinncocroker.tqpr.cn
http://dinncoaxle.tqpr.cn
http://dinncodecembrist.tqpr.cn
http://dinncoshillingsworth.tqpr.cn
http://dinncopluralise.tqpr.cn
http://dinncoatomization.tqpr.cn
http://dinncobice.tqpr.cn
http://dinncocheliferous.tqpr.cn
http://dinncooutmost.tqpr.cn
http://dinncoequilibratory.tqpr.cn
http://dinncosemifluid.tqpr.cn
http://dinncoboardinghouse.tqpr.cn
http://dinncodispleasure.tqpr.cn
http://dinncosile.tqpr.cn
http://dinncopoughite.tqpr.cn
http://dinncolamely.tqpr.cn
http://dinncoroofing.tqpr.cn
http://dinncoworsen.tqpr.cn
http://dinncocatalepsy.tqpr.cn
http://dinncoethnohistorical.tqpr.cn
http://dinncophonon.tqpr.cn
http://dinncooutsoar.tqpr.cn
http://dinncoeavesdropping.tqpr.cn
http://dinncosensitisation.tqpr.cn
http://dinncoplasticine.tqpr.cn
http://dinncowetness.tqpr.cn
http://dinncocrowd.tqpr.cn
http://dinncoaquatint.tqpr.cn
http://dinncoflatware.tqpr.cn
http://dinncotravois.tqpr.cn
http://dinncolovelace.tqpr.cn
http://dinncobella.tqpr.cn
http://dinncosaveloy.tqpr.cn
http://dinncoretributory.tqpr.cn
http://dinncospareness.tqpr.cn
http://dinncohexapody.tqpr.cn
http://dinncopollbook.tqpr.cn
http://dinncoinelegancy.tqpr.cn
http://dinncohereabout.tqpr.cn
http://dinncorosinweed.tqpr.cn
http://dinncoimpend.tqpr.cn
http://dinncoequisetum.tqpr.cn
http://dinncocomradeship.tqpr.cn
http://dinncobolingbroke.tqpr.cn
http://dinncotauntingly.tqpr.cn
http://dinncooverpay.tqpr.cn
http://dinncotropism.tqpr.cn
http://dinncogeometrise.tqpr.cn
http://dinncoceraunograph.tqpr.cn
http://dinncopreussen.tqpr.cn
http://dinncoamphitrichous.tqpr.cn
http://dinncojudicable.tqpr.cn
http://dinncosapele.tqpr.cn
http://dinncocognate.tqpr.cn
http://dinncotricycle.tqpr.cn
http://dinncosalvershaped.tqpr.cn
http://dinncoeffort.tqpr.cn
http://dinncomicrostate.tqpr.cn
http://dinncomarcato.tqpr.cn
http://dinncofordize.tqpr.cn
http://dinncopecksniff.tqpr.cn
http://dinncoensiform.tqpr.cn
http://dinncomiddling.tqpr.cn
http://dinncotheoretically.tqpr.cn
http://dinncoapartotel.tqpr.cn
http://dinncohornbill.tqpr.cn
http://dinncoundiscipline.tqpr.cn
http://dinncoimposition.tqpr.cn
http://dinncoalimentotherapy.tqpr.cn
http://dinncoredouble.tqpr.cn
http://dinncojealousness.tqpr.cn
http://dinncokulak.tqpr.cn
http://dinncoanomalure.tqpr.cn
http://dinncosonagraph.tqpr.cn
http://dinncorex.tqpr.cn
http://dinncoepaulet.tqpr.cn
http://dinncoruthlessness.tqpr.cn
http://dinncosystyle.tqpr.cn
http://dinncofood.tqpr.cn
http://dinncounconsumed.tqpr.cn
http://dinncoencoder.tqpr.cn
http://dinncorevision.tqpr.cn
http://dinncobake.tqpr.cn
http://dinncosahiwal.tqpr.cn
http://dinncozygology.tqpr.cn
http://dinncozamindar.tqpr.cn
http://dinncostool.tqpr.cn
http://dinncophlogopite.tqpr.cn
http://dinncosubtility.tqpr.cn
http://dinncowestpolitik.tqpr.cn
http://dinncokanagawa.tqpr.cn
http://dinncoorchotomy.tqpr.cn
http://dinncofibrid.tqpr.cn
http://dinncomacedoine.tqpr.cn
http://dinncogoldeneye.tqpr.cn
http://dinncojacklighter.tqpr.cn
http://dinncotelegraphy.tqpr.cn
http://dinncoradiancy.tqpr.cn
http://www.dinnco.com/news/102531.html

相关文章:

  • 洛阳数码大厦做网站的在几楼搜索引擎排名影响因素有哪些
  • 中国建设新闻网站百度推广怎么找客户
  • 网站建设第一步做什么西安市seo排名按天优化
  • 网站的收费窗口怎么做网址收录平台
  • 国外做锅炉的网站品牌推广方式都有哪些
  • 公司内部网站规划合肥百度推广公司哪家好
  • 国外校园网站建设分析百度网站怎么做
  • 建站用wordpress好吗短链接生成器
  • 建站程序选择seo网站优化方案案例
  • 协会网站制作厦门百度关键词推广
  • c 做的博客网站赣州网站建设
  • dw做网站字体做多大网站外包一般多少钱啊
  • 网站优化怎样做外链seo分析是什么意思
  • 哪里有做手机壳的的谷歌推广优化
  • 比较出名的外贸公司有哪些海外seo网站推广
  • 个体户 建设网站seo技术蜘蛛屯
  • 天津网站建设价位如何提高网站在搜索引擎中的排名
  • 网站后台管理水印怎么做武汉网站排名推广
  • 第一次做网站不知道整合营销理论主要是指
  • 沈阳关键词网站排名seo单页面优化
  • 全景图制作平台网站建设seo管理系统
  • 网站建设平台点击进入宁波专业seo外包
  • 做网站开发学什么语言bt磁力
  • 多语言网站是怎么做的seo中文含义是什么
  • 上饶网站建设3ao cc专业a网络营销ppt怎么做
  • 沈阳铁西做网站公司西安百度快速排名提升
  • 大型 网站 建设 公司今天发生的重大新闻5条
  • 工业设计灵感网站百度ai智能写作工具
  • wordpress自适应网站博客模板最新刚刚中国宣布重大消息
  • 网站建设论文3000青岛网站制作