zoukankan      html  css  js  c++  java
  • 冒泡法排序

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


  • 相关阅读:
    第四周作业
    jsp第二次作业
    jsp第一次作业
    软件测试1
    activity
    listview
    sql
    登录
    第二次安卓作业
    安卓第一周作业
  • 原文地址:https://www.cnblogs.com/dongweian/p/7966349.html
Copyright © 2011-2022 走看看