Prepared By:
Nik Fatinah Binti N. Mohd Farid (2012790807)
Hanis Salwan Bt Mobidin(2012144449)
Assignment 2
Merge Sort Usin...
Problem
 Find a sorted list of element in non-
decreasing order.
 Elements can be in positive or negative
integers.
The Decomposition Technique and
Mapping Technique Used
 Recursive decomposition technique was
implemented to solve this p...
The Program Structurejava
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
imp...
// Split the array in half
int[] first = new int[list.length / 2];
int[] second = new int[list.length - first.length];
Sys...
private static void merge(int[] first, int[] second, int []
result) {
// Merge both halves into the result array
// Next e...
// As long as neither iFirst nor iSecond is past the end, move the
// smaller element into the result.
while (iFirst < fir...
public static void main(String args[]) throws Exception
{
String list="";
int i=0,n=0;
MergeSort s= new MergeSort();
Array...
int elementlist[] = new int[arrlist.size()];
Iterator<Integer> iter = arrlist.iterator();
for (int j=0;iter.hasNext();j++)...
Illustration of the Graph
100 45 67 34 23 100
100 45 67 34 23 100
100 45 67 34 23 100
45 67
45 67
45 67 100
23 100
23 100
...
The Output
Reference
 http://javahungry.blogspot.com/2013/06/java-
sorting-program-code-merge-sort.html
Upcoming SlideShare
Loading in …5
×

Recursive decomposition

340 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
340
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Recursive decomposition

  1. 1. Prepared By: Nik Fatinah Binti N. Mohd Farid (2012790807) Hanis Salwan Bt Mobidin(2012144449) Assignment 2 Merge Sort Using Recursive Decomposition Technique
  2. 2. Problem  Find a sorted list of element in non- decreasing order.  Elements can be in positive or negative integers.
  3. 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. 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. 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. 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. 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. 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. 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]+" "); } } }
  10. 10. Illustration of the Graph 100 45 67 34 23 100 100 45 67 34 23 100 100 45 67 34 23 100 45 67 45 67 45 67 100 23 100 23 100 23 34 100 23 34 45 67 100 100 Merge Sort Algorithm Merge Algorithm
  11. 11. The Output
  12. 12. Reference  http://javahungry.blogspot.com/2013/06/java- sorting-program-code-merge-sort.html

×