help: why this code give me wrong answer

csharp , timing class Tulsa, United States
  • 8 years ago
    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 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(); } }
  • 8 years ago
    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(); } }

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”