namespace 快速排序 { class Program { static void Main(string[] args) { //利用随机数生产10万个数 Random r = new Random(0); int[] nums = new int[100000]; for (int i = 0; i < nums.Length; i++) { nums[i] = r.Next(); } Stopwatch sp = new Stopwatch(); sp.Start(); // 冒泡 // PopSort(nums); // 快排 QiuckSort(nums, 0, nums.Length - 1); sp.Stop(); Console.WriteLine(sp.Elapsed); Console.ReadKey(); } static void QiuckSort(int[] arr, int lower, int high) { if (lower < high) { var pos = FindPos(arr, lower, high); QiuckSort(arr, lower, pos - 1); QiuckSort(arr, pos + 1, high); } } static int FindPos(int[] arr, int lower, int high) { int temp = arr[lower]; while (lower < high) { while (lower < high && arr[high] >= temp) { high--; } arr[lower] = arr[high]; while (lower < high && arr[lower] <= temp) { lower++; } arr[high] = arr[lower]; } arr[lower] = temp; return lower; } static void PopSort(int[] array) { for (int i = 0; i < array.Length - 1; i++) { for (int j = 0; j < array.Length - i - 1; j++) { if (array[j] > array[j + 1]) { int t = array[j]; array[j] = array[j + 1]; array[j + 1] = t; } } } } } }
浙公网安备 33010602011771号