Friday, February 26, 2016

CS 2420 - Assignment 4 - Part 5

So now that I am done with the List class, I have moved on to another part of the assignment. This time I have to plot the running times of my sorts.

After thinking about it for a while, I decided that I would create a new class that would just be called from the Main function and it would sort lists of different sizes and it would output the time in milliseconds to a text file. This would make it easier to actually make a graph for each sort. In a graph, X would be the number of elements in the list and Y would be the time it took to sort the list.

Here is what I have for this new class:

using System;
using System.Diagnostics;
using System.IO;

class RunningTimes
{
    public static void SortPlot(ISorter sorter, int elements, string name)
    {
        Random randint = new Random();
        Stopwatch watch = new Stopwatch();
        StreamWriter file = new StreamWriter(@"ChristianMunoz_RunningTimes.txt", true);
        long time = watch.ElapsedMilliseconds;
        int[] list;

        while(elements > 0)
        {
            list = new int[elements];

            for (int i = 0; i < list.Length; i++)
                list[i] = randint.Next(1, 200);

            watch.Reset();
            watch.Start();
            sorter.sort(list, 0, list.Length);
            watch.Stop();
            time = watch.ElapsedMilliseconds;
            Console.WriteLine(time);

            file.WriteLine("Sort: {0} Element count: {1} Time in Milliseconds: {2}", name, elements, time);

            elements = elements - 10000;
        }
        file.WriteLine(" ");
        file.Close();
    }
}

In the part that says: @"ChristianMunoz_RunningTimes.txt" that is the location and name of the text file I'm creating. You can change the name to anything you want, and you can find the file in your pc by first going to the folder where you have your C# work: Documents\CSharp\DataStructuresAndSorts\Assignment4\bin\Debug. Or you can also replace the name and tell Visual Studio a specific location by pasting in a path that you want.

This is how I call the function from my main:

RunningTimes.SortPlot(qSorter, 70000, "Quick Sort");

Since this new class isn't doing anything special, I don't have to create a new instance of the class and I can just call it from my other file. In the above example: I pass in a Quick Sort object, I tell it how big I want my list to be (in this case it will be 70000 integers), and just for kicks I pass in a string to make it easier to read the txt file of the sort that I am on. The output of the file will look like this:

Quick Sort Element count: 70000 Time in Milliseconds: 93
Quick Sort Element count: 60000 Time in Milliseconds: 72
Quick Sort Element count: 50000 Time in Milliseconds: 48
Quick Sort Element count: 40000 Time in Milliseconds: 37
Quick Sort Element count: 30000 Time in Milliseconds: 20
Quick Sort Element count: 20000 Time in Milliseconds: S
Quick Sort Element count: 10000 Time in Milliseconds: 2

That's all for now, thanks for reading!

No comments:

Post a Comment