Simple Sorting Methods (Buble Sort, Selection Sort, Insertion Sort)
1. ssObjectives
a. Know how, in reality, three simple sorting methods work.
b. Know how to use analysis tool to compare performance of sorting algorithms
2. Problem statement
a. Write a Java program to measure time (in seconds) needed for each simple sorting algorithms applying on the same random array of integer values. Sizes of arrays are accordingly 10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000 and 50000. Each time, you write down the measured time in following table.
Table 1 - Experiment 1: Simple sorting on random data
Bubble Sort
Selection Sort
Insertion Sort
10000
556ms
210ms
149ms
15000
1257ms
467ms
341ms
20000
2231ms
842ms
608ms
25000
3486ms
1297ms
937ms
30000
5110ms
1899ms
1395ms
35000
6906ms
2564ms
1871ms
40000
9294ms
3472ms
2511ms
45000
11514ms
4273ms
3087ms
50000
14553ms
5467ms
3871ms
b. Based on above table, give your comments on real complexity of the three simple sorting algorithms. (Remember, all of them are O(n^2) in theory).
3. Instruction: (Follow instructions step-by-step)
a. Take a look at sample source files and read it carefully. There are three files:
i. Array.java: contains class Array, ii. TimeUtils.java: contains class TimeUtils and iii. SortingApp.java: contains main() method.
Class Name
Typical Method
Notes
Array.java
Array
public void randomInit(int numElements)
Create an array of random long-integer values with size specified by numElements.
public void bubbleSort()
Bubble sorting method.
public void selectionSort()
Selection sorting method. You will add its code by yourself (textbook).
public void insertionSort()
Insertion sorting method. You will add its code by yourself (textbook).
TimeUtils
.java
TimeUtils
public static long now()
Get current time in milli-seconds since epoch.
SortingApp.java
SortingApp public static void main(String[] args)
Main method used to measure time.
Class Diagram
b. The Array class is highly similar