C#做的冒泡排序动态演示_第1页
C#做的冒泡排序动态演示_第2页
C#做的冒泡排序动态演示_第3页
C#做的冒泡排序动态演示_第4页
C#做的冒泡排序动态演示_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、冒泡排序动态演示一、 工作介绍利用c#语言实现了冒泡排序的动态演示,可以对十个0100间的整数进行冒泡排序,排序过程较清晰明了,每次排序的结果都显示出来,把冒泡的思想展现了出来,对初次理解冒泡的人有很好的帮助作用!1、首先设置了两个窗口,一个作为输入数字和排序方法选择的窗体,另一个作为手动输入数字时的临时窗体,当上述的初始化工作结束后,选择了冒泡方法后,就进入了排序过程的窗体,在这里就开始演示冒泡排序的思想,以上是程序的基本框架。 2、依次介绍各个模块的实现过程。初始化排序数字和排序方法选择窗体,该窗体如下图1所示图1初始化排序数字和排序方法选择窗体此窗体共有4个单击事件触发控件,其分别为图1

2、所示的“手动输入数”、“生成随机数”、“普通冒泡演示”和“优化冒泡演示”,前两个实现待排序数字的初始化的功能,后两个实现冒泡方法选择的功能。此窗体还含有3个time控件和1个tooltip控件,time控件用来实现程序执行的时间间隔,而tooltip控件用来实现实时显示窗体鼠标处的坐标用途,以来更好的分配各个模块的位置。另外还有2个label控件分别用来显示初始化的数字和排好序后的结果。相关控件的主要代码1.1“手动输入数”按钮的触发事件代码:private void button3_click_1(object sender, eventargs e) input input = new i

3、nput(); input.showdialog(); randnums = input.randnums; if (randnums = null) return; label1.text = ; for (int i = 0; i randnums.length; i+) label1.text += randnumsi + ; 其中input.showdialog();此语句的执行就打开了输入数字的窗体:如下图所示:如图中所示的窗体,共有三个控件分别为:label、txtinput和button。其主要的代码如下: public partial class input : form pu

4、blic int randnums=new int10; public input() initializecomponent(); private void button1_click(object sender, eventargs e) string text = this.txtinput.text; string result = text.split(,); if (result.length != 10) messagebox.show(输入个数不是十个); return; int i = 0; for (; i 10; i+) try randnumsi = int.parse

5、(resulti); catch messagebox.show(第 + (i + 1) + 个输入不是整数); return; if (randnumsi 99) messagebox.show(第 + (i + 1) + 个数不在0-100); return; this.close(); private void txtinput_textchanged(object sender, eventargs e) private void label1_click(object sender, eventargs e) private void input_load(object sender

6、, eventargs e) 1.2“生成随机数”按钮的触发事件代码:private void button1_click(object sender, eventargs e) showrandom(10); public void showrandom(int num) if (label1.text != ) label1.text = ; label1.font = new font(font.systemfontname, 15); randnums=new intnum; random rand = new random(); for (int i = 0; i num; i+)

7、randnumsi = rand.next(100); label1.text += randnumsi + ; 其效果如下图所示:1.3“普通冒泡演示”和“优化冒泡演示”按钮当数字初始化完成后就可以选择排序功能的工作其代码基本相同,原理也一样这里就拿“普通冒泡演示”来说明,并且他们是和time控件一起工作的,其代码如下:namespace hyl public partial class form1 : form int step = 0,start=0,state=0; int amend = 0; public int randnums; form demo; label, labels

8、; label sign,explain; button startbutton, stepbutton,stopbutton,closebutton; int srcx, srcy, targetx, targety,indexi,indexj; int i = 0,i1=0,m; bool flag = true; public form1() initializecomponent(); private void button2_click(object sender, eventargs e) amend = 0; if (randnums = null) messagebox.sho

9、w(请先生成随机数); return; demo = new form(); demo.startposition = system.windows.forms.formstartposition.centerscreen; demo.size = new system.drawing.size(screen.getworkingarea(this).width, screen.getworkingarea(this).height); demo.text = 通用冒泡过程观看; stepbutton = new button(); stepbutton.location = new syst

10、em.drawing.point(850, 60); stepbutton.size = new system.drawing.size(90, 25); stepbutton.text = 单步观看; stepbutton.click += new system.eventhandler(this.stepbutton_click); demo.controls.add(stepbutton); startbutton = new button(); startbutton.location = new system.drawing.point(850, 90); startbutton.s

