using System.Diagnostics;
const String separator = "; ";
public String GenerateSortedArray(Int32 length)
intArray = new Int32[length];
for (Int32 i = 0; i < length; i++)
return "Created sorted array of length: " + length;
public String GenerateRandomArray(Int32 length)
Random random = new Random();
intArray = new Int32[length];
for (Int32 i = 0; i < length; i++)
intArray[i] = random.Next(length);
return "Created random array of length: " + length;
public String SequentialSearch(Int32 repeats)
if (intArray == null || intArray.Length == 0)
return "There is no array to search in.";
Random random = new Random();•
Stopwatch stopwatch = new Stopwatch();•
for (Int32 i = 0; i < repeats; i++)
Int32 item = random.Next(intArray.Length);
while (intArray[j] != item)
return repeats + " sequential searches in [ms]: " + stopwatch.ElapsedMilliseconds;
public String BinarySearch(Int32 repeats)
if (intArray == null || intArray.Length == 0)
return "There is no array to search in.";
Random random = new Random();
Stopwatch stopwatch = new Stopwatch();
for (Int32 i = 0; i < repeats; i++)
Int32 item = random.Next(intArray.Length);
Int32 stop = intArray.Length - 1;
Int32 j = (start + stop) / 2;
while (intArray[j] != item)
return repeats + " binary searches in [ms]: " + stopwatch.ElapsedMilliseconds;
public String BubbleSort()
Stopwatch stopwatch = new Stopwatch();
Int32 outerStop = intArray.Length - 1;
for (Int32 i = 0; i < outerStop; i++)
Int32 innerStop = outerStop - i;
for (Int32 j = 0; j < innerStop; j++)
if (intArray[j] > intArray[j+1])
Int32 temp = intArray[j + 1];
intArray[j + 1] = intArray[j];
String output = "Bubble sorted in [ms]: " + stopwatch.ElapsedMilliseconds;
public override String ToString()
for (Int32 i = 0; i < intArray.Length; i++)
output += intArray[i] + separator;