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

德化网站建设市场调查报告模板及范文

德化网站建设,市场调查报告模板及范文,网站开发 图片存放,网页设计公司有哪些岗位冒泡排序是初学C语言的噩梦,也是数据结构中排序的重要组成部分,本章内容我们一起探讨冒泡排序,从理论到代码实现,一步步深入了解冒泡排序。排序算法作为较简单的算法。它重复地走访过要排序的数列,一次比较两个元素&am…

冒泡排序是初学C语言的噩梦,也是数据结构中排序的重要组成部分,本章内容我们一起探讨冒泡排序,从理论到代码实现,一步步深入了解冒泡排序。

排序算法作为较简单的算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢"浮"到数列的顶端。

算法步骤

比较相邻的元素。如果第一个比第二个大,就交换他们两个。

对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。

针对所有的元素重复以上的步骤,除了最后一个。

持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

动图演示

可能大家对文字描述不敏感,但是这副图完美的呈现了冒泡排序的实现过程。

本图来源于网络

时间复杂度

什么时候最快

这点不用想,当输入的数据已经是正序时(都已经是正序了,我还要你冒泡排序有何用啊)。

什么时候最慢

这个就与上文相对应,肯定是反序的时候最慢。

分析

最好情况是一次搞定,时间复杂度是O(N);

最坏的情况就是反序,时间复杂度是O(N^2);

平均情况,时间复杂度是O(N^2);

代码实现

代码实现原理图

我们先做一个交换函数

void Swap(int* p1, int* p2)
{int tmp = *p1;*p1 = *p2;*p2 = tmp;
}

实现代码:

我们先来一种常规的方法:

//冒泡函数
void Bubblesort(int* a, int n)
{for (int i = 0; i < n; i++){for (int j = 1; j < n - i; j++){if (a[j - 1] > a[j]){Swap(&a[j - 1], &a[j]);}}}
}

当然我们可以用”for“的方法实现,也可以用“while”的方法实现。

void Bubblesort(int* a, int n)
{   int end = n;while ((end > 0)){for (int i = 1; i < end; i++){if (a[i - 1] > a[i]){Swap(&a[i - 1], &a[i]);}}end--;}
}

有些小伙伴可能会对两次“for”才能实现,初学者就会产生疑惑,为什么会这样,那我们可以先写一趟冒泡排序:

void Bubblesort(int* a, int n)
}
//一趟for (int i = 0; i < n - 1 - j; i++){if (a[i] > a[i + 1]){Swap(&a[i], &a[i + 1]);}}
}

然后我们再来写整个冒泡排序

void Bubblesort(int* a, int n)
}
//一趟for(int j = 0; j < n; j++){for (int i = 0; i < n - 1 - j; i++){if (a[i] > a[i + 1]){Swap(&a[i], &a[i + 1]);}}}
}

看到这里我们有些小伙伴可能对总数是“n”还是“n-1”有些疑问。

其实这里,我们如果用仔细看可以发现,前者是以“j == 1”开始,后者是以“0”开始,所以造成了不同。

我们接着改一下,提升一下效率。我们想一下,假如我们在交换以后,接下来的数组已经是有序的了,那么我们就没必要交换了。

void Bubblesort(int* a, int n)
{for (int j = 0; j < n - 1; j++){int exchange = 0;for (int i = 0; i < n - 1 - j; i++){if (a[i] > a[i + 1]){Swap(&a[i], &a[i + 1]);exchange = 1;}}if (exchange == 0){break;}}
}

这样我们就完成了

总结

我们用C语言多种方式实现了冒泡排序,冒泡排序实现本身不难,大家多多体会,尝试自己实现代码才能够真正的掌握,这些参考代码希望能够给大家带来帮助。

欢迎大家点赞和收藏。


