Your SlideShare is downloading. ×
0
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Recursive decomposition
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Recursive decomposition

81

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
81
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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]+" "); } } }
  • 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. The Output
  • 12. Reference  http://javahungry.blogspot.com/2013/06/java- sorting-program-code-merge-sort.html

×