1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| 1 public class QuickFindAlgorithm 2 { 3 public static void TestRandomizedQuickFind() 4 { 5 int[] unsorted = 6 { 7 4, 1, 5, 2, 6, 3, 7, 9, 8, 0 8 }; 9 10 Console.WriteLine("Find Value : {0}", 11 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 1)); 12 Console.WriteLine("Find Value : {0}", 13 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 2)); 14 Console.WriteLine("Find Value : {0}", 15 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 3)); 16 Console.WriteLine("Find Value : {0}", 17 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 4)); 18 Console.WriteLine("Find Value : {0}", 19 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 5)); 20 Console.WriteLine("Find Value : {0}", 21 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 6)); 22 Console.WriteLine("Find Value : {0}", 23 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 7)); 24 Console.WriteLine("Find Value : {0}", 25 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 8)); 26 Console.WriteLine("Find Value : {0}", 27 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 9)); 28 Console.WriteLine("Find Value : {0}", 29 RandomizedQuickFind(unsorted, 0, unsorted.Length - 1, 10)); 30 31 int median = RandomizedQuickFind(unsorted, 32 0, unsorted.Length - 1, (unsorted.Length + 1) / 2); 33 Console.WriteLine("Find Median : {0}", median); 34 35 Console.Read(); 36 } 37 38 static int RandomizedQuickFind(int[] a, int p, int r, int i) 39 { 40 if (p == r) 41 return a[p]; 42 43 int q = RandomizedPartition(a, p, r); 44 int k = q - p + 1; 45 46 if (i == k) // the pivot value is the answer 47 { 48 return a[q]; 49 } 50 else if (i < k) // i is in left side 51 { 52 return RandomizedQuickFind(a, p, q - 1, i); 53 } 54 else // i is in right side 55 { 56 return RandomizedQuickFind(a, q + 1, r, i - k); 57 } 58 } 59 60 static void RandomizedQuickSort(int[] unsorted, int left, int right) 61 { 62 if (!(left < right)) return; 63 64 int pivotIndex = RandomizedPartition(unsorted, left, right); 65 66 RandomizedQuickSort(unsorted, left, pivotIndex - 1); 67 RandomizedQuickSort(unsorted, pivotIndex + 1, right); 68 } 69 70 static int RandomizedPartition(int[] unsorted, int left, int right) 71 { 72 int i = random.Next(left, right); 73 Swap(unsorted, i, right); 74 return Partition(unsorted, left, right); 75 } 76 77 static int Partition(int[] unsorted, int left, int right) 78 { 79 int pivotIndex = right; 80 81 // 哨兵 82 int sentinel = unsorted[right]; 83 84 // 子数组长度为 right - left + 1 85 int i = left - 1; 86 for (int j = left; j <= right - 1; j++) 87 { 88 if (unsorted[j] <= sentinel) 89 { 90 i++; 91 Swap(unsorted, i, j); 92 } 93 } 94 95 Swap(unsorted, i + 1, pivotIndex); 96 97 return i + 1; 98 } 99 100 static void Swap(int[] unsorted, int i, int j) 101 { 102 int temp = unsorted[i]; 103 unsorted[i] = unsorted[j]; 104 unsorted[j] = temp; 105 } 106 107 static Random random = new Random(new Guid().GetHashCode()); 108 }
|