算法

来源:互联网 发布:手机总是弹出登录网络 编辑:程序博客网 时间:2024/06/03 22:48
[csharp] view plain copy
 print?
  1. // --------------------------------------------------------------------------------------------------------------------  
  2. // <copyright file="Program.cs" company="Chimomo's Company">  
  3. //  
  4. // Respect the work.  
  5. //  
  6. // </copyright>  
  7. // <summary>  
  8. //  
  9. // The bubble sort.  
  10. //  
  11. // 冒泡排序(Bubble Sort),是一种计算机科学领域较简单的排序算法。它重复地走访要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来,直到没有元素再需要交换为止。这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。  
  12. //  
  13. // 冒泡排序的算法如下:  
  14. // 1、对无序区的每一对相邻元素作比较,从开始第一对到最后一对,如果相邻元素对中的第一个比第二个大,就交换他们两个。从而,最后一个元素必定是最大元素,把该最大元素移出无序区。  
  15. // 2、持续对越来越少的无序区元素重复上面的步骤,直到没有任何一对元素需要比较为止。  
  16. //  
  17. // 时间复杂度:  
  18. // 冒泡排序的平均时间复杂度为O(n^2)。  
  19. //  
  20. // 算法稳定性:  
  21. // 冒泡排序就是把大的元素往后调。比较是相邻的两个元素作比较,交换也发生在这两个元素之间。所以,如果两个元素相等,我想你是不会再无聊地把他们俩交换一下的;如果两个相等的元素没有相邻,那么即使通过前面的元素交换把这两个相等相邻起来,这时候也不会交换。所以相同元素的前后顺序并没有改变,所以冒泡排序是一种稳定的排序算法。  
  22. //  
  23. // </summary>  
  24. // --------------------------------------------------------------------------------------------------------------------  
  25.   
  26. namespace CSharpLearning  
  27. {  
  28.     using System;  
  29.   
  30.     /// <summary>  
  31.     /// The program.  
  32.     /// </summary>  
  33.     public static class Program  
  34.     {  
  35.         /// <summary>  
  36.         /// 冒泡排序算法。  
  37.         /// </summary>  
  38.         /// <param name="a">  
  39.         /// 待排序数组。  
  40.         /// </param>  
  41.         private static void BubbleSort(int[] a)  
  42.         {  
  43.             Console.WriteLine("In Bubble Sort:");  
  44.   
  45.             // 外循环是限制一次冒泡排序比较的元素个数。  
  46.             for (int i = a.Length - 1; i >= 1; i--)  
  47.             {  
  48.                 // 内循环比较0到i-1个元素的大小。  
  49.                 for (int j = 0; j <= i - 1; j++)  
  50.                 {  
  51.                     // 排序过程。  
  52.                     if (a[j] > a[j + 1])  
  53.                     {  
  54.                         int t = a[j];  
  55.                         a[j] = a[j + 1];  
  56.                         a[j + 1] = t;  
  57.                     }  
  58.                 }  
  59.   
  60.                 Console.Write("Round {0}: ", a.Length - i);  
  61.                 foreach (int k in a)  
  62.                 {  
  63.                     Console.Write(k + " ");  
  64.                 }  
  65.   
  66.                 Console.WriteLine();  
  67.             }  
  68.         }  
  69.   
  70.         /// <summary>  
  71.         /// The main.  
  72.         /// </summary>  
  73.         public static void Main()  
  74.         {  
  75.             int[] a = { 1, 6, 4, 2, 8, 7, 9, 3, 10, 5 };  
  76.             Console.WriteLine("Before Bubble Sort:");  
  77.             foreach (int i in a)  
  78.             {  
  79.                 Console.Write(i + " ");  
  80.             }  
  81.   
  82.             Console.WriteLine("\r\n");  
  83.             BubbleSort(a);  
  84.             Console.WriteLine("\r\nAfter Bubble Sort:");  
  85.             foreach (int i in a)  
  86.             {  
  87.                 Console.Write(i + " ");  
  88.             }  
  89.   
  90.             Console.WriteLine(string.Empty);  
  91.         }  
  92.     }  
  93. }  
  94.   
  95. // Output:  
  96. /* 
  97. Before Bubble Sort: 
  98. 1 6 4 2 8 7 9 3 10 5 
  99.  
  100. In Bubble Sort: 
  101. Round 1: 1 4 2 6 7 8 3 9 5 10 
  102. Round 2: 1 2 4 6 7 3 8 5 9 10 
  103. Round 3: 1 2 4 6 3 7 5 8 9 10 
  104. Round 4: 1 2 4 3 6 5 7 8 9 10 
  105. Round 5: 1 2 3 4 5 6 7 8 9 10 
  106. Round 6: 1 2 3 4 5 6 7 8 9 10 
  107. Round 7: 1 2 3 4 5 6 7 8 9 10 
  108. Round 8: 1 2 3 4 5 6 7 8 9 10 
  109. Round 9: 1 2 3 4 5 6 7 8 9 10 
  110.  
  111. After Bubble Sort: 
  112. 1 2 3 4 5 6 7 8 9 10 

  1. */  
转自:http://blog.csdn.net/troubleshooter/article/details/4645789

原创粉丝点击