1. Write a computer program that prompts the user for a number, creates an array
ID: 3792069 • Letter: 1
Question
1. Write a computer program that prompts the user for a number, creates an array for that number of random integers, and then uses the bubble sort to order the array. The program should print out the array prior to the call to the sorting algorithm and afterward. Write the program in C#.
2. Repeat 1 but use selection sort this time.
1 and 2 are primarily intended to make sure that your algorithms work.
Once you are convinced your programs work, do the following
3. Write a computer program that prompts the user for two numbers, n for the number of items in the array to sort, and num_i for a number of arrays of this size to sort. Then do the following:
Initiate a variable running_time to 0
Create a for loop that iterates num_i times.
In the body of the loop,
Create an array of n random integers
Get the time and set this to start-time. You will have to figure out what the appropriate command is in the programming language you are using to find them time
Use bubble sort to sort the array
Get the time and set this to end-time
Subtract end-time from start-time and add the result to running_time
Once the program has run, note
The number of items sorted
The number of iterations
The average running time for each size array
Repeat the process nine times, using 50, 250 and 500 as the size of the array, and 100, 1000 and 10,000 as the number of arrays of each size to sort.
4. Repeat 3 using selection sort.
Explanation / Answer
We can use diaginoising to calculate the runtime of the program..here is the code given below .
Buble sort.
using System;
using System;
using System.Diagnostics;
class bubblesort
{
static void Main(string[] args)
{
int a,t;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string StartTime = (DateTime.Now.ToLongTimeString());
Console.WriteLine("Enter size of Array:-");
len = int.Parse(Console.ReadLine());
int[] a = new int[len];
Console.WriteLine("Enter the Elements of the Array:-");
for (int i = 0; i < a.Length; i++)
{
array[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine(" The Elemets of the Array are:-");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
for (int j = 0; j <= a.Length - 2; j++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
Console.WriteLine("The Sorted Array :");
foreach (int aray in a)
Console.Write(aray + " ");
Console.ReadLine();
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 " + elapsedTime);
}
}
------------------
using System;
using System;
using System.Diagnostics;
class selectionsort
{
static void Main(string[] args)
{
int a,tmp, min_key;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string StartTime = (DateTime.Now.ToLongTimeString());
Console.WriteLine("Enter size of Array:-");
len = int.Parse(Console.ReadLine());
int[] a = new int[len];
Console.WriteLine("Enter the Elements of the Array:-");
for (int i = 0; i < a.Length; i++)
{
a[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine(" The Elemets of the a are:-");
for (int i = 0; i < a.Length; i++)
{
Console.WriteLine(a[i]);
}
for (int j = 0; j < a.Length-1; j++)
{
min_key = j;
for (int k = j + 1; k < a.Length; k++)
{
if (a[k] < a[min_key])
{
min_key = k;
}
}
tmp = a[min_key];
a[min_key] = a[j];
a[j] = tmp;
}
Console.WriteLine("The Array After Selection Sort is: ");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(a[i]);
}
Console.ReadLine();
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 " + elapsedTime);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.