(Execution time for sorting) Write a program that obtains the execution time of selection sort, bubble sort, merge sort, quick sort, heap sort, and radix sort for input size 50000, 100,000, 150,000, 200,000, 250,000, and 300,000. Your program should create data randomly and print a table like this:
The text gives a recursive quick sort. Write a nonrecursive version in this exercise.
Araysize Selection SortBubble So Merge Sort Quick Sort Heap So Radix Sort 50000 100000 150000 200000 250000 300000
Expert Answer
//importing packages
import java.io.*;
import java.util.Scanner;
import java.lang.*;
class AllSortings
{
public static void main(String[] args)
{
//fill array with random values less than 50000
double[] selArray=new double[50000];
//fill array with random values less than 150000
double[] bubbleArray=new double[150000];
//fill array with random values less than 150000
double[] quickArray=new double[150000];
//for generating random numbers between 1 and 50000
for(int i=0;i<50000;i++)
selArray[i]=(int)(Math.random()*50000);
//call selection sort funtion
long selSortTime=selSort(selArray,50000);
System.out.println(“SIZEt”+”ALGORITHMt”+”TIME”);
System.out.println();
System.out.println(“50000 “+”Selection sort “+selSortTime+” millisec”);
//for generating random numbers between 1 and 150000
for(int i=0;i<150000;i++)
bubbleArray[i]=(int)(Math.random()*150000);
//call bubble sort funtion
long bubbleSortTime=bubbleSort(bubbleArray,50000);
System.out.println(“150000 “+”Bubble sort “+bubbleSortTime+” millisec”);
}
}
//bubble sort funtion
public static long bubbleSort(double bubbleArray[],int size)
{
long startTime=System.currentTimeMillis();
for(int i=0;i<size;i++)
for(int j=1;j<size-1;j++)
if(bubbleArray[j-1]>bubbleArray[j])
{
double temp;
temp=bubbleArray[j-1];
bubbleArray[j-1]=bubbleArray[j];
bubbleArray[j]=temp;
}
long endTime=System.currentTimeMillis();
return endTime-startTime;
}
//selection sort funtion
public static long selSort(double selArray[],int size)
{
long startTime=System.currentTimeMillis();
int startScan,minIndex;
double minValue;
for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=selArray[startScan];
for(int index=startScan+1;index<size;index++)
{
if(selArray[index]<minValue)
{
minValue=selArray[index];
minIndex=index;
}
}
selArray[minIndex]=selArray[startScan];
selArray[startScan]=minValue;
}
long endTime=System.currentTimeMillis();
return endTime-startTime;
}
OUTPUT :