码迷,mamicode.com
首页 > 编程语言 > 详细

冒泡法排序

时间:2017-12-03 17:16:04      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:步骤   isp   思路   bsp   技术分享   最大的   write   logs   冒泡   

 /*------------------------------------冒泡法排序的基本思路------------------------------------
 * 1:拿数组的第一个元素intTest[0]的值循环与剩余的元素比较,把最大的值与intTest[0]交换值,然后剩余的元素依次比较
 ,即可得到从大到小排序的数组。由此可知,数组长度为n,至少要经过的计算步骤f(n):f(n)=f(n-1)+(n-1)。其中n>=1,f(1)=0
 */
1
namespace TestAppliacion 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 int[] intTest=new int[]{2,5,7,65,1}; 8 9 TestMath(intTest); 10 Console.ReadKey(); 11 } 12 // 13 static void TestMath(int[] intTest) 14 { 15 //排序前 16 string strBefore = null; 17 foreach (int a in intTest) 18 { 19 strBefore += a.ToString() + " "; 20 } 21 Console.WriteLine("排序前:" + strBefore); 22 //冒泡法排序 23 int n = 0;//排序总步骤 24 for (int i = 0; i < intTest.Length-1; i++) 25 { 26 for (int j = 1+i; j < intTest.Length; j++) 27 { 28 if (intTest[i] < intTest[j]) 29 { 30 int temp = intTest[i]; 31 intTest[i] = intTest[j]; 32 intTest[j] = temp; 33 } 34 n++; 35 } 36 } 37 //排序后 38 string strAfter = null; 39 foreach (int a in intTest) 40 { 41 strAfter += a.ToString() + " "; 42 } 43 Console.WriteLine("排序后:" + strAfter); 44 Console.WriteLine("冒泡法排序需进行 {0} 次排序", n); 45 } 46 } 47 }

 技术分享图片

 

冒泡法排序

标签:步骤   isp   思路   bsp   技术分享   最大的   write   logs   冒泡   

原文地址:http://www.cnblogs.com/dongweian/p/7966349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!