S
StudiousStudent
Guest
So I'm trying to time thi recursive function. sorted is a list. I'm wondering how I would do this for each iteration.
Stopwatch stopWatch = new Stopwatch();
//code here to build the input array. For each iteration stopWatch.Start();
sorted = MergeSort(unsorted); stopWatch.Stop();
// Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime: {0} Elements {1} ",elapsedTime, bound);
stopWatch.Reset();
Merge Sort Functions
private static List<int> MergeSort(List<int> unsorted)
{
if (unsorted.Count <= 1)
return unsorted;
List<int> left = new List<int>();
List<int> right = new List<int>();
int middle = unsorted.Count / 2;
for (int j = 0; j < unsorted.Count; j++)
{
for (int i = 0; i < middle; i++) // Dividing the unsorted list
{
left.Add(unsorted);
}
for (int i = middle; i < unsorted.Count; i++)
{
right.Add(unsorted);
}
}
left = MergeSort(left);
right = MergeSort(right);
return Merge(left, right);
}
private static List<int> Merge(List<int> left, List<int> right)
{
List<int> result = new List<int>();
while (left.Count > 0 || right.Count > 0)
{
if (left.Count > 0 && right.Count > 0)
{
if (left.First() <= right.First()) // Comparing First two elements to see which is smaller
{
result.Add(left.First());
left.Remove(left.First()); // Rest of the list minus the first element
}
else
{
result.Add(right.First());
right.Remove(right.First());
}
}
else if (left.Count > 0)
{
result.Add(left.First());
left.Remove(left.First());
}
else if (right.Count > 0)
{
result.Add(right.First());
right.Remove(right.First());
}
}
return result;
}
StudiousStudent
Continue reading...
Stopwatch stopWatch = new Stopwatch();
//code here to build the input array. For each iteration stopWatch.Start();
sorted = MergeSort(unsorted); stopWatch.Stop();
// Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime: {0} Elements {1} ",elapsedTime, bound);
stopWatch.Reset();
Merge Sort Functions
private static List<int> MergeSort(List<int> unsorted)
{
if (unsorted.Count <= 1)
return unsorted;
List<int> left = new List<int>();
List<int> right = new List<int>();
int middle = unsorted.Count / 2;
for (int j = 0; j < unsorted.Count; j++)
{
for (int i = 0; i < middle; i++) // Dividing the unsorted list
{
left.Add(unsorted);
}
for (int i = middle; i < unsorted.Count; i++)
{
right.Add(unsorted);
}
}
left = MergeSort(left);
right = MergeSort(right);
return Merge(left, right);
}
private static List<int> Merge(List<int> left, List<int> right)
{
List<int> result = new List<int>();
while (left.Count > 0 || right.Count > 0)
{
if (left.Count > 0 && right.Count > 0)
{
if (left.First() <= right.First()) // Comparing First two elements to see which is smaller
{
result.Add(left.First());
left.Remove(left.First()); // Rest of the list minus the first element
}
else
{
result.Add(right.First());
right.Remove(right.First());
}
}
else if (left.Count > 0)
{
result.Add(left.First());
left.Remove(left.First());
}
else if (right.Count > 0)
{
result.Add(right.First());
right.Remove(right.First());
}
}
return result;
}
StudiousStudent
Continue reading...