快速排序与冒泡排序性能对比(多线程计时)

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

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

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