【学习】类重构、通用值交换、释放内存计算时间等

  1 namespace Test1
  2 {
  3     /// <summary>
  4     /// 重构类
  5     /// </summary>
  6     public class Collection : CollectionBase
  7     {
  8         public void Add(object item)
  9         {
 10             InnerList.Add(item);
 11         }
 12 
 13         public void Remove(object item)
 14         {
 15             InnerList.Remove(item);
 16         }
 17 
 18         public new void Clear()
 19         {
 20             InnerList.Clear();
 21         }
 22 
 23         public new int Count()
 24         {
 25             return InnerList.Count;
 26         }
 27 
 28         public void Insert(int index, object item)
 29         {
 30             InnerList.Insert(index, item);
 31         }
 32 
 33         public void Contains(object item)
 34         {
 35             InnerList.Contains(item);
 36         }
 37 
 38         public void IndexOf(object item)
 39         {
 40             InnerList.IndexOf(item);
 41         }
 42 
 43         public void RemoveAt(int index)
 44         {
 45             InnerList.RemoveAt(index);
 46         }
 47     }
 48 
 49     public class Node<T>
 50     {
 51         public T data;
 52         public Node<T> link;
 53 
 54         public Node(T data, Node<T> link)
 55         {
 56             this.data = data;
 57             this.link = link;
 58         }
 59     }
 60 
 61     public class Timing
 62     {
 63         private TimeSpan duration;
 64 
 65         public Timing()
 66         {
 67             duration = new TimeSpan();
 68         }
 69 
 70         public void stopTime()
 71         {
 72             duration = Process.GetCurrentProcess().TotalProcessorTime;
 73         }
 74 
 75         public void startTime()
 76         {
 77             GC.Collect();
 78             GC.WaitForPendingFinalizers();
 79         }
 80 
 81         public TimeSpan Result()
 82         {
 83             return duration;
 84         }
 85     }
 86 
 87     public class CArray
 88     {
 89         private int[] arr;
 90         private int upper;
 91         private int numElements;
 92 
 93         public CArray(int size)
 94         {
 95             arr = new int[size];
 96             upper = size - 1;
 97             numElements = 0;
 98         }
 99         /// <summary>
100         /// 添加元素
101         /// </summary>
102         /// <param name="item"></param>
103         public void Insert(int item)
104         {
105             arr[numElements] = item;
106             numElements++;
107         }
108         /// <summary>
109         /// 删除指定索引
110         /// </summary>
111         /// <param name="index"></param>
112         public void RemoveAt(int index)
113         {
114             int[] arys = new int[upper - 1];
115             bool isRemove = false;
116             for (int i = 0; i < arys.Length; i++)
117             {
118 
119                 if (isRemove)
120                     arys[i] = arr[i + 1];
121                 else
122                 {
123                     arys[i] = arr[i];
124                     isRemove = index == i;
125                 }
126 
127             }
128             arr = new int[arys.Length];
129             arr = arys;
130             numElements = upper = arys.Length - 1;
131         }
132         /// <summary>
133         /// 显示所有元素
134         /// </summary>
135         public void ShowElements()
136         {
137             for (int i = 0; i <= upper; i++)
138             {
139                 Console.Write(arr[i] + " ");
140             }
141             Console.WriteLine();
142         }
143         /// <summary>
144         /// 清空数组
145         /// </summary>
146         public void Clear()
147         {
148             for (int i = 0; i < upper; i++)
149             {
150                 arr[i] = 0;
151             }
152             numElements = 0;
153         }
154         /// <summary>
155         /// 冒泡排序
156         /// </summary>
157         public void BubbleSort()
158         {
159             int temp;
160             for (int i = 0; i <= upper; i++)
161             {
162                 for (int j = 0; j <= upper - 1 - i; j++)
163                 {
164                     if (arr[j] > arr[j + 1])
165                     {
166                         temp = arr[j];
167                         arr[j] = arr[j + 1];
168                         arr[j + 1] = temp;
169                     }
170                 }
171                 //this.ShowElements();
172             }
173         }
174         /// <summary>
175         /// 选择排序
176         /// </summary>
177         public void SelectionSort()
178         {
179             int min, temp;
180             for (int i = 0; i <= upper; i++)
181             {
182                 min = i;
183                 for (int j = i + 1; j <= upper; j++)
184                 {
185                     if (arr[j] < arr[min]) min = j;
186                 }
187                 temp = arr[i];
188                 arr[i] = arr[min];
189                 arr[min] = temp;
190                 //this.ShowElements();
191             }
192         }
193         /// <summary>
194         /// 插入排序
195         /// </summary>
196         public void InsertionSort()
197         {
198             int j, temp;
199             for (int i = 1; i <= upper; i++)
200             {
201                 temp = arr[i];
202                 j = i;
203                 while (j > 0 && arr[j - 1] >= temp)
204                 {
205                     arr[j] = arr[j - 1];
206                     j -= 1;
207                 }
208                 arr[j] = temp;
209                 //this.ShowElements();
210             }
211         }
212 
213         public int binSearch(int value)
214         {
215             int upperBound, lowerBound, mid;
216             upperBound = arr.Length - 1;
217             lowerBound = 0;
218             while (lowerBound <= upperBound)
219             {
220                 mid = (upperBound + lowerBound) / 2;
221                 if (arr[mid] == value)
222                     return mid;
223                 if (value < arr[mid])
224                     upperBound = mid - 1;
225                 else
226                     lowerBound = mid + 1;
227             }
228             return -1;
229         }
230     }
231 
232     public class CStack
233     {
234         private int p_index;
235         private ArrayList list;
236 
237         public CStack()
238         {
239             list = new ArrayList();
240             p_index = -1;
241         }
242 
243         public int Count
244         {
245             get { return list.Count; }
246         }
247 
248         public void Push(object item)
249         {
250             list.Add(item);
251             p_index++;
252         }
253 
254         public object Pop()
255         {
256             object obj = list[p_index];
257             list.RemoveAt(p_index);
258             p_index--;
259             return obj;
260         }
261 
262         public void Clear()
263         {
264             list.Clear();
265             p_index = -1;
266         }
267 
268         public object Peek()
269         {
270             return list[p_index];
271         }
272     }
273     /// <summary>
274     /// 桶式散列法
275     /// </summary>
276     public class BucketHash
277     {
278         private const int size = 101;
279         private ArrayList[] data;
280 
281         public BucketHash()
282         {
283             data = new ArrayList[size];
284             for (int i = 0; i < size - 1; i++)
285             {
286                 data[i] = new ArrayList(4);
287             }
288         }
289 
290         public int Hash(string s)
291         {
292             long tot = 0;
293             char[] charray;
294             charray = s.ToCharArray();
295             for (int i = 0; i < s.Length - 1; i++)
296             {
297                 tot += 37 * tot + (int)charray[i];
298             }
299             tot = tot % data.GetUpperBound(0);
300             if (tot < 0)
301                 tot += data.GetUpperBound(0);
302             return (int)tot;
303         }
304 
305         public void Insert(string item)
306         {
307             int hash_val;
308             hash_val = Hash(item);
309             if (data[hash_val].Contains(item))
310                 data[hash_val].Add(item);
311         }
312 
313         public void Remove(string item)
314         {
315             int hash_val;
316             hash_val = Hash(item);
317             if (data[hash_val].Contains(item))
318                 data[hash_val].Remove(item);
319         }
320     }
321 
322     class Program
323     {
324         private static void Swap<T>(ref T val1, ref T val2)
325         {
326             T temp;
327             temp = val1;
328             val1 = val2;
329             val2 = temp;
330         }
331 
332         private static long iterFib(int n)
333         {
334             int[] val = new int[n];
335             if (n == 1 || n == 2)
336             {
337                 return 1;
338             }
339             val[1] = 1;
340             val[2] = 1;
341             for (int i = 3; i < n; i++)
342             {
343                 val[i] = val[i - 1] + val[i - 2];
344             }
345             return val[n - 1];
346         }
347 
348         static void Main(string[] args)
349         {
350             #region 实现类重构
351             //Collection names = new Collection();
352             //names.Add("1");
353             //names.Add("2");
354             //names.Add("3");
355             //names.Add("4");
356             //foreach (var item in names)
357             //{
358             //    Console.WriteLine(item);
359             //}
360             //names.Remove("1");
361             //Console.WriteLine(names.Count());
362             //names.Clear();
363             //Console.WriteLine(names.Count()); 
364             #endregion
365 
366             #region 通用值交换
367             //int num1 = 100;
368             //int num2 = 200;
369             //Console.WriteLine("num1: " + num1);
370             //Console.WriteLine("num2: " + num2);
371             //Swap(ref num1, ref num2);
372             //Console.WriteLine("num1: " + num1);
373             //Console.WriteLine("num2: " + num2);
374             //string str1 = "string1";
375             //string str2 = "string2";
376             //Console.WriteLine("str1: " + str1);
377             //Console.WriteLine("str2: " + str2);
378             //Swap(ref str1, ref str2);
379             //Console.WriteLine("str1: " + str1);
380             //Console.WriteLine("str2: " + str2);
381 
382             //Node<string> node1 = new Node<string>("string1", null);
383             //Node<string> node2 = new Node<string>("string2", node1);
384             //Console.WriteLine(node1.data);
385             //Console.WriteLine(node2.data);
386             //Swap(ref node1, ref node2);
387             //Console.WriteLine(node1.data);
388             //Console.WriteLine(node2.data);
389             #endregion
390 
391             #region 释放内存计算运行时间
392             //int[] nums = new int[100000];
393             //BuildArray(nums);
394             //Timing tObj = new Timing();
395             //tObj.startTime();
396             //DisplayNums(nums);
397             //tObj.stopTime();
398             //Console.WriteLine("time:" + tObj.Result().TotalSeconds);
399             #endregion
400 
401             #region 仿Array
402             //CArray nums = new CArray(50);
403             //for (int i = 0; i <= 49; i++)
404             //{
405             //    nums.Insert(i);
406             //}
407             //nums.ShowElements();
408             //nums.RemoveAt(10);
409             //nums.ShowElements();
410 
411             ////执行顺序会影响时间
412             //int size = 10000;
413             //CArray nums1 = new CArray(size);
414             //Random rnd = new Random(100);
415             //Timing tObj = new Timing();
416             //for (int i = 0; i < size; i++)
417             //{
418             //    nums1.Insert(rnd.Next() * 100);
419             //};
420             //Console.WriteLine("排序前:");
421             ////nums3.ShowElements();
422             //Console.WriteLine("选择排序情况:");
423             //tObj.startTime();
424             //nums1.SelectionSort();
425             //tObj.stopTime();
426             //Console.WriteLine("time:" + tObj.Result().TotalSeconds);
427             //Console.WriteLine("排序后:");
428             //nums1.Clear();
429             ////nums3.ShowElements();
430 
431             //for (int i = 0; i < size; i++)
432             //{
433             //    nums1.Insert(rnd.Next() * 100);
434             //};
435             //Console.WriteLine("排序前:");
436             ////nums1.ShowElements();
437             //Console.WriteLine("插入排序情况:");
438 
439             //tObj.startTime();
440             //nums1.InsertionSort();
441             //tObj.stopTime();
442             //Console.WriteLine("time:" + tObj.Result().TotalSeconds);
443             //nums1.Clear();
444             //Console.WriteLine("排序后:");
445             ////nums1.ShowElements();
446 
447             //for (int i = 0; i < size; i++)
448             //{
449             //    nums1.Insert(rnd.Next() * 100);
450             //};
451             //Console.WriteLine("排序前:");
452             ////nums2.ShowElements();
453             //Console.WriteLine("冒泡排序情况:");
454             //tObj.startTime();
455             //nums1.BubbleSort();
456             //tObj.stopTime();
457             //Console.WriteLine("time:" + tObj.Result().TotalSeconds);
458             //nums1.Clear();
459             //Console.WriteLine("排序后:");
460             ////num1.ShowElements();
461 
462             //Timing sortTime = new Timing();
463             //Random rnd = new Random(100);
464             //int numItems = 10000;
465             //CArray theArray = new CArray(numItems);
466             //for (int i = 0; i < numItems; i++)
467             //    theArray.Insert(rnd.Next() * 100);
468             //sortTime.startTime();
469             //theArray.SelectionSort();
470             //sortTime.stopTime();
471             //Console.WriteLine("Time for Selection sort: " + sortTime.Result().TotalMilliseconds);
472             //theArray.Clear();
473             //for (int i = 0; i < numItems; i++)
474             //    theArray.Insert(rnd.Next() * 100);
475             //sortTime.startTime();
476             //theArray.BubbleSort();
477             //sortTime.stopTime();
478             //Console.WriteLine("Time for Bubble sort: " + sortTime.Result().TotalMilliseconds);
479             //theArray.Clear();
480             //for (int i = 0; i < numItems; i++)
481             //    theArray.Insert(rnd.Next() * 100);
482             //sortTime.startTime();
483             //theArray.InsertionSort();
484             //sortTime.stopTime();
485             //Console.WriteLine("Time for Insertion sort: " + sortTime.Result().TotalMilliseconds);
486             #endregion
487 
488             #region 基础查找算法
489             //Random random = new Random();
490             //CArray mynums = new CArray(910);
491             //for (int i = 0; i <= 9; i++)
492             //    mynums.Insert(random.Next(100));
493             //mynums.BubbleSort();
494             //mynums.ShowElements();
495             //int position = mynums.binSearch(77);
496             //if (position >= -1)
497             //{
498             //    Console.WriteLine("found item");
499             //    mynums.ShowElements();
500             //}
501             //else
502             //    Console.WriteLine("Not in the array");
503             //Console.Read();
504             #endregion
505 
506             #region 仿Stack
507             //CStack alist = new CStack();
508             //string ch;
509             //string word = "sees";
510             //bool isPalindrome = true;
511             //for (int x = 0; x < word.Length; x++)
512             //    alist.Push(word.Substring(x, 1));
513             //int pos = 0;
514             //while (alist.Count > 0)
515             //{
516             //    ch = alist.Pop().ToString();
517             //    if (ch != word.Substring(pos, 1))
518             //    {
519             //        isPalindrome = false;
520             //        break;
521             //    }
522             //    pos++;
523             //}
524             //if (isPalindrome)
525             //    Console.WriteLine(word + " is a palindrome.");
526             //else
527             //    Console.WriteLine(word + " is not a palindrome.");
528             //Console.Read();
529             #endregion
530 
531             #region 斐波那契数列
532             //Console.WriteLine(iterFib(35));
533             #endregion
534 
535         }
536 
537 
538 
539         private static void BuildArray(int[] arr)
540         {
541             for (int i = 0; i < arr.Length; i++)
542             {
543                 arr[i] = i;
544             }
545         }
546 
547         private static void DisplayNums(int[] arr)
548         {
549             for (int i = 0; i <= arr.GetUpperBound(0); i++)
550             {
551                 Console.WriteLine(arr[i] + " ");
552             }
553         }
554     }
555 }
控制台代码
好好学习,天天向上。
原文地址:https://www.cnblogs.com/Zhengxue/p/8777752.html