1. Prepared By:
Nik Fatinah Binti N. Mohd Farid (2012790807)
Hanis Salwan Bt Mobidin(2012144449)
Assignment 2
Merge Sort Using Recursive
Decomposition Technique
2. Problem
Find a sorted list of element in non-
decreasing order.
Elements can be in positive or negative
integers.
3. The Decomposition Technique and
Mapping Technique Used
Recursive decomposition technique was
implemented to solve this problem.
Merge sort apply a divide and conquer algorithm.
Merge sort work by dividing the unsorted list into
n sublists, each having 1 element.
Repeatedly ,merge sublists to produce a new
sublists until there is only 1 sublist remaining.
Mapping Technique: Since this is a serial
program, no mapping technique used.
4. The Program Structurejava
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
public class MergeSort {
public static int[] mergeSort(int [] list) {
if (list.length <= 1) {
return list;
}
5. // Split the array in half
int[] first = new int[list.length / 2];
int[] second = new int[list.length - first.length];
System.arraycopy(list, 0, first, 0, first.length);
System.arraycopy(list, first.length, second, 0, second.length);
// Sort each half
mergeSort(first);
mergeSort(second);
// Merge the halves together, overwriting the original array
merge(first, second, list);
return list;
6. private static void merge(int[] first, int[] second, int []
result) {
// Merge both halves into the result array
// Next element to consider in the first array
int iFirst = 0;
// Next element to consider in the second array
int iSecond = 0;
// Next open position in the result
int j = 0;
7. // As long as neither iFirst nor iSecond is past the end, move the
// smaller element into the result.
while (iFirst < first.length && iSecond < second.length) {
if (first[iFirst] < second[iSecond]) {
result[j] = first[iFirst];
iFirst++;
} else {
result[j] = second[iSecond];
iSecond++;
}
j++;
}
// copy what's left
System.arraycopy(first, iFirst, result, j, first.length - iFirst);
System.arraycopy(second, iSecond, result, j, second.length -
iSecond);
}
8. public static void main(String args[]) throws Exception
{
String list="";
int i=0,n=0;
MergeSort s= new MergeSort();
ArrayList<Integer> arrlist=new ArrayList<Integer>();
System.out.println(" ");
System.out.println(" ");
System.out.println("Please enter the list of elements,one element
per line");
System.out.println(" write 'STOP' when list is completed ");
BufferedReader bf=new BufferedReader(new
InputStreamReader(System.in));
while(!(list=bf.readLine()).equalsIgnoreCase("stop")){
int intelement=Integer.parseInt(list);
arrlist.add(intelement);
}
9. int elementlist[] = new int[arrlist.size()];
Iterator<Integer> iter = arrlist.iterator();
for (int j=0;iter.hasNext();j++) {
elementlist[j] = iter.next();
}
elementlist=mergeSort(elementlist);
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("Values after Merge Sort : ");
for (int j=0;j<elementlist.length;j++) {
System.out.println(elementlist[j]+" ");
}
}
}