15 Comparison of Sorting Algorithms

advertisement
Comparison of Sorting Algorithms
There are many different ways to sort data. You probably use different methods yourself
when you sort things. In fact, you probably use a combination of some of the methods
listed below. If you search through the stack and to find the first one, and then put it at
the beginning, you are doing a form of “selection sort”. If you start from the beginning
and take each one and put it where it belongs in the part of the pile that is already sorted,
you are doing a form of “insertion sort”. If you split the pile into two or more parts, sort
the parts and then put them back together, you are doing a form of “merge sort”.
(However, unless you are very strange, you probably don’t perform a bubble sort or
quicksort!!)
Some of these are easier to remember and write than others, and some are faster than
others. In this assignment, you will find out which is the fastest.
First, let’s make a Sorter interface. All the different types of sorters will implement this
interface, which means they must have a sort() method of the form shown in the
interface. This allows the testing method to take any one and test it without knowing
which one it is. Create a new Empty document, type up the following and save it:
// The "Sorter" interface.
public interface Sorter
{
public void sort (int num, int list[]);
}
Now create all of the sorter classes shown on the attached pages (SelectionSorter,
InsertionSorter, BubbleSorter, BetterBubbleSorter, MergeSorter and QuickSorter).
To ensure that they are all working properly, write the following testing program:
import java.util.*;
// The "SortTester" class.
public class SortTester
{
// Number of values in the list:
private static final int NUM = 100;
// List of random numbers
private static int list[] = new int [NUM];
// "Seed" for the random number generator
// (so that each sort algorithm is given the same set of data)
private static long seed = new Random().nextLong();
public static void main (String[] args)
{
System.out.println("Original list:");
makeList();
showList();
System.out.println("----------------------------------------");
System.out.println("Selection sort:");
test (new SelectionSorter());
System.out.println("----------------------------------------");
System.out.println("Insertion sort:");
test (new InsertionSorter());
System.out.println("----------------------------------------");
System.out.println("Bubble sort:");
test (new BubbleSorter());
System.out.println("----------------------------------------");
System.out.println("Better bubble sort:");
test (new BetterBubbleSorter());
System.out.println("----------------------------------------");
System.out.println("Merge sort:");
test (new MergeSorter());
System.out.println("----------------------------------------");
System.out.println("Quick sort:");
test (new QuickSorter());
} // main method
public static void test(Sorter x)
{
makeList();
x.sort(NUM, list); // call the sort method of the Sorter
showList();
}
public static void makeList ()
{
Random rand = new Random(seed);
for (int i = 0 ; i < NUM ; i++)
{
list [i] = (int) (rand.nextDouble () * 100 + 1);
}
}
public static void showList ()
{
for (int i = 0 ; i < NUM ; i++)
{
System.out.print (list [i]);
System.out.print (" ");
}
System.out.println ();
}
} // SortTester class
Compile and run the SortTester. You should see that all the sorters sort the data
correctly. Notice that they all get the same data, even though it is “random”. That is
because we use the same number to “seed the pseudo random number generator” each
time. A random number generator is a program that generates numbers that appear to be
random. It generates each number from the previous one. By starting it with the same
“seed” number, we ensure that it generates the same set of numbers each time. This is
the only really fair way to test the sorting algorithms.
Next, write the program to compare the speed of the different sorting algorithms. This
program reads the time before and after the sort is performed and prints the difference.
To save some typing, you can modify the previous program to create this one – but make
sure you do a “Save As”! (Note: it might take a few minutes to run all the tests.)
import java.util.*;
// The "SortComparison" class.
public class SortComparison
{
// Number of values in the list:
private static final int NUM = 100000;
// List of random numbers
private static int list[] = new int [NUM];
// "Seed" for the random number generator
// (so that each sort algorithm is given the same set of data)
private static long seed = new Random().nextLong();
public static void main (String[] args)
{
System.out.print("Selection sort:
");
System.out.println(time (new SelectionSorter()));
System.out.print("Insertion sort:
");
System.out.println(time (new InsertionSorter()));
System.out.print("Bubble sort:
");
System.out.println(time (new BubbleSorter()));
System.out.print("Better bubble sort: ");
System.out.println(time (new BetterBubbleSorter()));
System.out.print("Merge sort:
");
System.out.println(time (new MergeSorter()));
System.out.print("Quick sort:
");
System.out.println(time (new QuickSorter()));
} // main method
public static long time(Sorter x)
{
long startTime;
makeList();
startTime = Calendar.getInstance ().getTimeInMillis ();
x.sort(NUM, list);
return Calendar.getInstance ().getTimeInMillis () - startTime;
}
<-------- makeList and showList are the same as in SortTester -------->
} // SortComparison class
Which sorting algorithm is fastest?
Download