static void Main(string[] args) { Stopwatch timer = new Stopwatch(); int n = 10, k = 50, re; LinearSearchList <int> sl = new LinearSearchList <int>(n + 8); RandomizData(sl, n); sl[8] = 50; Console.Write("随机排列: "); sl.Show(true); timer.Start(); sl.Sort(); timer.Stop(); Console.Write("排序后: "); sl.Show(true); Console.WriteLine("Elapsed time = {0} Ticks", timer.ElapsedTicks); timer.Start(); re = sl.BinarySearch(k, 0, n); timer.Stop(); Console.WriteLine("二分法:k={0}, re={1}, i={2}", k, re, ~re); Console.WriteLine("Elapsed time = {0} Ticks", timer.ElapsedTicks); ///两种方法在有数据被找到时查找不一致 timer.Start(); re = sl.SequencedSearch(k, 0, n); timer.Stop(); Console.WriteLine("顺序法:k={0}, re={1}, i={2}", k, re, ~re); Console.WriteLine("Elapsed time = {0} Ticks", timer.ElapsedTicks); }
public static void RandomizData(LinearSearchList <int> sl, int n) { int k; Random random = new Random(); for (int i = 0; i < n; i++) { k = random.Next(100); sl.Add(k); } }
public static void Main(string[] args) { int[] datas = { 10, 20, 50, 100, 12, 35, 41, 173, 26, 554, 12, 365, 412, 32 }; LinearSearchList <int> linearSearch = new LinearSearchList <int>(datas); linearSearch.Show(); Console.WriteLine(); int findvalue = 168; Console.WriteLine("{0} is {1} the set.", findvalue, (linearSearch.indexOf(findvalue) > 0) ? "in" : "not in"); findvalue = 173; Console.WriteLine("{0} is {1} the set.", findvalue, (linearSearch.BinarySearch(findvalue) > 0) ? "in" : "not in"); Console.WriteLine(); Console.WriteLine("after sorted:"); linearSearch.Show(false); }