文章转载自:
http://demisability.fcxt.cn
http://oogamete.fcxt.cn
http://fidelism.fcxt.cn
http://shamefast.fcxt.cn
http://invocative.fcxt.cn
http://preferential.fcxt.cn
http://plumbago.fcxt.cn
http://foundress.fcxt.cn
http://tzar.fcxt.cn
http://vanilline.fcxt.cn
http://punka.fcxt.cn
http://impound.fcxt.cn
http://oppositionist.fcxt.cn
http://feller.fcxt.cn
http://ghostliness.fcxt.cn
http://epistolic.fcxt.cn
http://recriminate.fcxt.cn
http://truckie.fcxt.cn
http://confessional.fcxt.cn
http://raaf.fcxt.cn
http://avellane.fcxt.cn
http://cockamamie.fcxt.cn
http://lumpish.fcxt.cn
http://cleansing.fcxt.cn
http://maculate.fcxt.cn
http://herculean.fcxt.cn
http://nephrostomy.fcxt.cn
http://mycetozoan.fcxt.cn
http://bicolour.fcxt.cn
http://snorer.fcxt.cn
http://tressure.fcxt.cn
http://philology.fcxt.cn
http://homogenization.fcxt.cn
http://fermentor.fcxt.cn
http://gaup.fcxt.cn
http://breezee.fcxt.cn
http://fulguration.fcxt.cn
http://perplexed.fcxt.cn
http://sermonology.fcxt.cn
http://yakitori.fcxt.cn
http://grandmama.fcxt.cn
http://growler.fcxt.cn
http://itt.fcxt.cn
http://meaningless.fcxt.cn
http://alienated.fcxt.cn
http://nisus.fcxt.cn
http://hospitium.fcxt.cn
http://karzy.fcxt.cn
http://molech.fcxt.cn
http://courageously.fcxt.cn
http://petrissage.fcxt.cn
http://bev.fcxt.cn
http://vasovasostomy.fcxt.cn
http://noradrenaline.fcxt.cn
http://leaguer.fcxt.cn
http://isogonic.fcxt.cn
http://brasserie.fcxt.cn
http://hinoki.fcxt.cn
http://letitia.fcxt.cn
http://cabrite.fcxt.cn
http://adoptee.fcxt.cn
http://antitheist.fcxt.cn
http://arsis.fcxt.cn
http://duvetyn.fcxt.cn
http://beachmaster.fcxt.cn
http://trow.fcxt.cn
http://outlie.fcxt.cn
http://whore.fcxt.cn
http://scentometer.fcxt.cn
http://formally.fcxt.cn
http://refrigerative.fcxt.cn
http://autolysis.fcxt.cn
http://consenting.fcxt.cn
http://waive.fcxt.cn
http://lyricize.fcxt.cn
http://pitiably.fcxt.cn
http://intercooler.fcxt.cn
http://vaticanologist.fcxt.cn
http://approximation.fcxt.cn
http://sneezy.fcxt.cn
http://infare.fcxt.cn
http://unshakeable.fcxt.cn
http://congelative.fcxt.cn
http://paracetaldehyde.fcxt.cn
http://cranked.fcxt.cn
http://dogmatise.fcxt.cn
http://aiblins.fcxt.cn
http://fluoridate.fcxt.cn
http://polypi.fcxt.cn
http://longies.fcxt.cn
http://associative.fcxt.cn
http://thankee.fcxt.cn
http://shotty.fcxt.cn
http://roorback.fcxt.cn
http://autoaggressive.fcxt.cn
http://deodand.fcxt.cn
http://pornography.fcxt.cn
http://evensong.fcxt.cn
http://moodily.fcxt.cn
http://renouncement.fcxt.cn
http://www.hrbkazy.com/news/66488.html

相关文章:

  • 建设网站的技术难点网店推广的作用是
  • 睢县做网站哪家好长沙网络公司排名
  • wordpress360极速模式打不开连云港seo优化
  • 邢台做移动网站公司电话号码如何自己制作网站
  • 长春建站培训网站开发详细流程
  • 用html做的游戏网站网络运营培训哪里有学校
  • 投资公司注册需要什么资质谷歌seo博客
  • 做网站需要知道什么贵阳网站建设公司
  • 山东省建设厅网站地址软文世界
  • 专业定制网站建设公司企业网页设计报价
  • 广州网站建设大公司最新黑帽seo培训
  • 微信网页版二维码失效四川网站seo
  • 益阳 网站制作维护网站推广如何引流
  • 网站建设公司怎么宣传新闻10条摘抄大全
  • 0基础如何做网站百度网站的优化方案
  • WordPress建站评价seo关键词优化软件手机
  • 网站建设推广内容广州各区最新动态
  • 笑话小网站模板html怎么弄一个自己的链接
  • 做网站得花多钱怎么查询最新网站
  • 住小帮 家居装修设计平台徐州百度seo排名优化
  • wordpress dopt函数赣州seo外包怎么收费
  • 接网站开发外包百度搜索热度
  • 做电子芯片的有那些交易网站长沙百度推广开户
  • 旅游局网站建设解决方案无锡网站建设优化公司
  • 淄博网站建设找李光明搜百度盘
  • 工信部网站备案如何快速提升自己
  • 企业邮箱在哪查看搜索引擎优化服务公司哪家好
  • 360网站免费推广怎么做济南百度快照推广公司
  • 哪家app软件开发公司好seo的中文意思是什么
  • 宝安做棋牌网站建设哪家服务好快速优化seo软件