Please do this in Java. Include the sorts in your code and print out the time th
ID: 3810868 • Letter: P
Question
Please do this in Java.
Include the sorts in your code and print out the time that it took for each amount of numbers.....Some might be too slow and not work.
Sorting Write a program that does two of the three, BubbleSort, InsertionSort, SelectionSort, and compares the time differences with two identical starting arrays. Use Arrays of random numbers size 20, 100, 500, 1000, 10000 Example of random number generating and figuring out the time complexity: for (int i 0; i stuff. length; i++) stuff ij new Integer ((int) (Math.random() size)); startTime System. currentTimeMillis(); Insertionsort.sort stuff); finish Time System currentTimeMillis(); System println ("The time in milliseconds to sort size items is (finishTime start Time)Explanation / Answer
PROGRAM CODE:
package array;
public class Sorting {
public static void insertionSort(int array[])
{
int n = array.length;
for (int j = 1; j < n; j++) {
int value = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > value ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = value;
}
}
public static void selectionSort(int array[])
{
for (int i = 0; i < array.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < array.length; j++){
if (array[j] < array[index]){
index = j;
}
}
int smallerNumber = array[index];
array[index] = array[i];
array[i] = smallerNumber;
}
}
public static void main(String[] args) {
long startTime, endTime;
int size20[] = new int[20];
int size100[] = new int[100];
int size500[] = new int[500];
int size1000[] = new int[1000];
int size10000[] = new int[10000];
for(int i=0; i<size20.length; i++)
{
size20[i] = new Integer((int)Math.random()*20);
}
startTime = System.currentTimeMillis();
insertionSort(size20);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 20 items using insertion sort is " + (endTime-startTime));
startTime = System.currentTimeMillis();
selectionSort(size20);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 20 items using selection sort is " + (endTime-startTime));
for(int i=0; i<size100.length; i++)
{
size100[i] = new Integer((int)Math.random()*100);
}
startTime = System.currentTimeMillis();
insertionSort(size100);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 100 items using insertion sort is " + (endTime-startTime));
startTime = System.currentTimeMillis();
insertionSort(size100);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 100 items using selection sort is " + (endTime-startTime));
for(int i=0; i<size500.length; i++)
{
size500[i] = new Integer((int)Math.random()*500);
}
startTime = System.currentTimeMillis();
insertionSort(size500);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 500 items using insertion sort is " + (endTime-startTime));
startTime = System.currentTimeMillis();
insertionSort(size500);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 500 items using selection sort is " + (endTime-startTime));
for(int i=0; i<size1000.length; i++)
{
size1000[i] = new Integer((int)Math.random()*1000);
}
startTime = System.currentTimeMillis();
insertionSort(size1000);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 1000 items using insertion sort is " + (endTime-startTime));
startTime = System.currentTimeMillis();
insertionSort(size1000);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 1000 items using selection sort is " + (endTime-startTime));
for(int i=0; i<size10000.length; i++)
{
size10000[i] = new Integer((int)Math.random()*10000);
}
startTime = System.currentTimeMillis();
insertionSort(size10000);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 10000 items using insertion sort is " + (endTime-startTime));
startTime = System.currentTimeMillis();
insertionSort(size10000);
endTime = System.currentTimeMillis();
System.out.println("The time in milliseconds to sort 10000 items using selection sort is " + (endTime-startTime));
}
}
OUTPUT:
The time in milliseconds to sort 20 items using insertion sort is 0
The time in milliseconds to sort 20 items using selection sort is 0
The time in milliseconds to sort 100 items using insertion sort is 0
The time in milliseconds to sort 100 items using selection sort is 0
The time in milliseconds to sort 500 items using insertion sort is 0
The time in milliseconds to sort 500 items using selection sort is 0
The time in milliseconds to sort 1000 items using insertion sort is 0
The time in milliseconds to sort 1000 items using selection sort is 0
The time in milliseconds to sort 10000 items using insertion sort is 0
The time in milliseconds to sort 10000 items using selection sort is 1