C做的冒泡排序动态演示文档格式.docx
- 文档编号:18120781
- 上传时间:2022-12-13
- 格式:DOCX
- 页数:21
- 大小:197.80KB
C做的冒泡排序动态演示文档格式.docx
《C做的冒泡排序动态演示文档格式.docx》由会员分享,可在线阅读,更多相关《C做的冒泡排序动态演示文档格式.docx(21页珍藏版)》请在冰豆网上搜索。
}
其中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(;
10;
try
randNums[i]=int.Parse(result[i]);
catch
第"
+(i+1)+"
个输入不是整数"
if(randNums[i]<
0||randNums[i]>
99)
个数不在0-100"
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();
num;
randNums[i]=rand.Next(100);
其效果如下图所示:
1.3“普通冒泡演示”和“优化冒泡演示”按钮
当数字初始化完成后就可以选择排序功能的工作其代码基本相同,原理也一样这里就拿“普通冒泡演示”来说明,并且他们是和Time控件一起工作的,其代码如下:
namespacehyl
{
publicpartialclassForm1:
{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()
privatevoidbutton2_Click(objectsender,EventArgse)
amend=0;
if(randNums==null)
请先生成随机数"
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)
step=1;
sortTimer.Interval=500;
timer2.Interval=500;
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)
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="
趟"
for(intj=0;
j<
m;
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<
k++)
label2.Text+=randNums[k]+"
sign.Location=new,-30);
if(!
amend==1)
explain.Text="
因为上一趟没有数据的交换,所以这组数据已经有序,也就不需要继续比较."
演示完成"
if(amend==1)flag=false;
privatevoidtimer2_Tick(objectsender,EventArgse)
inttemp=0;
if(i1<
m-i-1)
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="
>
因为"
+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
if(randNums[i1]<
<
又因为大数本来就在下方,所以不用交换位置."
="
所以不用变动位置."
state=2;
i++;
sign.Location=new,-30);
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);
if(labels[indexI,indexJ].Location.X<
targetX)
labels[indexI,indexJ].Location=new,indexJ].Location.X+1,labels[indexI,indexJ].Location.Y);
labels[indexI,indexJ].Location=new,targetY);
if(labels[indexI,indexJ+1].Location.Y<
targetY+25)
labels[indexI,indexJ+1].Location=new,indexJ+1].Location.X,labels[indexI,indexJ+1].Location.Y+1);
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);
labels[indexI,indexJ+1].Location=new,srcY);
LabeltempLabel=labels[indexI,indexJ];
labels[indexI,indexJ]=labels[indexI,indexJ+1];
labels[indexI,indexJ+1]=tempLabel;
=true;
=false;
privatevoidbutton3_Click(objectsender,EventArgse)
}
privatevoidbutton4_Click(objectsender,EventArgse)
{amend=1;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 冒泡 排序 动态 演示