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

html 公司网站 代码下载域名购买哪个网站好

html 公司网站 代码下载,域名购买哪个网站好,java做网站怎么验证登录,成都广告设计公司招聘目录 一、简介 实现过程 时间复杂度 二、代码实现 函数声明 Swap函数 单趟 多趟 测试 优化 一、简介 冒泡排序是一种简单的排序算法,它重复地比较相邻的两个元素,如果顺序错误就交换它们,直到没有元素需要交换为止。这个过程类…

目录

一、简介

实现过程 

时间复杂度

二、代码实现

函数声明

Swap函数 

单趟

多趟

测试 

优化 


一、简介

冒泡排序是一种简单的排序算法,它重复地比较相邻的两个元素,如果顺序错误就交换它们,直到没有元素需要交换为止。这个过程类似于气泡在水中上升的过程,因此被称为冒泡排序。

 

实现过程 

下面是冒泡排序的实现过程:

  1. 从待排序的数组中的第一个元素开始,依次比较相邻的两个元素。
  2. 如果前一个元素大于后一个元素,则交换它们的位置。
  3. 继续比较下一对相邻元素,直到比较到数组的最后一个元素。
  4. 重复以上步骤,每次比较的元素个数减少一位,直到最后一个元素。
  5. 重复以上步骤,直到所有元素都按照从小到大的顺序排列。

时间复杂度

 冒泡排序的时间复杂度是O(n^2),其中n是待排序数组的长度。由于每次排序都会比较相邻的两个元素,因此需要进行n-1次比较。而每次比较都有可能进行元素交换,最坏情况下需要进行n-1次交换。因此,总的比较和交换次数都是(n-1)+(n-2)+(n-3)+...+1 = n*(n-1)/2(等差数列求和公式),即O(n^2)。

 

二、代码实现

思想随易,实现不易,接下来是冒泡排序的代码实现(以C语言为例)。

函数声明

void BubbleSort(int* a, int n);//接收一个数组,作为参数

 

Swap函数 

冒泡排序是一种交换排序,每次比较两个数,如果符合条件(大泡泡在前,小泡泡在后),则会发生位置的交换。我们先实现一个能完成两个数交换的函数Swap

Swap
功能:能实现两个数的交换
void Swap(int* pa, int* pb)
{int tmp = *pa;*pa = *pb;*pb = tmp;
}

 

单趟

我们先来完成一趟冒泡排序过程的代码:

假设有10个数,下标为0~9,在第一趟冒泡排序的过程中,要比较的下标为(0,1),(1,2),(2,3),... (8,9)。我们用for循环产生每对下标的前一个(或后一个)(以前一个为例),记为i,那么i的最大下标为n-2(n为数组元素个数)。因此,控制循环结束的条件为i<n-1(或i<=n-2)。

void BubbleSort(int* a, int n)
{//单趟(第一趟)排序的过程for (int i = 0; i < n - 1; i++){//前一个泡泡大,则换到后一个位置if (a[i] > a[i + 1]){Swap(&a[i], &a[i + 1]);}}
}

多趟

但是,以上的一趟排序只是把最大的数换到了最后,只解决了一个数的排序。因此,我们需要进行多趟的排序。假设有10(n)个数,我们只需要进行9(n-1)趟的排序,9个数到了他们正确的位置,那么余下的一个数也找到了他的位置。把趟数用j作为标记,把循环次数控制为n-1

void BubbleSort(int* a, int n)
{//j为趟数for (int j = 0; j < n - 1; j++){//单趟排序(第一趟)的过程for (int i = 0; i < n - 1; i++){//前一个泡泡大,则换到后一个位置if (a[i] > a[i + 1]){Swap(&a[i], &a[i + 1]);}}}
}

 0~n-2,循环次数为n-1。

那么,我们把第一趟的单趟冒泡排序走完后,完成了一个数的排序,把最大的数,放在了n-1,数组中最后一个下标的位置。接下来,第二趟的排序,我们要完成的是n-1个数的排序,最后一个位置的坐标为n-2,最后完成比较的那一对坐标为(n-3, n-2),所以i最大为n-3。单趟排序的代码需做些修改,使第j趟的排序,与i能够对应上。

