Download LAB09:Sorting

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
King Fahd University of Petroleum & Minerals
Information and Computer Science Department
ICS201, SECTIONS Summer Session(2003_
INTRODUCTION TO COMPUTER SCIENCE
Lab 09: Sorting Methods
Sorting
Objectives:



1.
Re-enforce our understanding of sorting and searching algorithms
Empirically compare the running times of sorting algorithms
Compare Objects
Sorting Algorithms Animation Applets.
Click on each of the following links to visualize the steps involved in each of the sorting algorithms learnt
in the lectures:




Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Some Utility Programs
ArrayUtil.java is a utility program that simply provides methods to generate, swap
and print integers.
ArrayUtil.java
import java.util.Random;
public class ArrayUtil {
public static int[] randomIntArray(int length, int n) {
int[] a = new int[length];
Random generator = new Random();
for (int i = 0; i < a.length; i++)
a[i] = generator.nextInt(n);
return a;
}
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
The following program provides a stopwatch object which can help in measuring the
running time of a particular sorting method.
StopWatch.java
public class StopWatch {
private long elapsedTime;
private long startTime;
public StopWatch() {
elapsedTime = 0;
startTime = 0;
}
public void start() {
startTime = System.currentTimeMillis();
}
public void stop() {
long endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
}
public long getElapsedTime() {
return elapsedTime;
}
}
Selection Sort (for integers)
SelectionSort.java
Selection Sort
This includes the following steps:
1. Find the smallest (or largest) item in the list
2. Swap this item with the item at the beginning of the list
3. Repeat steps 1 and 2 using
class SelectSort {
public static int minimumPosition(int[] a, int from) {
int minPos = from;
for (int i = from + 1; i < a.length; i++)
if (a[i] < a[minPos]) minPos = i;
return minPos;
}
public static void sort(int[] a) {
for (int n = 0; n < a.length - 1; n++) {
int minPos = minimumPosition(a, n);
if (minPos != n)
ArrayUtil.swap(a, minPos, n);
}
}
}
public class SelectionSort {
public static void main(String[] args) {
System.out.println("Selection Sort for integers");
System.out.println("Size" + "\t" + "Time");
for(int size = 1000; size <= 50000; size=size+1000)
{
int range = size*10;
int[] a = ArrayUtil.randomIntArray(size, range);
StopWatch s = new StopWatch();
s.start();
SelectSort.sort(a);
s.stop();
System.out.println(size + "\t" + s.getElapsedTime());
}
}
}
The Output
2.
Updating the algorithms to work for Comparable Objects
As we saw in the lectures, the standard sorting and searching methods work with both primitive types and
objects whose classes implements the Comparable interface.
We can equally improve on the various sorting and searching methods we saw in the lectures by
overloading the methods in the respective classes to handle array of Comparable objects. The following
example improved on SelectSort to accept Comparable objects.
Example 2: Improved Select Sort
public class SelectSort {
public static int minimumPosition(int[] a, int from) {
int minPos = from;
for (int i = from + 1; i < a.length; i++)
if (a[i] < a[minPos])
minPos = i;
return minPos;
}
public static int minimumPosition(Comparable[] a, int from) {
int minPos = from;
for (int i = from + 1; i < a.length; i++)
if (a[i].compareTo(a[minPos])<0)
minPos = i;
return minPos;
}
public static void sort(int[] a) {
for (int n = 0; n < a.length - 1; n++) {
int minPos = minimumPosition(a, n);
if (minPos != n)
ArrayUtil.swap(a, minPos, n);
}
}
public static void sort(Comparable[] a) {
for (int n = 0; n < a.length - 1; n++) {
int minPos = minimumPosition(a, n);
if (minPos != n)
ArrayUtil.swap(a, minPos, n);
}
}
}
Notice that the ArrayUtil class is also updated to have a swap method for handling comparable objects.
Also notice the addition of a method copyArray().
import java.util.Random;
public class ArrayUtil {
public static int[] randomIntArray(int length, int n) {
int[] a = new int[length];
Random generator = new Random();
for (int i = 0; i < a.length; i++)
a[i] = generator.nextInt(n);
return a;
}
public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void swap(Comparable[] a, int i, int j) {
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
public static Comparable[] copyArray(Comparable[] a, int size) {
String[] b = new String[size];
if (size <= a.length)
for (int i=0; i<size; i++)
b[i] = (String) a[i];
return b;
}
}
InsertionSort.java
class InsertSort {
public static void sort(int[] a) {
int current, n, j;
for (n = 1; n < a.length; n++) {
current = a[n];
for (j = n; j>0 && current < a[j-1]; j--)
a[j] = a[j-1];
a[j] = current;
}
}
}
public class InsertionSort {
public static void main(String[] args) {
int[] a = ArrayUtil.randomIntArray(20, 100);
ArrayUtil.print(a);
InsertSort.sort(a);
ArrayUtil.print(a);
}
}
A Comparison of Selection Sort and Insertion Sort
SortTest.java
public class SortTest {
public static void main(String[] args) {
System.out.println("Comparison of Selection Sort and Insertion Sort for integers");
System.out.println("Size" + "\t" + "Selection" + "\t" + "Insertion");
for(int size = 1000; size <= 20000; size=size+1000)
{
int range = size*10;
int[] a = ArrayUtil.randomIntArray(size, range);
int[] b = new int[size];
for(int k = 0; k < size; k++)
b[k] = a[k];
StopWatch s = new StopWatch();
s.start();
SelectSort.sort(a);
s.stop();
System.out.print(size + "\t" + s.getElapsedTime());
s.start();
InsertSort.sort(b);
s.stop();
System.out.println("\t\t" + s.getElapsedTime());
}
}
}
The Output
Compile and run the above examples.
Exercise #1
Use Selection sort, to sort the following data. Print the array before and
after ={ 5.0,4.4,1.9,2.9,3.4,3.5};
Exercise #2
Modify the selection sort algorithm to sort an array of integers in
descending order.
Exercise #3
A test class is given below. This class uses print method of class ArrayUtil to print array of string.
public class SelectSortStringTest {
public static void main(String[] args) {
String[] a = {"Majed", "Ahmad", "Ali", "Zahid"};
ArrayUtil.print(a);
SelectSortString.sort(a);
ArrayUtil.print(a);
}
}
Implement class SelectSortString that sorts array of string. Use swap method for string of class ArrayUtil
in your program. Note: To compare string you can use compareTo method class String
str1.compareTo(str2)
return 0 if two strings are of same lexicographic order
return < 0 if str1 comes before str2
return > 0 if str1 comes after str2