C做的冒泡排序动态演示.docx

上传人:b****5 文档编号:5150719 上传时间:2022-12-13 格式:DOCX 页数:21 大小:197.80KB
下载 相关 举报
C做的冒泡排序动态演示.docx_第1页
第1页 / 共21页
C做的冒泡排序动态演示.docx_第2页
第2页 / 共21页
C做的冒泡排序动态演示.docx_第3页
第3页 / 共21页
C做的冒泡排序动态演示.docx_第4页
第4页 / 共21页
C做的冒泡排序动态演示.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

C做的冒泡排序动态演示.docx

《C做的冒泡排序动态演示.docx》由会员分享,可在线阅读,更多相关《C做的冒泡排序动态演示.docx(21页珍藏版)》请在冰豆网上搜索。

C做的冒泡排序动态演示.docx

C做的冒泡排序动态演示

冒泡排序动态演示

一、工作介绍

利用C#语言实现了冒泡排序的动态演示,可以对十个0~100间的整数进行冒泡排序,排序过程较清晰明了,每次排序的结果都显示出来,把冒泡的思想展现了出来,对初次理解冒泡的人有很好的帮助作用!

1、首先设置了两个窗口,一个作为输入数字和排序方法选择的窗体,另一个作为手动输入数字时的临时窗体,当上述的初始化工作结束后,选择了冒泡方法后,就进入了排序过程的窗体,在这里就开始演示冒泡排序的思想,以上是程序的基本框架。

2、依次介绍各个模块的实现过程。

①初始化排序数字和排序方法选择窗体,该窗体如下图1所示

图1初始化排序数字和排序方法选择窗体

此窗体共有4个单击事件触发控件,其分别为图1所示的“手动输入数”、“生成随机数”、“普通冒泡演示”和“优化冒泡演示”,前两个实现待排序数字的初始化的功能,后两个实现冒泡方法选择的功能。

此窗体还含有3个Time控件和1个toolTip控件,Time控件用来实现程序执行的时间间隔,而toolTip控件用来实现实时显示窗体鼠标处的坐标用途,以来更好的分配各个模块的位置。

另外还有2个label控件分别用来显示初始化的数字和排好序后的结果。

②相关控件的主要代码

1.1“手动输入数”按钮的触发事件代码:

privatevoidbutton3_Click_1(objectsender,EventArgse)

{

Inputinput=newInput();

input.ShowDialog();

randNums=input.randNums;

if(randNums==null)return;

label1.Text="";

for(inti=0;i

{

label1.Text+=randNums[i]+"";

}

}

其中input.ShowDialog();此语句的执行就打开了输入数字的窗体:

如下图所示:

如图中所示的窗体,共有三个控件分别为:

label、txtInput和Button。

其主要的代码如下:

publicpartialclassInput:

Form

{

publicint[]randNums=newint[10];

publicInput()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

stringtext=;

string[]result=text.Split(',');

if(result.Length!

=10)

{

MessageBox.Show("输入个数不是十个");

return;

}

inti=0;

for(;i<10;i++)

{

try

{

randNums[i]=int.Parse(result[i]);

}

catch

{

MessageBox.Show("第"+(i+1)+"个输入不是整数");

return;

}

if(randNums[i]<0||randNums[i]>99)

{

MessageBox.Show("第"+(i+1)+"个数不在0-100");

return;

}

}

this.Close();

}

privatevoidtxtInput_TextChanged(objectsender,EventArgse)

{

}

privatevoidlabel1_Click(objectsender,EventArgse)

{

}

privatevoidInput_Load(objectsender,EventArgse)

{

}

}

}

1.2“生成随机数”按钮的触发事件代码:

privatevoidbutton1_Click(objectsender,EventArgse)

{ShowRandom(10);

}

publicvoidShowRandom(intnum)

