Need to make a main and a time functions that compares insert, merge, quick sort
ID: 3679572 • Letter: N
Question
Need to make a main and a time functions that compares insert, merge, quick sort and time them. Numbering main points i'd like covered
I've already implemented the sorts, just need help with main and a time function PLEASE use c# if possible its what I coded everything in and would easiest for me to understand.
void InsertionSort(int[] a)
void MergeSort(int[] a, int p, int r)
void Quicksort(int[] a, int P, int r)
1) I'm confused at the part where I must make a copy of array a into b
b[1....n] = a[i,1....n]
This is what I did
int[] b= new int [n];
b[n] = a[i, n];
but having some issues with that line with out of bounds
2) a time function that times the algorithms in milli seconds
t1 = time();
t2 = time();
3) Calculate avg (fairly sure I did this right)
this is how I did it
in the inner most for loop
IS_taccum = talg1[i, n] + IS_taccum;
and once out I did this
double IS_tavg = 0;
IS_tavg = IS_taccum / m;
Copy of code for the main , I get out of range error on b[n] = a[i, n]; so the way I did might be totally wrong
Random random = new Random();
int ns = 1000,nf =20000,counter=1000,m=10;
int randomNumber = random.Next(0, 32767);
double t1, t2;
int[,] a = new int[m, nf];
int n= 0;
double IS_taccum = 0;
double[,] talg1 = new double[m, nf];
// Generates numbers in te array
for (int i = 0;i<m;i++)
{
for(int j =0;j<nf;j++)
{
a[i, j] = random.Next(0, 32767);
}
}
for (n = ns; n <= nf;n = n + counter)
{
for (int i = 0;i <= m; i = i+1)
{
int[] b= new int [n];
b[n] = a[i, n];
t1 = time();
InsertionSort(b);
t2 = time();
talg1[i, n] = t2 - t1;
IS_taccum = talg1[i, n] + IS_taccum;
}
}
double IS_tavg = 0;
IS_tavg = IS_taccum / m;
Copy of my assignment
Explanation / Answer
Good Wishes,
1.
2. To know the time taken by insertion sort is:
in main() add this
const intsize = 1000000;//size n
Random random =new Random(5000);
int[] array =new int[size];
for(inti = 0; i < array.Length; i++)
array[i] = random.Next();
//Console.WriteLine("Unsorted Arrays are: ");
DateTimestart =DateTime.Now;InsertionSort(array);//change when thru with insertion sort toMergeSort(array) and QuickSort(array, 0, array.Length -1);
TimeSpanelapsedTime =DateTime.Now - start;
Console.WriteLine("The elapsed time (in milliseconds) is: "+elapsedTime.TotalMilliseconds.ToString() +" and"+ (elapsedTime.TotalMilliseconds /1000.0).ToString()+" Seconds");/*Console.WriteLine("The elapsed time seconds is: " + elapsedTime.TotalSeconds);*/
//Console.WriteLine("The sorted Arrays are ");Likewise for other sorting techniques.
3.
do it this way
Hope this is clear.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.