




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、冒泡排序動態(tài)演示一、工作介紹利用C#語言實現(xiàn)了冒泡排序的動態(tài)演示,可以對十個0100間的整數(shù)進(jìn)行冒泡排序,排序過程較清晰明了,每次排序的結(jié)果都顯示出來,把冒泡的思想展現(xiàn)了出來,對初次理解冒泡的人有很好的幫助作用!1、首先設(shè)臵了兩個窗口,一個作為輸入數(shù)字和排序方法選擇的窗體,另一個作為手動輸入數(shù)字時的臨時窗體,當(dāng)上述的初始化工作結(jié)束后,選擇了冒泡方法后,就進(jìn)入了排序過程的窗體,在這里就開始演示冒泡排序的思想,以上是程序的基本框架。2、依次介紹各個模塊的實現(xiàn)過程。初始化排序數(shù)字和排序方法選擇窗體,該窗體如下圖1所示圖1初始化排序數(shù)字和排序方法選擇窗體此窗體共有4個單擊事件觸發(fā)控件,其分別為圖1所示
2、的“手動輸入數(shù)”、“生成隨機(jī)數(shù)”、“普通冒泡演示”和“優(yōu)化冒泡演示”,前兩個實現(xiàn)待排序數(shù)字的初始化的功能,后兩個實現(xiàn)冒泡方法選擇的功能。此窗體還含有3個Time控件和1個toolTip控件,Time控件用來實現(xiàn)程序執(zhí)行的時間間隔,而toolTip控件用來實現(xiàn)實時顯示窗體鼠標(biāo)處的坐標(biāo)用途,以來更好的分配各個模塊的位臵。另外還有2個label控件分別用來顯示初始化的數(shù)字和排好序后的結(jié)果。相關(guān)控件的主要代碼1.1“手動輸入數(shù)”按鈕的觸發(fā)事件代碼:privatevoidbutton3_Click_1(objectsender,EventArgse)Inputinput=newInput();input
3、.ShowDialog();randNums=input.randNums;if(randNums=null)return;label1.Text=""for(inti=0;i<randNums.Length;i+)label1.Text+=randNumsi+""其中input.ShowDialog();此語句的執(zhí)行就打開了輸入數(shù)字的窗體:如下圖所示:其主要的代碼如下:publicpartialclassInput:FormpublicintrandNums=newint10;publicInput()InitializeComponent();
4、privatevoidbutton1_Click(objectsender,EventArgse)stringtext=this.txtInput.Text;stringresult=text.Split(',');if(result.Length!=10)MessageBoxShow(輸入個數(shù)不是十個");return;inti=0;for(;i<10;i+)tryrandNumsi=int.Parse(resulti);catchMessageBoxShowf第"+(i+1)+"個輸入不是整數(shù)");return;if(randN
5、umsi<0|randNumsi>99)MessageBoxShowf第"+(i+1)+"個數(shù)不在0-100');return;this.Close();privatevoidtxtInput_TextChangedQbjectsender,EventArgse)_privatevoidlabel1_Click(objectsender,EventArgse)_privatevoidInput_Load(objectsender,EventArgse)_1.2“生成隨機(jī)數(shù)”按鈕的觸發(fā)事件代碼:privatevoidbuttton1_Click(objec
6、tsender,EventArgse)ShowRandom(10);publicvoidShowRandom(ntnum)if(label1.Text!="")label1.Text=""label1.Font=newFont(Font.SystemFontName,15);randNums=newintnum;Randomrand=newRandom();for(inti=0;i<num;i+)randNumsi=rand.Next(100);label1.Text+=randNumsi+""其效果如下圖所示:1.3“普通冒泡
7、演示”和“優(yōu)化冒泡演示”按鈕當(dāng)數(shù)字初始化完成后就可以選擇排序功能的工作其代碼基本相同,原理也一樣這里就拿“普通冒泡演示”來說明,并且他們是和Time控件一起工作的,其代碼如下:namespacehylpublicpartialclassForm1:Formintstep=0,start=0,state=0;intamend=0;publicintrandNums;Formdemo;Label,labels;Labelsign,explain;ButtonstartButton,stepButton,stopButton,closeButton;intsrcX,srcY,targetX,targ
8、etY,indexI,indexJ;inti=0,i1=0,m;boolflag=true;publicForm1()InitializeComponent();privatevoidbutton2_Click(objectsender,EventArgse)amend=0;if(randNums=null)MessageBoxShow(請先生成隨機(jī)數(shù)");return;demo=newForm();demo.StartPosition=System.Windows.Forms.FormStartPosition.CenterScreen;demo.Size=newSystem.Dr
9、awing.Size(Screen.GetWorkingArea(this).Width,Screen.GetWorkingArea(this).Height);demo.Text="通用冒泡過程觀看"stepButton=newButton();stepButton.Location=newSystem.Drawing.Point(850,60);stepButton.Size=newSystem.Drawing.Size(90,25);stepButton.Text="單步觀看"stepButton.Click+=newSystem.EventHan
10、dler(this.stepButton_Click);demo.Controls.Add(stepButton);startButton=newButton();startButton.Location=newSystem.Drawing.Point(850,90);startButton.Size=newSystem.Drawing.Size(90,25);startButton.Text="連續(xù)觀看"startButton.Click+=newSystem.EventHandler(this.startButton_Click);demo.Controls.Add(s
11、tartButton);stopButton=newButton();stopButton.Location=newSystem.Drawing.Point(850,120);stopButton.Size=newSystem.Drawing.Size(90,25);stopButton.Text="停止"stopButton.Click+=newSystem.EventHandler(this.stopButton_Click);demo.Controls.Add(stopButton);closeButton=newButton();closeButton.Locati
12、on=newSystem.Drawing.Point(850,150);closeButton.Size=newSystem.Drawing.Size(90,25);closeButton.Text="關(guān)閉窗口"closeButton.Click+=newSystem.EventHandler(this.closeButton_Click);demo.Controls.Add(closeButton);explain=newLabel();explain.AutoSize=true;explain.ForeColor=Color.Red;explain.Location=n
13、ewSystem.Drawing.Point(180,2);explain.Font=newFont(Font.SystemFontName,13);demo.Controls.Add(explain);Labeltitle=newLabel();title.SetBounds(800,200,200,450);title.Text="冒泡排序(Bubblesort)是基于交換排序的一種算法它是依次兩兩比較待排序元素;若為逆序(遞增或遞減)則進(jìn)行交換,將待排序元素從左至右比較一遍稱為一趟“冒泡”。每趟冒泡都將待排序列中的最大關(guān)鍵字交換到最后(或最前)位置。直到全部元素有序為止。&qu
14、ot;title.Font=newFont(Font.SystemFontName,9);title.ForeColor=Color.Red;demo.Controls.Add(title);sign=newLabel();sign.AutoSize=true;sign.Font=newFont(Font.SystemFontName,15);sign.BackColor=Color.YellowGreen;sign.ForeColor=Color.Black;demo.Controls.Add(sign);m=randNums.Length;labels=newLabelm,m;demo.S
15、how();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;priva
16、tevoidstepButton_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)labelsi,i1.BackColor=Color.White;labelsi,i1+1.BackColor=Color.Pi
17、nk;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)Label
18、stepLabel=newLabel();stepLabel.AutoSize=true;stepLabel.Location=newSystem.Drawing.Point(0,i*70+55);stepLabel.Font=newFont(Font.SystemFontName,10);stepLabel.Text="第"+(i+1)+"趟"demo.Controls.Add(stepLabel);for(intj=0;j<m;j+)labelsi,j=newLabel();labelsi,j.AutoSize=true;labelsi,j.L
19、ocation=newSystem.Drawing.Point(j*70+50,i70+50);labelsi,j.Font=newFont(Font.SystemFontName,15);if(j+i>=m|i+1=m)labelsi,j.BackColor=Color.Pink;elselabelsi,j.BackColor=Color.White;labelsi,j.ForeColor=Color.Black;labelsi,j.Text=randNumsj+""labelsi,j.Size=newSystem.Drawing.Size(0,20);demo.C
20、ontrols.Add(labelsi,j);if(i+1>=m|(!flag&&amend=1)label2.Text=""label2.Font=newFont(Font.SystemFontName,15);for(intk=0;k<m;k+)label2.Text+=randNumsk+""sign.Location=newSystem.Drawing.Point(-30,-30);timer1.Enabled=false;sortTimer.Enabled=false;timer1.Enabled=false;tim
21、er2.Enabled=false;start=0;step=0;state=0;i=0;i1=0;startButton.Enabled=false;stepButton.Enabled=false;if(!flag&&amend=1)explain.Text="因為上一趟沒有數(shù)據(jù)的交換,所以這組數(shù)據(jù)已經(jīng)有序,也就不需要繼續(xù)比較."MessageBoxShow(演示完成");return;i1=0;if(amend=1)flag=false;timer2.Enabled=true;sortTimer.Enabled=false;privatevo
22、idtimer2_Tick(objectsender,EventArgse)inttemp=0;if(i1<m-i-1)if(i1=0)labelsi,i1.BackColor=Color.Pink;if(randNumsi1>randNumsi1+1)if(amend=1)flag=true;sign.Location=newSystem.Drawing.Point(i1*70+85,i*70+50);sign.Text=">"explain.Text="因為"+randNumsi1+">"+randNums
23、i1+1+",又因為大數(shù)總向下沉,所以要交換位置."temp=randNumsi1;randNumsi1=randNumsi1+1;randNumsi1+1=temp;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;elsetimer1.Enab
24、led=true;timer2.Enabled=false;elseif(randNumsi1<randNumsi1+1)sign.Location=newSystem.Drawing.Point(i1*70+85,i*70+50);sign.Text="<"explain.Text="因為"+randNumsi1+"<"+randNumsi1+1+",又因為大數(shù)本來就在下方,所以不用交換位置."elsesign.Location=newSystem.Drawing.Point(i1*70+85,
25、i*70+50);sign.Text="="explain.Text="因為"+randNumsi1+"="+randNumsi1+1+",所以不用變動位置."if(step=1)state=2;return;elselabelsi,i1.BackColor=Color.White;labelsi,i1+1.BackColor=Color.Pink;i1+;elsei+;sign.Location=newSystem.Drawing.Point(-30,-30);sortTimer.Enabled=true;tim
26、er2.Enabled=false;privatevoidtimer1_Tick(objectsender,EventArgse)if(labelsindexI,indexJ.Location.Y>srcY-25)labelsindexI,indexJ.Location=newSystem.Drawing.Point(labelsindexI,indexJ.Location.X,labelsindexI,indexJ.Location.Y-1);elseif(labelsindexI,indexJ.Location.X<targetX)labelsindexI,indexJ.Loc
27、ation=newSystem.Drawing.Point(labelsindexI,indexJ.Location.X+1,labelsindexI,indexJ.Location.Y);elselabelsindexI,indexJ.Location=newSystem.Drawing.Point(targetX,targetY);if(labelsindexI,indexJ+1.Location.Y<targetY+25)labelsindexI,indexJ+1.Location=newSystem.Drawing.Point(labelsindexI,indexJ+1.Loca
28、tion.X,labelsindexI,indexJ+1.Location.Y+1);elseif(labelsindexI,indexJ+1.Location.X>srcX)labelsindexI,indexJ+1.Location=newSystem.Drawing.Point(labelsindexI,indexJ+1.Location.X-1,labelsindexI,indexJ+1.Location.Y);elselabelsindexI,indexJ+1.Location=newSystem.Drawing.Point(srcX,srcY);LabeltempLabel=
29、labelsindexI,indexJ;labelsindexI,indexJ=labelsindexI,indexJ+1;labelsindexI,indexJ+1=tempLabel;sign.Location=newSystem.Drawing.Point(-30,-30);i1+;if(step=1)state=3;elsethis.timer2.Enabled=true;this.timer1.Enabled=false;privatevoidbutton3_Click(objectsender,EventArgse)Inputinput=newInput();input.ShowD
30、ialog();randNums=input.randNums;if(randNums=null)return;label1.Text=""for(inti=0;i<randNums.Length;i+)label1.Text+=randNumsi+""privatevoidbutton4_Click(objectsender,EventArgse)amend=1;if(randNums=null)MessageBoxShow(請先生成隨機(jī)數(shù)");return;demo=newForm();demo.StartPosition=Syste
31、m.Windows.Forms.FormStartPosition.CenterScreen;demo.Size=newSystem.Drawing.Size(Screen.GetWorkingArea(this).Width,Screen.GetWorkingArea(this).Height);demo.Text="改良冒泡過程觀看"stepButton=newButton();stepButton.Location=newSystem.Drawing.Point(850,60);stepButton.Size=newSystem.Drawing.Size(90,25)
32、;stepButton.Text="單步觀看"stepButton.Click+=newSystem.EventHandler(this.stepButton_Click);demo.Controls.Add(stepButton);startButton=newButton();startButton.Location=newSystem.Drawing.Point(850,90);startButton.Size=newSystem.Drawing.Size(90,25);startButton.Text="連續(xù)觀看"startButton.Clic
33、k+=newSystem.EventHandler(this.startButton_Click);demo.Controls.Add(startButton);stopButton=newButton();stopButton.Location=newSystem.Drawing.Point(850,120);stopButton.Size=newSystem.Drawing.Size(90,25);stopButton.Text="停止"stopButton.Click+=newSystem.EventHandler(this.stopButton_Click);dem
34、o.Controls.Add(stopButton);closeButton=newButton();closeButton.Location=newSystem.Drawing.Point(850,150);closeButton.Size=newSystem.Drawing.Size(90,25);closeButton.Text="關(guān)閉窗口"closeButton.Click+=newSystem.EventHandler(this.closeButton_Click);demo.Controls.Add(closeButton);explain=newLabel()
35、;explain.AutoSize=true;explain.ForeColor=Color.Red;explain.Location=newSystem.Drawing.Point(180,2);explain.Font=newFont(Font.SystemFontName,13);demo.Controls.Add(explain);Labeltitle=newLabel();title.SetBounds(800,200,200,450);title.Text="冒泡排序(Bubblesort)是基于交換排序的一種算法。它是依次兩兩比較待排序元素;若為逆序(遞增或遞減)則進(jìn)行
36、交換,將待排序元素從左至右比較一遍稱為一趟“冒泡”。每趟冒泡都將待排序列中的最大關(guān)鍵字交換到最后(或最前)位置。直到全部元素有序為止。|改良的排序算法會判斷上一趟比較完后,有沒有數(shù)據(jù)交換,沒有就說明這組數(shù)據(jù)已經(jīng)是有序的,也就不需要繼續(xù)比較.這樣就減少了排序的次數(shù),但是他需要多一個變量來標(biāo)識上一趟的狀態(tài)."title.Font=newFont(Font.SystemFontName,9);title.ForeColor=Color.Red;demo.Controls.Add(title);sign=newLabel();sign.AutoSize=true;sign.Font=newFont(Font.SystemFontName,15);sign.BackColor=Color.YellowGreen;sign.ForeColor=Color.Black;demo.Controls.Add(s
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 勞務(wù)合同補(bǔ)充協(xié)議合同范本
- 單位房屋借用合同范本
- 勞動使用期合同范本
- 利用合同范本掙錢
- 上海徐匯金杯租車合同范本
- 監(jiān)控弱電維護(hù)合同范本
- 醫(yī)院電動車租售合同范本
- 備案的借住合同范本
- 單位之間借支合同范本
- 2003勞務(wù)合同范本
- 2024年湖南環(huán)境生物職業(yè)技術(shù)學(xué)院高職單招職業(yè)技能測驗歷年參考題庫(頻考版)含答案解析
- 《化工流程教案》課件
- 后循環(huán)缺血治療
- 體育學(xué)科核心素養(yǎng)解析
- 2024年浙江紹興杭紹臨空示范區(qū)開發(fā)集團(tuán)有限公司招聘筆試真題
- 2025年體檢科醫(yī)療質(zhì)量控制工作計劃
- 2024年萍鄉(xiāng)衛(wèi)生職業(yè)學(xué)院單招職業(yè)適應(yīng)性測試題庫參考答案
- 飛行器小學(xué)生課件
- 無人機(jī)法律法規(guī)與安全飛行 第2版2-2 領(lǐng)空
- 《單片機(jī)應(yīng)用實訓(xùn)教程》課件第4章
- 應(yīng)急突發(fā)處置
評論
0/150
提交評論