快速排序与冒泡排序效率对比

  1 //C# Code
  2 
  3 using System;
  4 using System.Collections.Generic;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Timers;
  8 
  9  
 10 
 11 namespace QuickSort
 12 {
 13     class Program
 14     {
 15         private static int UsedTime = 0;
 16         private static Random Seed = new Random();//Random Seed
 17 
 18  
 19 
 20         /// <summary>
 21         /// SortMethod
 22         /// </summary>
 23         /// <param name="arr">NumArray</param>
 24         /// <param name="begin">FirstIndex</param>
 25         /// <param name="end">LastIndex</param>
 26         /// <param name="isRandomKey">IsRandomKey</param>
 27         private static void QuickSort(int[] arr,int begin,int end,bool isRandomKey)
 28         {
 29             //The leftindex less than the rightindex,the sorting isn't complete.
 30             if (begin < end)
 31             {
 32                 int i = begin - 1, j = end + 1, key;
 33                 if (!isRandomKey)
 34                     key = arr[(begin + end) / 2];
 35                 else
 36                     key = arr[Seed.Next(begin, end)];
 37 
 38                 while (true)
 39                 {
 40                     while (i < end && arr[++i] < key) ;
 41                     while (j > 0 && arr[--j] > key) ;
 42 
 43                     if (i >= j)
 44                         break;
 45                     Swap(arr, i, j);
 46                 }
 47                 QuickSort(arr, begin, i - 1,isRandomKey);//Left part recursion.
 48                 QuickSort(arr, j + 1, end,isRandomKey);//Right part recursion.
 49             }
 50         }//end SortMethod
 51 
 52         /// <summary>
 53         /// SortMethod
 54         /// </summary>
 55         /// <param name="arr">NumArray</param>
 56         private static void BubbleSort(int[] arr)
 57         {
 58             int i,j;
 59             for (i = 0; i < arr.Length - 1; i++)
 60             {
 61                 for (j = 0; j < arr.Length - 1 - i; j++)
 62                 {
 63                     if (arr[j] > arr[j + 1])
 64                     {
 65                         Swap(arr,j,j+1);
 66                     }
 67                 }
 68             }
 69         }//end SortMethod
 70 
 71         /// <summary>
 72         /// Swap variables' values
 73         /// </summary>
 74         /// <param name="arr">Array</param>
 75         /// <param name="i">Current left index</param>
 76         /// <param name="j">Current right index</param>
 77         private static void Swap(int[] arr, int i, int j)
 78         {
 79             int temp = arr[i];
 80             arr[i] = arr[j];
 81             arr[j] = temp;
 82         }//end method
 83 
 84         //TimerEventMethod
 85         private static void TimerEvent(object source, ElapsedEventArgs e)
 86         {
 87             UsedTime++;
 88         }
 89 
 90  
 91 
 92         static void Main(string[] args)
 93         {
 94             int ArrLen = 0;//Array length
 95 Reinput:
 96             Console.Write("Please input array length:");
 97             if (!int.TryParse(Console.ReadLine(), out ArrLen))
 98             {
 99                 Console.WriteLine("Error input !");
100                 goto Reinput;
101             }
102 
103 
104             int[] NumArr = new int[ArrLen];
105             int[] NumArr_2 = new int[NumArr.Length];
106 
107 
108             Console.WriteLine("Assignment values ...");
109             for (int i = 0; i < NumArr.Length; i++)//Assign values
110             {
111                 NumArr[i] = Seed.Next(-1000, 1000);
112             }
113             Console.WriteLine("Assignment to complete.");
114 
115  
116 
117             Console.WriteLine("\nCopying array to array2 ...");
118             for (int i = 0; i < NumArr.Length; i++)//Copy array
119             {
120                 NumArr_2[i] = NumArr[i];
121             }
122             Console.WriteLine("Copy completed.");
123 
124  
125 
126             /*//Cancel the notes to output datas
127             Console.Write("Before Sort:");
128             foreach(int a in NumArr)
129                 Console.Write("{0} ",a);
130              */
131 
132 
133             Timer t = new Timer();//Create a timer to calculate used time.
134             t.Elapsed += new ElapsedEventHandler(TimerEvent);
135             t.Interval = 1;
136             UsedTime = 0;
137             Console.Write("\nBegin QuickSorting , please wait ......");
138             t.Start();
139             QuickSort(NumArr, 0, NumArr.Length - 1, false);
140             t.Stop();
141             t.Close();
142 
143 
144             /*//Cancel the notes to output datas
145             Console.Write("\n\n\nAfter Sorted:");
146             foreach(int a in NumArr)
147                 Console.Write("{0} ",a);
148              */
149             Console.WriteLine("\nQuickSorting is Complete. Used time : {0} msec.",UsedTime);
150 
151  
152 
153             t = new Timer();//Create a timer to calculate used time.
154             t.Elapsed += new ElapsedEventHandler(TimerEvent);
155             t.Interval = 1;
156             UsedTime = 0;
157             Console.Write("\nBegin BubbleSorting , please wait ......");
158             //Console.Write("\nBegin RandomQuickSorting ......");
159             t.Start();
160             BubbleSort(NumArr_2);
161             //QuickSort(NumArr_2, 0, NumArr_2.Length - 1, true);
162             t.Stop();
163             t.Close();
164 
165 
166             /*//Cancel the notes to output datas
167             Console.Write("\n\n\nAfter Sorted:");
168             foreach(int a in NumArr_2)
169                 Console.Write("{0} ",a);
170              */
171             Console.WriteLine("\nBubbleSorting is Complete. Used time : {0} msec.",UsedTime);
172             //Console.WriteLine("\nRandomQuickSorting is Complete. Used time : {0} msec.",UsedTime);
173 
174             Console.ReadLine();
175         }//end Main()
176     }//end class
177 }//end namespace

//欢迎转载,请注明原创,感谢

Never give up!
原文地址:https://www.cnblogs.com/CoffeeMX/p/2610499.html