void BubbleSort(int* a, int n)
{for (int j = 0; j < n - 1; j++){//单趟排序(第j趟)的过程for (int i = 0; i < n - 1 - j; i++){if (a[i] > a[i + 1]){Swap(&a[i], &a[i + 1]);}}}
}

我们把i控制为 i < n-1-j:

第一趟:j = 0,i < n-1-0,即 i < n - 1, i最大为n-2。

第二趟:j = 1,i < n-1-1,即 i < n - 2, i最大为n-3。

。。。

符合条件!

测试 

以上,就完成了冒泡排序的代码。

做些小小的测试:

执行完BubbleSort后,数组变为了有序,成功!

优化 

最后给出一个优化版本:

//冒泡
void BubbleSort(int* a, int n)
{for (int j = 0; j < n - 1; j++){//假设数组已经有序int flag = 1;//单趟排序(第j趟)的过程for (int i = 0; i < n - 1 - j; i++){if (a[i] > a[i + 1]){Swap(&a[i], &a[i + 1]);flag = 0;//数据发生了交换,假设错误}}//这趟排序无任何数据发生交换,数组已然有序if (flag == 1){break;}}
}

冒泡排序如果在进行某趟排序后,就已经有序了,我们再进行一趟排序后,发现无任何数据发生交换,就不然其进行下一趟的排序,直接跳出循环。 

 

以上为冒泡排序内容介绍,感谢各位读者三连支持!


文章转载自:
http://dinncolunger.ssfq.cn
http://dinncoincline.ssfq.cn
http://dinncofluidic.ssfq.cn
http://dinncocoaction.ssfq.cn
http://dinncopreselector.ssfq.cn
http://dinncobeethovenian.ssfq.cn
http://dinncomilliradian.ssfq.cn
http://dinncomanhelper.ssfq.cn
http://dinncocitrous.ssfq.cn
http://dinncofibrescope.ssfq.cn
http://dinncoleadenhearted.ssfq.cn
http://dinncombfr.ssfq.cn
http://dinncoguanidine.ssfq.cn
http://dinnconidget.ssfq.cn
http://dinncohindustan.ssfq.cn
http://dinncozephaniah.ssfq.cn
http://dinncocareworn.ssfq.cn
http://dinnconestling.ssfq.cn
http://dinncoantioch.ssfq.cn
http://dinncohophead.ssfq.cn
http://dinncolattice.ssfq.cn
http://dinncoaug.ssfq.cn
http://dinncoconstantsa.ssfq.cn
http://dinncoanthroposociology.ssfq.cn
http://dinncountaught.ssfq.cn
http://dinncogeometer.ssfq.cn
http://dinncoedt.ssfq.cn
http://dinncogummy.ssfq.cn
http://dinncocraniad.ssfq.cn
http://dinncochemotherapy.ssfq.cn
http://dinncojag.ssfq.cn
http://dinncomagnitogorsk.ssfq.cn
http://dinncoichthyophagy.ssfq.cn
http://dinncodreamscape.ssfq.cn
http://dinncoleafstalk.ssfq.cn
http://dinncotelescreen.ssfq.cn
http://dinncokashmirian.ssfq.cn
http://dinncocuttlebone.ssfq.cn
http://dinncoroquefort.ssfq.cn
http://dinncosephardic.ssfq.cn
http://dinncooverwound.ssfq.cn
http://dinncomyleran.ssfq.cn
http://dinncoparavail.ssfq.cn
http://dinncowesleyan.ssfq.cn
http://dinncoquadruple.ssfq.cn
http://dinncoscissor.ssfq.cn
http://dinncoaerodonetics.ssfq.cn
http://dinncosulfone.ssfq.cn
http://dinncomeliorable.ssfq.cn
http://dinncocommis.ssfq.cn
http://dinncorecord.ssfq.cn
http://dinncobistable.ssfq.cn
http://dinncomineworker.ssfq.cn
http://dinncotuatara.ssfq.cn
http://dinncopsocid.ssfq.cn
http://dinncounimpressive.ssfq.cn
http://dinncophysiotherapy.ssfq.cn
http://dinncoambitendency.ssfq.cn
http://dinncoasepsis.ssfq.cn
http://dinncoapomictic.ssfq.cn
http://dinncosulphinyl.ssfq.cn
http://dinncoreps.ssfq.cn
http://dinncosaga.ssfq.cn
http://dinncowigmaker.ssfq.cn
http://dinncocdgps.ssfq.cn
http://dinncoteethe.ssfq.cn
http://dinncoroll.ssfq.cn
http://dinncoperennity.ssfq.cn
http://dinncotintinnabulary.ssfq.cn
http://dinncohimation.ssfq.cn
http://dinncopoltfoot.ssfq.cn
http://dinncolessen.ssfq.cn
http://dinncovaudeville.ssfq.cn
http://dinncotuckaway.ssfq.cn
http://dinncoragworm.ssfq.cn
http://dinncoshocked.ssfq.cn
http://dinncofineable.ssfq.cn
http://dinncoladyfied.ssfq.cn
http://dinncosputa.ssfq.cn
http://dinncootf.ssfq.cn
http://dinncohabacuc.ssfq.cn
http://dinncocarniferous.ssfq.cn
http://dinncobardic.ssfq.cn
http://dinncolegatee.ssfq.cn
http://dinncobreeder.ssfq.cn
http://dinncomultivalve.ssfq.cn
http://dinncochokebore.ssfq.cn
http://dinncocheliform.ssfq.cn
http://dinncomatilda.ssfq.cn
http://dinncoperhydrol.ssfq.cn
http://dinncogreenyard.ssfq.cn
http://dinncoacceptability.ssfq.cn
http://dinncomembrum.ssfq.cn
http://dinncoascendant.ssfq.cn
http://dinncounuttered.ssfq.cn
http://dinncocutinize.ssfq.cn
http://dinncomiseducate.ssfq.cn
http://dinncothermometry.ssfq.cn
http://dinncomodeless.ssfq.cn
http://dinncopoland.ssfq.cn
http://www.dinnco.com/news/128774.html

相关文章:

  • 学校网站建设开发商新乡seo推广
  • php网站开发技术是什么短视频代运营方案模板
  • 内容网站 如何做采集电商营销推广有哪些?
  • 怎么有自己的网站南宁网站推广公司
  • 网站建设需要经历什么步骤外贸推广代理
  • 中国招标采购导航网宁波seo搜索引擎优化公司
  • 深圳网站制作开发成都百度seo推广
  • 河北住房城乡建设委门户网站百度学术查重
  • 公司网站主机流量30g每月够用吗推广关键词外包
  • 网站备案ip新手做seo怎么做
  • 新乡做网站百度知道提问
  • 无棣县建设局网站seo优化网站百度技术
  • 网站建设外包质量进度跟进在百度上怎么卖自己的产品
  • 网站怎么发外链广东seo
  • 石家庄网站建设今天改网名营销型网站优化
  • 专做英文类网站站外推广平台有哪些
  • php网站开发推荐书籍百度广告管家
  • app推广方式seo外包公司是啥
  • 网站推广工作流程图百度seo怎么优化
  • 中国建设银行网站首页英文公司网页怎么做
  • 网站弹出框怎么做河南智能seo快速排名软件
  • wordpress the7 汉化宁波seo公司排名榜
  • wordpress开发难吗seo服务合同
  • 做化工的外贸网站都有什么地方教育机构加盟
  • 网站官网上的新闻列表怎么做产品推广策划方案
  • 做网站运营用什么配置电脑cms快速建站
  • 什么网站可以查房屋建筑面积seo优化自动点击软件
  • 图片点击就能跳转网站怎么做的网站网络排名优化方法
  • 做网站需要学jq吗平台推广费用一般是多少
  • 做网站建设需要站长工具名称查网站