{

if(label1.Text!

="")label1.Text="";

label1.Font=newFont(Font.SystemFontName,15);

randNums=newint[num];

Randomrand=newRandom();

for(inti=0;i

{

randNums[i]=rand.Next(100);

label1.Text+=randNums[i]+"";

}

}

其效果如下图所示:

1.3“普通冒泡演示”和“优化冒泡演示”按钮

当数字初始化完成后就可以选择排序功能的工作其代码基本相同,原理也一样这里就拿“普通冒泡演示”来说明,并且他们是和Time控件一起工作的,其代码如下:

namespacehyl

{

publicpartialclassForm1:

Form

{intstep=0,start=0,state=0;

intamend=0;

publicint[]randNums;

Formdemo;

Label[,]labels;

Labelsign,explain;

ButtonstartButton,stepButton,stopButton,closeButton;

intsrcX,srcY,targetX,targetY,indexI,indexJ;

inti=0,i1=0,m;

boolflag=true;

publicForm1()

{

InitializeComponent();

}

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

amend=0;

if(randNums==null)

{

MessageBox.Show("请先生成随机数");

return;

}

demo=newForm();

demo.StartPosition=;

demo.Size=new,Screen.GetWorkingArea(this).Height);

demo.Text="通用冒泡过程观看";

stepButton=newButton();

stepButton.Location=new,60);

stepButton.Size=new,25);

stepButton.Text="单步观看";

stepButton.Click+=newSystem.EventHandler(this.stepButton_Click);

;

startButton=newButton();

startButton.Location=new,90);

startButton.Size=new,25);

startButton.Text="连续观看";

startButton.Click+=newSystem.EventHandler(this.startButton_Click);

;

stopButton=newButton();

stopButton.Location=new,120);

stopButton.Size=new,25);

stopButton.Text="停止";

stopButton.Click+=newSystem.EventHandler(this.stopButton_Click);

;

closeButton=newButton();

closeButton.Location=new,150);

closeButton.Size=new,25);

closeButton.Text="关闭窗口";

closeButton.Click+=newSystem.EventHandler(this.closeButton_Click);

;

explain=newLabel();

explain.AutoSize=true;

explain.ForeColor=Color.Red;

explain.Location=new,2);

explain.Font=newFont(Font.SystemFontName,13);

;

Labeltitle=newLabel();

title.SetBounds(800,200,200,450);

title.Text="冒泡排序(Bubblesort)是基于交换排序的一种算法。

它是依次两两比较待排序元素;若为逆序(递增或递减)则进行交换,将待排序元素从左至右比较一遍称为一趟“冒泡”。

每趟冒泡都将待排序列中的最大关键字交换到最后(或最前)位置。

直到全部元素有序为止。

";

title.Font=newFont(Font.SystemFontName,9);

title.ForeColor=Color.Red;

;

sign=newLabel();;

sign.AutoSize=true;

sign.Font=newFont(Font.SystemFontName,15);

sign.BackColor=Color.YellowGreen;

sign.ForeColor=Color.Black;

;

m=randNums.Length;

labels=newLabel[m,m];

demo.Show();

}

privatevoidstartButton_Click(objectsender,EventArgse)

{

if(start==0)

{

start=1;

stepButton.Enabled=false;

sortTimer.Enabled=true;

}

}

privatevoidstopButton_Click(objectsender,EventArgse)

{

timer1.Enabled=false;

timer2.Enabled=false;

sortTimer.Enabled=false;

stopButton.Enabled=false;

startButton.Enabled=false;

}

privatevoidstepButton_Click(objectsender,EventArgse)

{

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;

}

elseif(state==2)

{

labels[i,i1].BackColor=Color.White;

labels[i,i1+1].BackColor=Color.Pink;

i1++;

state=3;

timer2.Enabled=false;

}

elseif(state==3)

{

timer2.Enabled=true;

}

}

privatevoidcloseButton_Click(objectsender,EventArgse)

{

sortTimer.Enabled=false;

timer1.Enabled=false;

timer2.Enabled=false;

i=0;

i1=0;

start=0;

step=0;

state=0;

flag=true;

demo.Close();

}

privatevoidsortTimer_Tick(objectsender,EventArgse)

{

LabelstepLabel=newLabel();

stepLabel.AutoSize=true;

stepLabel.Location=new,i*70+55);

stepLabel.Font=newFont(Font.SystemFontName,10);

stepLabel.Text="第"+(i+1)+"趟";

;