11、ize = new system.drawing.size(90, 25); startbutton.text = 连续观看; startbutton.click += new system.eventhandler(this.startbutton_click); demo.controls.add(startbutton); stopbutton = new button(); stopbutton.location = new system.drawing.point(850, 120); stopbutton.size = new system.drawing.size(90, 25)

12、; stopbutton.text = 停止; stopbutton.click += new system.eventhandler(this.stopbutton_click); demo.controls.add(stopbutton); closebutton = new button(); closebutton.location = new system.drawing.point(850, 150); closebutton.size = new system.drawing.size(90, 25); closebutton.text = 关闭窗口; closebutton.c

13、lick += new system.eventhandler(this.closebutton_click); demo.controls.add(closebutton); explain = new label(); explain.autosize = true; explain.forecolor = color.red; explain.location = new system.drawing.point(180,2); explain.font = new font(font.systemfontname, 13); demo.controls.add(explain); la

14、bel title = new label(); title.setbounds(800, 200,200,450); title.text = 冒泡排序(bubble sort)是基于交换排序的一种算法。它是依次两两比较待排序元素;若为逆序(递增或递减)则进行交换,将待排序元素从左至右比较一遍称为一趟“冒泡”。 每趟冒泡都将待排序列中的最大关键字交换到最后(或最前)位置。直到全部元素有序为止。; title.font = new font(font.systemfontname, 9); title.forecolor = color.red; demo.controls.add(title

15、); sign = new label(); ; sign.autosize = true; sign.font = new font(font.systemfontname, 15); sign.backcolor = color.yellowgreen; sign.forecolor = color.black; demo.controls.add(sign); m = randnums.length; labels = new labelm, m; demo.show(); private void startbutton_click(object sender, eventargs e

16、) if (start = 0) start = 1; stepbutton.enabled = false; sorttimer.enabled = true; private void stopbutton_click(object sender, eventargs e) timer1.enabled = false; timer2.enabled = false; sorttimer.enabled = false; stopbutton.enabled = false; startbutton.enabled = false; private void stepbutton_clic

17、k(object sender, eventargs e) if (start = 0) start = 1; step = 1; sorttimer.interval = 500; timer2.interval = 500; stopbutton.enabled = false; startbutton.enabled = false; sorttimer.enabled = true; if(state=1) timer1.enabled = true; else if(state=2) labelsi, i1.backcolor = color.white; labelsi, i1 +

18、 1.backcolor = color.pink; i1+; state = 3; timer2.enabled =false; else if(state=3) timer2.enabled = true; private void closebutton_click(object sender, eventargs e) sorttimer.enabled = false; timer1.enabled = false; timer2.enabled = false; i = 0; i1 = 0; start = 0; step = 0; state = 0; flag = true;

19、demo.close(); private void sorttimer_tick(object sender, eventargs e) label steplabel = new label(); steplabel.autosize = true; steplabel.location=new system.drawing.point(0, i * 70 + 55); steplabel.font = new font(font.systemfontname, 10); steplabel.text = 第 + (i + 1) + 趟; demo.controls.add(steplab

20、el); for (int j = 0; j = m | i+1=m) labelsi, j.backcolor = color.pink; else labelsi, j.backcolor = color.white; labelsi, j.forecolor = color.black; ; labelsi, j.text = randnumsj + ; labelsi, j.size = new system.drawing.size(0, 20); demo.controls.add(labelsi, j); if (i + 1 = m | (!flag & amend=1) lab

21、el2.text = ; label2.font = new font(font.systemfontname, 15); for (int k = 0; k m; k+) label2.text += randnumsk + ; sign.location = new system.drawing.point(-30,-30); timer1.enabled = false; sorttimer.enabled = false; timer1.enabled = false; timer2.enabled = false; start = 0; step = 0; state = 0; i

22、= 0; i1 = 0; startbutton.enabled = false; stepbutton.enabled = false; if (!flag & amend = 1) explain.text = 因为上一趟没有数据的交换,所以这组数据已经有序,也就不需要继续比较.; messagebox.show(演示完成); return; i1 = 0; if(amend=1) flag = false; timer2.enabled = true; sorttimer.enabled = false; private void timer2_tick(object sender, e

23、ventargs e) int temp = 0; if (i1 randnumsi1 + 1) if (amend = 1) flag = true; sign.location = new system.drawing.point(i1 * 70 + 85, i * 70 + 50); sign.text = ; explain.text = 因为 + randnumsi1 + + randnumsi1 + 1 + ,又因为大数总向下沉,所以要交换位置.; temp = randnumsi1; randnumsi1 = randnumsi1 + 1; randnumsi1 + 1 = te

24、mp; indexi = i; indexj = i1; srcx = labelsindexi, indexj.location.x; srcy = labelsindexi, indexj.location.y; targetx = labelsindexi, indexj + 1.location.x; targety = labelsindexi, indexj + 1.location.y; if (step = 1) state = 1; else timer1.enabled = true; timer2.enabled = false; else if (randnumsi1

25、randnumsi1 + 1) sign.location = new system.drawing.point(i1 * 70 + 85, i * 70 + 50); sign.text = ; explain.text = 因为 + randnumsi1 + srcy - 25) labelsindexi, indexj.location = new system.drawing.point(labelsindexi, indexj.location.x, labelsindexi, indexj.location.y - 1); else if (labelsindexi, indexj

26、.location.x targetx) labelsindexi, indexj.location = new system.drawing.point(labelsindexi, indexj.location.x + 1, labelsindexi, indexj.location.y); else labelsindexi, indexj.location = new system.drawing.point(targetx, targety); if (labelsindexi, indexj + 1.location.y srcx) labelsindexi, indexj + 1

