Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of.

Similar presentations


Presentation on theme: "Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of."— Presentation transcript:

1 Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of list L in nondecreasing order according * to comparator c, using the merge-sort algorithm. **/ public static void mergeSort (List L, Comparator c) { int n = L.size(); if (n < 2) return; // the list L is already sorted in this case // divide List L1 = new NodeList(); // first list used in recursion List L2 = new NodeList(); // second list used in recursion int i = 0; while (i < n/2) { L1.insertLast(L.remove(L.first())); // move the first n/2 elements to L1 i++; } while (!L.isEmpty()) L2.insertLast(L.remove(L.first())); // move the rest to L2 // recur mergeSort(L1,c); mergeSort(L2,c); //conquer merge(L1,L2,c,L); }

2 /** * Merge two sorted lists, L1 and L2, into a sorted list L. **/ public static void merge(List L1, List L2, Comparator c, List L) { while (!L1.isEmpty() && !L2.isEmpty()) if (c.compare(L1.first().element(), L2.first().element()) <= 0) L.insertLast(L1.remove(L1.first())); else L.insertLast(L2.remove(L2.first())); while(!L1.isEmpty()) // move the remaining elements of L1 L.insertLast(L1.remove(L1.first())); while(!L2.isEmpty()) // move the remaining elements of L2 L.insertLast(L2.remove(L2.first())); }

3 public static void main (String[] args) { NodeList s = new NodeList(); //s.insertLast(new Integer((int)(Math.random()*100))); System.out.println("Merge sorting:" + "\n" + "\n"); for(int i = 0; i < 10; i++) {Integer j = new Integer((int)(Math.random()*100)); s.insertLast(j); } System.out.println("Input sequence:" + "\n"); DNode p = (DNode) s.first();

4 while (p != s.trailer){ System.out.print(((Integer)(p.element())).intValue() + " "); p = p.getNext(); } System.out.println('\n'); Comparator c = new Comparator(); mergeSort(s, c); System.out.println("Result:" + "\n"); p = (DNode) s.first(); while (p != s.trailer){ System.out.print(((Integer)(p.element())).intValue() + " "); p = p.getNext(); } System.out.println('\n'); }

5 public class Comparator { public int compare(Object v1, Object v2) { if (((Integer) v1).intValue() < ((Integer) v2).intValue()) return -1; else if (((Integer) v1).intValue() == ((Integer) v2).intValue()) return 0; return 1; } public boolean isLessThan (Object v1, Object v2) { Integer u1 = (Integer) v1; Integer u2 = (Integer) v2; if (u1.intValue() < u2.intValue()) return true; return false; }

6 public boolean isEqualTo (Object v1, Object v2) { Integer u1 = (Integer) v1; Integer u2 = (Integer) v2; if (u1.intValue() == u2.intValue()) return true; return false; } public boolean isLargerThan (Object v1, Object v2) { Integer u1 = (Integer) v1; Integer u2 = (Integer) v2; if (u1.intValue() > u2.intValue()) return true; return false; }

7 2. Analyze and implement the following quick-sorting program. import java.lang.*; public class QuickSorter { public static void quickSort (Integer[] S, Comparator c) { if (S.length < 2) return; // the array is already sorted in this case quickSortStep(S, c, 0, S.length-1); // recursive sort method } private static void quickSortStep (Object[] S, Comparator c, int leftBound, int rightBound ) { if (leftBound >= rightBound) return; // the indices have crossed Object temp; // temp object used for swapping Object pivot = S[rightBound]; int leftIndex = leftBound; // will scan rightward int rightIndex = rightBound-1; // will scan leftward while (leftIndex <= rightIndex) { // scan right until larger than the pivot while ( (leftIndex <= rightIndex) && (c.compare(S[leftIndex], pivot)<=0) ) leftIndex++; // scan leftward to find an element smaller than the pivot

8 while ( (rightIndex >= leftIndex) && (c.compare(S[rightIndex], pivot)>=0)) rightIndex--; if (leftIndex < rightIndex) { // both elements were found temp = S[rightIndex]; S[rightIndex] = S[leftIndex]; // swap these elements S[leftIndex] = temp; } } // the loop continues until the indices cross temp = S[rightBound]; // swap pivot with the element at leftIndex S[rightBound] = S[leftIndex]; S[leftIndex] = temp; // the pivot is now at leftIndex, so recurse quickSortStep(S, c, leftBound, leftIndex-1); quickSortStep(S, c, leftIndex+1, rightBound); }

9 public static void main (String[] args) { int k = 0; Integer[] nums = new Integer[10]; System.out.println("Quick sorting:" + "\n"); System.out.println("Input sequence:" + "\n"); //Create an array to hold numbers for(int i = 0; i < nums.length; i++) {k = (int) (Math.random()*100); //Generate random numbers nums[i] = new Integer(k); System.out.print(k + " "); } System.out.println();

10 Comparator c = new Comparator(); quickSort(nums, c); //Sort them System.out.println("Result:" + "\n"); for (int j = 0; j < nums.length; j++) //Print them out System.out.print(nums[j].intValue() + " "); System.out.println(); }

11 3. Analyze and implement the following set-operations program. import java.lang.*; abstract class Merger { private Object a, b; private PositionIterator1 iterA, iterB; public void merge( Sequence A, Sequence B, Comparator comp, Sequence C ) { NodeSequence A1 = (NodeSequence) A; //iterA = (PositionIterator1) A1.elements(); iterA = new PositionIterator1(A1); NodeSequence B1 = (NodeSequence) B; //iterB = (PositionIterator1) B1.elements(); iterB = new PositionIterator1(B1); boolean aExists = advanceA(); boolean bExists = advanceB();

12 while( aExists && bExists ) { /*System.out.println(((Integer) a).intValue() + " " + ((Integer) b).intValue());*/ if( comp.isLessThan( a, b )) { aIsLess( a, C ); aExists = advanceA(); } else if( comp.isEqualTo( a, b )) { bothAreEqual( a, b, C ); aExists = advanceA(); bExists = advanceB(); } else { bIsLess( b, C ); bExists = advanceB(); } while( aExists ) { aIsLess( a, C ); aExists = advanceA(); } while( bExists ) { bIsLess( b, C ); bExists = advanceB(); } protected void aIsLess( Object a, Sequence C ) {} protected void bothAreEqual( Object a, Object b, Sequence C ) { } protected void bIsLess( Object b, Sequence C ) {}

13 private boolean advanceA() { if( iterA.hasNext()) { a = (iterA.next()).element(); //System.out.println(((Integer) a).intValue()); return true; } return false; } private boolean advanceB() { if( iterB.hasNext()) { b = (iterB.next()).element(); return true; } return false; }

14 class UnionMerger extends Merger { protected void aIsLess( Object a, Sequence C ) { C.insertLast( a ); } protected void bothAreEqual( Object a, Object b, Sequence C ) { C.insertLast( a ); } protected void bIsLess( Object b, Sequence C ) { C.insertLast( b ); } class IntersectMerger extends Merger { protected void aIsLess( Object a, Sequence C ) {} protected void bothAreEqual( Object a, Object b, Sequence C ) { C.insertLast( a ); } protected void bIsLess( Object b, Sequence C ) {} } class SubtractMerger extends Merger { protected void aIsLess( Object a, Sequence C ) { C.insertLast( a ); } protected void bothAreEqual( Object a, Object b, Sequence C ) { } protected void bIsLess( Object b, Sequence C ) {} }

15 public class SetOperations { public static void main (String[] args) { NodeSequence a = new NodeSequence(); NodeSequence b = new NodeSequence(); int i = 0; int j[] = {1, 2, 3, 4, 5, 4, 5, 6, 7, 8}; Position p = a.insertFirst(new Integer(j[0])); for(i = 0; i < 4; i++) {Integer k = new Integer(j[i+1]); p = a.insertAfter(p, k); } System.out.print("set1: {"); p = a.first(); for (i = 0; i < 5; i++) { try { System.out.print(((Integer)(p.element())).intValue() + " "); p = a.next(p); } catch(BoundaryViolationException e) {break;} } System.out.print("}"); System.out.println('\n');

16 p = b.insertFirst(new Integer(j[5])); for(i = 5; i < 9; i++) {Integer k = new Integer(j[i+1]); p = b.insertAfter(p, k); } System.out.print("set2: {"); p = b.first(); for (i = 0; i < 5; i++) { try{ System.out.print(((Integer)(p.element())).intValue() + " "); p = b.next(p); } catch(BoundaryViolationException e) {break;} } System.out.print("}"); System.out.println('\n'); Comparator c = new Comparator(); NodeSequence s1 = new NodeSequence();

17 UnionMerger u1 = new UnionMerger(); u1.merge(a, b, c, s1); System.out.print("Union of sets: {"); p = s1.first(); for (i = 0; i < 10; i++) { try{ System.out.print(((Integer)(p.element())).intValue() + " "); p = s1.next(p); } catch(BoundaryViolationException e) {break;} } System.out.print("}"); System.out.println('\n'); IntersectMerger u2 = new IntersectMerger(); NodeSequence s2 = new NodeSequence(); u2.merge(a, b, c, s2); System.out.print("Intersection of sets: {"); p = s2.first(); for (i = 0; i < 10; i++) { try{ System.out.print(((Integer)(p.element())).intValue() + " "); p = s2.next(p); } catch(BoundaryViolationException e) {break;} } System.out.print("}"); System.out.println('\n');

18 SubtractMerger u3 = new SubtractMerger(); NodeSequence s3 = new NodeSequence(); u3.merge(a, b, c, s3); System.out.print("Difference of sets: {"); p = s3.first(); for (i = 0; i < 10; i++) { try{ System.out.print(((Integer)(p.element())).intValue() + " "); p = s3.next(p); } catch(BoundaryViolationException e) {break;} } System.out.print("}"); System.out.println('\n'); }

Download ppt "Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of."

Similar presentations


Ads by Google