Question: Design and implement an experiment to compare in real time the running times of the following sorting algorithms: - bubble sort - insertion sort -
Design and implement an experiment to compare in real time the running times of the following sorting algorithms: - bubble sort - insertion sort - selection sort - quicksort - merge sort An implementation of quicksort is given in a separate file. Time the sorting algorithms several times each, and save the results in a .csv file that can be open in an Excel file later. Format your .csv file as follows: , , , , , , , , , , , , , , , , , , , , , , , , , (r.t. stands for running time) Time not less than 100 runs of these algorithms (you should have a similar number of lines in your .csv file). To time your code, you can use any of the Java utilities discussed in class, i.e. System. currentTimeMillis(), System.nanoTime(), or (new Date()).getTime(). Use Excel to depict graphically the results of your experiment. What did you observe?
use following code
public void quicksort()
{
quicksort(0, length - 1);
}
private void quicksort(int begin, int end)
{
int temp;
int pivot = findPivotLocation(begin, end);
// swap list[pivot] and list[end]
temp = list[pivot];
list[pivot] = list[end];
list[end] = temp;
pivot = end;
int i = begin,
j = end - 1;
boolean iterationCompleted = false;
while (!iterationCompleted)
{
while (list[i] < list[pivot])
i++;
while ((j >= 0) && (list[pivot] < list[j]))
j--;
if (i < j)
{
//swap list[i] and list[j]
temp = list[i];
list[i] = list[j];
list[j] = temp;
i++;
j--;
} else
iterationCompleted = true;
}
//swap list[i] and list[pivot]
temp = list[i];
list[i] = list[pivot];
list[pivot] = temp;
if (begin < i - 1) quicksort(begin, i - 1);
if (i + 1 < end) quicksort(i + 1, end);
}
/*
* Computes the pivot
*/
private int findPivotLocation(int b, int e)
{
return (b + e) / 2;
}
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class WritingToTextfile
{
public static void main(String[] args)
{
String outputFilename = "output.csv";
PrintWriter output = null;
//open output stream
try
{
output = new PrintWriter(new FileWriter(outputFilename));
} catch (IOException ex)
{
System.exit(1);
}
Random rnd = new Random();
int n = 100;
for (int i = 0; i < n; i++)
{
output.println(i + "," + rnd.nextInt(10) + "," +
rnd.nextInt(100) + "," + rnd.nextInt(1000));
}
//close output stream
output.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
