BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI
SOFTWARE DEVELOPMENT & EDUCATIONAL TECHNOLOGY UNIT
SDET C102, CORE JAVA
LAB-7 : 16/11/07



Assuming you have the following interface:

public interface Sort{

public void sort(int elements[], int start, int end);

public void sort(int elements[]); //equivalent to calling sort(elements, 0, elements.length-1)

}



Implement this interface with an AbstractMergeSort class:



public abstract class AbstractMergeSort implements Sort {

public void merge(int a[], int b[], int mergedandb[]) {

//Your Code here.

}

}

public void sort(int elements[]); {

//Your Code here.

}

}

Now extend the abstract class with a concrete MergeSort class:

public class MergeSort extends MergeSort {

public void sort(int elements[]) {

//Your Code here.

}

Now implement a SorterTaskThread which can invoke MergeSort on a part of the array:

public class SorterTaskThread extends Thread {

public SorterThread(int elements[], int start, int end) {

//Your Code here.

}

public void run() {

//Your Code here.

}

}

Now extend the ParallelMergeSort from the AbstractMergeSortClass class, and make it invoke as many SorterTaskThread s as required:


public class ParallelMergeSort extends AbstractMergeSort {

private static final int NUM_THREADS=2;

public void sort(int elements[]) {

//Your Code here.

}

}

Now finally implement class MergeSortTester; Randomly populate an array of 200 elements using Math.random()*123456.

Sort the array using MergeSort, then using ParallelMergeSort.

Now run both implementations and test the time using this command:

test java MergeSortTester

Sample the times taken for 10 times, average it. Now find the ratio of time for MergeSort/time for ParallelMergeSort.

If it's <1, why is it so?

Now repeat the same with 200000 elements. Does the ratio change?