冒泡法排序

Posted 猿来到此

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了冒泡法排序相关的知识,希望对你有一定的参考价值。

 /*------------------------------------冒泡法排序的基本思路------------------------------------
 * 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 }


 

以上是关于冒泡法排序的主要内容,如果未能解决你的问题,请参考以下文章

冒泡排序法

java冒泡排序法代码

c语言冒泡排序法代码一直排序错误,有时只能排前两个,不明白原因,请问究竟哪里写错了,谢谢!

C语言——如何有效记忆冒泡排序法?

数组冒泡排序选择排序二分查找法

JAVA 冒泡排序法的详细解释是啥?