Question: I have the code for this but need help with the spreadsheet and report. 1. Write a computer program that prompts the user for a
I have the code for this but need help with the spreadsheet and report.
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. You can write the program in either Java, C++, C#, or whatever language you are most comfortable in.
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 program works, 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.
5. Create a spreadsheet showing the results of 3 and 4 and create a graph to graphically represent the information Show both sort algorithms on the same graph for comparison.
6. Write a one-page document explaining the results, bearing in mind that both algorithms have a complexity of O(n^2) and what you know about complexity analysis. Use your knowledge of complexity analysis to explain your results.
Here is the code:
// here is the four set of programs ... you can give different inputs.
1)c# code for Bubble sort
using System.IO;
using System;
class Program
{
static void Main()
{
Random rnd = new Random(); // random funtion
Console.WriteLine("Enter the size ");
int n = Convert.ToInt32(Console.ReadLine());
int[] a = new int[n]; //array element
Console.WriteLine("Array before sorting ");
for (int i = 0; i < n; i++)
{
a[i] = rnd.Next(1,100); // random numbers range(1 t0 100)
Console.WriteLine(a[i]);
}
for (int m = 0; m < n;m++ ) // bubble sort
{
for(int j=m+1;j
{
if(a[m]>a[j])
{
int x = a[j];
a[j] = a[m];
a[m] = x;
}
}
}
Console.WriteLine("Bubble Sorted Array ");
for (int i = 0; i < n; i++)
{
Console.WriteLine(a[i]);
}
}
}
2) selection sort program
using System.IO;
using System;
class Program
{
static void Main()
{
Random rnd = new Random(); // random funtion
Console.WriteLine("Enter the size ");
int n = Convert.ToInt32(Console.ReadLine());
int[] a = new int[n]; //array element
Console.WriteLine("Array before sorting ");
for (int i = 0; i < n; i++)
{
a[i] = rnd.Next(1,100); // random numbers range(1 t0 100)
Console.WriteLine(a[i]);
}
//Selection sort
int tmp, min_key;
for (int j = 0; j < n - 1; j++)
{
min_key = j;
for (int k = j + 1; k < n; k++)
{
if (a[k] < a[min_key])
{
min_key = k;
}
}
tmp = a[min_key];
a[min_key] = a[j];
a[j] = tmp;
}
Console.WriteLine(" Sorted Array using Selection sort ");
for (int i = 0; i < n; i++)
{
Console.WriteLine(a[i]);
}
}
}
3)bubble sort with time difference:
using System.IO;
using System;
class Program
{
static void Main()
{
TimeZone zone = TimeZone.CurrentTimeZone;
Random rnd = new Random();
Console.WriteLine("Enter the size ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the number of iterations ");
int s = Convert.ToInt32(Console.ReadLine());
int[] a = new int[n];
for(int l=0;l
{
Console.WriteLine("Array before sorting ");
for (int i = 0; i < n; i++)
{
a[i] = rnd.Next(1,100);
Console.WriteLine(a[i]);
}
Console.WriteLine("start time");
DateTime local = zone.ToLocalTime(DateTime.Now);
Console.WriteLine(local);
// Bubble sort
for (int m = 0; m < n;m++ )
{
for(int j=m+1;j
{
if(a[m]>a[j])
{
int x = a[j];
a[j] = a[m];
a[m] = x;
}
}
}
Console.WriteLine("End Time");
DateTime local1 = zone.ToLocalTime(DateTime.Now);
Console.WriteLine(local1);
Console.WriteLine("Bubble Sorted Array ");
for (int i = 0; i < n; i++)
{
Console.WriteLine(a[i]);
}
Console.WriteLine("The running time for array in milleseconds");
Console.WriteLine(local1.Subtract(local).TotalMinutes);
}
}
}
4)Selection sort with time difference
using System.IO;
using System;
class Program
{
static void Main()
{
TimeZone zone = TimeZone.CurrentTimeZone;
Random rnd = new Random();
Console.WriteLine("Enter the size ");
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the number of iterations ");
int s = Convert.ToInt32(Console.ReadLine());
int[] a = new int[n];
for(int l=0;l
{
Console.WriteLine("Array before sorting ");
for (int i = 0; i < n; i++)
{
a[i] = rnd.Next(1,100);
Console.WriteLine(a[i]);
}
Console.WriteLine("start time");
DateTime local = zone.ToLocalTime(DateTime.Now);
Console.WriteLine(local);
int tmp, min_key;
for (int j = 0; j < n - 1; j++)
{
min_key = j;
for (int k = j + 1; k < n; k++)
{
if (a[k] < a[min_key])
{
min_key = k;
}
}
tmp = a[min_key];
a[min_key] = a[j];
a[j] = tmp;
}
Console.WriteLine("End Time");
DateTime local1 = zone.ToLocalTime(DateTime.Now);
Console.WriteLine(local1);
Console.WriteLine(" Sorted Array using selection sort ");
for (int i = 0; i < n; i++)
{
Console.WriteLine(a[i]);
}
Console.WriteLine("The running time for array in milleseconds");
Console.WriteLine(local1.Subtract(local).TotalMinutes);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
