Data array A has data series from 1,000,000 to 1 with step size 1, which is in p
ID: 3663968 • Letter: D
Question
Data array A has data series from 1,000,000 to 1 with step size 1, which is in perfect decreasing order.
Data array B has data series from 1 to 1,000,000, which is in random order.
Please use “insert sorting” method to sort both data array A and B into increasing order to “1 to 1,000,000”.
(1) Write computer program to implement this algorithm and demonstrate the results
(2) What is the machine run time in second for sorting array A?
(3) What is the machine run time in second for sorting array B?
if possible can you explain this answer in MS EXCEL developer
THANK YOU
Explanation / Answer
/**
* 1. The java program that demonstrates the sorting of
* two arrays of size ,1,000,000. One arrays is in
* decreasing order from 1000000 to 1 and another array
* from 1 to 1,000,000 with random order.
* Sort the two arrays and find the time taken in millli seconds
* and prints the time taken for two arrays and prints
* the results to console
* */
//Sorting.java
import java.util.Arrays;
import java.util.Random;
public class Sorting
{
public static void main(String[] args)
{
//two integer arrays
int[] A=new int[1000000];
int[] B=new int[1000000];
//call fillA and fillB to fill two arrays
fillA(A);
fillB(B);
long startTime=System.currentTimeMillis();
//call sortA method
sortA(A);
long endTime=System.currentTimeMillis();
System.out.println("Time taken ,A : "+(endTime-startTime));
startTime=System.currentTimeMillis();
//call sortB method
sortB(B);
endTime=System.currentTimeMillis();
System.out.println("Time taken ,B : "+(endTime-startTime));
}
//Sorts the array, b
private static void sortB(int[] b)
{
Arrays.sort(b);
}
//Sorts the array, a
private static void sortA(int[] a)
{
Arrays.sort(a);
}
//Method that fills the b array
private static void fillB(int[] b)
{
for (int i = 0; i < b.length; i++)
{
b[i]=i+1;
}
//shuffle the array in random order
sortRandomOrder(b);
}
private static void sortRandomOrder(int[] b)
{
Random r=new Random();
for (int i = 0; i < b.length-1; i++)
{
//shuffle random order
int t=r.nextInt(b.length-1);
int element=b[t];
element=b[i];
b[i]=b[i+1];
b[i+1]=element;
}
}
//Method that fills the array a from 1000000-1 to 1
private static void fillA(int[] a)
{
for (int i = 1000000-1; i >=0; i--)
{
a[i]=(i+1);
}
}
}
------------------------------------------------------------------------------------------
2 and 3 comibined results as output from the above java program
Time taken ,A : 0
Time taken ,B : 15
Machine time taken to sort the array, A to sort the array in increasing order is almost zero,Time taken ,A : 0
Machine time taken to sort the array, B to sort the array in increasing order is almost zero,Time taken ,B : 15 milli seconds since the array, B is completedly shuffled random order array of size 10000000.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.