reza mir

reza mir

  • NA
  • 1
  • 0

help: why this code give me wrong answer

Feb 27 2009 6:49 AM
hello i copy this code from one ebook the timing class measure length of algorithm but always the start time(time span) is equal to stop time so ....duraition=start time- stop time==>0 where is the problem [code] using System; using System.Collections; using System.Diagnostics; class class1 { class CArray { private int[] arr; private int upper; private int numElements; public CArray(int size) { arr = new int[size]; upper = size - 1; numElements = 0; } public void Insert(int item) { arr[numElements] = item; numElements++; } public void DisplayElements() { for (int i = 0; i <= upper; i++) Console.Write(arr[i] + " "); Console.Write('\n'); } public void Clear() { for (int i = 0; i <= upper; i++) arr[i] = 0; numElements = 0; } public void SelectionSort() { int min, temp; for (int outer = 0; outer <= upper; outer++) { min = outer; for (int inner = outer + 1; inner <= upper; inner++) if (arr[inner] < arr[min]) min = inner; temp = arr[outer]; arr[outer] = arr[min]; arr[min] = temp; } } } public class Timing { TimeSpan startingTime; TimeSpan duration; TimeSpan test; public Timing() { startingTime = new TimeSpan(0); duration = new TimeSpan(0); } public void StopTime() { test = Process.GetCurrentProcess().Threads[0]. UserProcessorTime; duration = Process.GetCurrentProcess().Threads[0]. UserProcessorTime. Subtract(startingTime); } public void startTime() { GC.Collect(); GC.WaitForPendingFinalizers(); startingTime = Process.GetCurrentProcess().Threads[0]. UserProcessorTime; } public TimeSpan Result() { return duration; } } static void Main() { Timing sortTime = new Timing(); Random rnd = new Random(100); int numItems = 64000; CArray theArray = new CArray(numItems); for (int i = 0; i < numItems; i++) theArray.Insert((int)(rnd.NextDouble() * 100)); sortTime.startTime(); theArray.SelectionSort(); sortTime.StopTime(); } } [/code]

Answers (2)