【简单算法】快速排序、斐波拉切缓存
1. 快速排序:
1 private static void FastSort(int[] data, int low, int high) 2 { 3 int i = low; 4 int j = high; 5 int mid = data[(low + high) / 2]; 6 do 7 { 8 while (data[i] < mid && i < high) 9 { 10 i++; 11 } 12 while (data[j] > mid && j > low) 13 { 14 j--; 15 } 16 if (i <= j) 17 { 18 int temp = data[i]; 19 data[i] = data[j]; 20 data[j] = temp; 21 i++; 22 j--; 23 } 24 } while (i <= j); 25 if (i < high) 26 { 27 FastSort(data, i, high); 28 } 29 if (j > low) 30 { 31 FastSort(data, low, j); 32 } 33 }
2. 斐波拉切缓存:
1 class Phabe 2 { 3 private static List<long> phabeList; 4 static Phabe() 5 { 6 phabeList = new List<long>(); 7 phabeList.Add(1); 8 phabeList.Add(1); 9 } 10 private static void CalcTo(int to) 11 { 12 while (phabeList.Count < to) 13 { 14 int count = phabeList.Count; 15 long temp = phabeList[count - 1] + phabeList[count - 2]; 16 phabeList.Add(temp);//缓存计算结果 17 } 18 } 19 public static long Get(int index) 20 { 21 if (index <= 0) 22 { 23 return 0; 24 } 25 if (phabeList.Count < index) 26 { 27 CalcTo(index); 28 } 29 //Console.WriteLine("Total: " + phabeList.Count); 30 return phabeList[index-1]; 31 } 32 }