27、.location = new system.drawing.point(labelsindexi, indexj + 1.location.x - 1, labelsindexi, indexj + 1.location.y); else labelsindexi, indexj + 1.location = new system.drawing.point(srcx, srcy); label templabel = labelsindexi, indexj; labelsindexi, indexj = labelsindexi, indexj + 1; labelsindexi, in

28、dexj+1 = templabel; sign.location = new system.drawing.point(-30, -30); i1+; if (step = 1) state = 3; else this.timer2.enabled = true; this.timer1.enabled = false; private void button3_click(object sender, eventargs e) input input = new input(); input.showdialog(); randnums = input.randnums; if (ran

29、dnums = null) return; label1.text = ; for (int i = 0; i randnums.length; i+) label1.text += randnumsi + ; private void button4_click(object sender, eventargs e) amend = 1; if (randnums = null) messagebox.show(请先生成随机数); return; demo = new form(); demo.startposition = system.windows.forms.formstartpos

30、ition.centerscreen; demo.size = new system.drawing.size(screen.getworkingarea(this).width, screen.getworkingarea(this).height); demo.text = 改良冒泡过程观看; stepbutton = new button(); stepbutton.location = new system.drawing.point(850, 60); stepbutton.size = new system.drawing.size(90, 25); stepbutton.text

31、 = 单步观看; stepbutton.click += new system.eventhandler(this.stepbutton_click); demo.controls.add(stepbutton); startbutton = new button(); startbutton.location = new system.drawing.point(850, 90); startbutton.size = new system.drawing.size(90, 25); startbutton.text = 连续观看; startbutton.click += new syst

32、em.eventhandler(this.startbutton_click); demo.controls.add(startbutton); stopbutton = new button(); stopbutton.location = new system.drawing.point(850, 120); stopbutton.size = new system.drawing.size(90, 25); stopbutton.text = 停止; stopbutton.click += new system.eventhandler(this.stopbutton_click); d

33、emo.controls.add(stopbutton); closebutton = new button(); closebutton.location = new system.drawing.point(850, 150); closebutton.size = new system.drawing.size(90, 25); closebutton.text = 关闭窗口; closebutton.click += new system.eventhandler(this.closebutton_click); demo.controls.add(closebutton); expl

34、ain = new label(); explain.autosize = true; explain.forecolor = color.red; explain.location = new system.drawing.point(180, 2); explain.font = new font(font.systemfontname, 13); demo.controls.add(explain); label title = new label(); title.setbounds(800, 200, 200, 450); title.text = 冒泡排序(bubble sort)

35、是基于交换排序的一种算法。它是依次两两比较待排序元素;若为逆序(递增或递减)则进行交换,将待排序元素从左至右比较一遍称为一趟“冒泡”。 每趟冒泡都将待排序列中的最大关键字交换到最后(或最前)位置。直到全部元素有序为止。| 改良的排序算法会判断上一趟比较完后,有没有数据交换,没有就说明这组数据已经是有序的,也就不需要继续比较.这样就减少了排序的次数,但是他需要多一个变量来标识上一趟的状态.; title.font = new font(font.systemfontname, 9); title.forecolor = color.red; demo.controls.add(title); sign

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论