I need four programs: 1. Write a computer program that prompts the user for a nu
ID: 3807295 • Letter: I
Question
I need four programs:
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 C#.
2. repeat 1 but use selection sort
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 iterations. Then do the following: 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 end 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 that the output of your program should include:
- The number of items sorted
- The number of iterations
- The 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 iterations.
4. Repeat 3 using selection sort.
In totality, the answer should contain 4 program codes.
Explanation / Answer
// here is the four set of programs ... you can give diferent 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<n;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<s;l++) // iteration begins
{
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<n;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<s;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);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.