for(intj=0;j

{

labels[i,j]=newLabel();

labels[i,j].AutoSize=true;

labels[i,j].Location=new*70+50,i*70+50);

labels[i,j].Font=newFont(Font.SystemFontName,15);

if(j+i>=m||i+1==m)labels[i,j].BackColor=Color.Pink;

elselabels[i,j].BackColor=Color.White;

labels[i,j].ForeColor=Color.Black;;

labels[i,j].Text=randNums[j]+"";

labels[i,j].Size=new,20);

j]);

}

if(i+1>=m||(!

flag&&amend==1))

{

label2.Text="";

label2.Font=newFont(Font.SystemFontName,15);

for(intk=0;k

{

label2.Text+=randNums[k]+"";

}

sign.Location=new,-30);

timer1.Enabled=false;

sortTimer.Enabled=false;

timer1.Enabled=false;

timer2.Enabled=false;

start=0;

step=0;

state=0;

i=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;

}

privatevoidtimer2_Tick(objectsender,EventArgse)

{

inttemp=0;

if(i1

{

if(i1==0)

{

labels[i,i1].BackColor=Color.Pink;

}

if(randNums[i1]>randNums[i1+1])

{

if(amend==1)flag=true;

sign.Location=new*70+85,i*70+50);

sign.Text=">";

explain.Text="因为"+randNums[i1]+">"+randNums[i1+1]+",又因为大数总向下沉,所以要交换位置.";

temp=randNums[i1];

randNums[i1]=randNums[i1+1];

randNums[i1+1]=temp;

indexI=i;

indexJ=i1;

srcX=labels[indexI,indexJ].Location.X;

srcY=labels[indexI,indexJ].Location.Y;

targetX=labels[indexI,indexJ+1].Location.X;

targetY=labels[indexI,indexJ+1].Location.Y;

if(step==1)

{

state=1;

}

else

{

timer1.Enabled=true;

}

timer2.Enabled=false;

}

else

{

if(randNums[i1]

{

sign.Location=new*70+85,i*70+50);

sign.Text="<";

explain.Text="因为"+randNums[i1]+"<"+randNums[i1+1]+",又因为大数本来就在下方,所以不用交换位置.";

}

else

{

sign.Location=new*70+85,i*70+50);

sign.Text="=";

explain.Text="因为"+randNums[i1]+"="+randNums[i1+1]+",所以不用变动位置.";

}

if(step==1)

{

state=2;

return;

}

else

{

labels[i,i1].BackColor=Color.White;

labels[i,i1+1].BackColor=Color.Pink;

}

i1++;

}

}

else

{

i++;

sign.Location=new,-30);

sortTimer.Enabled=true;

timer2.Enabled=false;

}

}

privatevoidtimer1_Tick(objectsender,EventArgse)

{

if(labels[indexI,indexJ].Location.Y>srcY-25)

{

labels[indexI,indexJ].Location=new,indexJ].Location.X,labels[indexI,indexJ].Location.Y-1);

}

else

{

if(labels[indexI,indexJ].Location.X

{

labels[indexI,indexJ].Location=new,indexJ].Location.X+1,labels[indexI,indexJ].Location.Y);

}

else

{

labels[indexI,indexJ].Location=new,targetY);

}

}

if(labels[indexI,indexJ+1].Location.Y

{

labels[indexI,indexJ+1].Location=new,indexJ+1].Location.X,labels[indexI,indexJ+1].Location.Y+1);

}

else

{

if(labels[indexI,indexJ+1].Location.X>srcX)

{

labels[indexI,indexJ+1].Location=new,indexJ+1].Location.X-1,labels[indexI,indexJ+1].Location.Y);

}

else

{

labels[indexI,indexJ+1].Location=new,srcY);

LabeltempLabel=labels[indexI,indexJ];

labels[indexI,indexJ]=labels[indexI,indexJ+1];

labels[indexI,indexJ+1]=tempLabel;

sign.Location=new,-30);

i1++;

if(step==1)

{

state=3;

}

else

{

=true;

}

=false;

}

}

}

privatevoidbutton3_Click(objectsender,EventArgse)

{

Inputinput=newInput();

input.ShowDialog();

randNums=input.randNums;

if(randNums==null)return;

label1.Text="";

for(inti=0;i

{

label1.Text+=randNums[i]+"";

}

}

privatevoidbutton4_Click(objectsender,EventArgse)

{amend=1;

if(randNums==null)

{

MessageBox.Show("请先生成随机数");

return;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1