冒泡演算法:先看代碼吧,我不喜歡先說一大堆,看不懂了再說
1 class Program 2 3 { 4 static void Main(string[] args) 5 { 6 int[] arr = { 8, 15, 16, 11, 99, 4 }; 7 for (int i = 0; i <arr.Length-1 ; i++) 8 { 9 for (int j = 0; j <arr.Length-1-i; j++) 10 { 11 if (arr[j]<arr[j+1]) 12 { 13 int t = arr[j]; 14 arr[j] = arr[j+1]; 15 arr[j + 1] = t; 16 } 17 } 18 } 19 foreach (int a in arr) 20 { 21 Console.WriteLine(a); 22 } 23 } 24 25 }
第一個回圈控制次數
第二排序內的數字 -1是為了不越界
在判斷如果數大于則交換位子
用兩個回圈嵌套,再用第三個變數接受交換,然后遍歷陣列
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/103514.html
標籤:C#
上一篇:Winform中封裝DevExpress的MarqueeProgressBarComtrol實作彈窗式進度條效果
