Advertisement
linqserver

Rafal ArrayList Sorted Merge

Mar 6th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. package week8;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6.  
  7. public class homework_P6_28 {
  8.     /*6.28 Write a method
  9.     public static ArrayList<Integer> mergeSorted(ArrayList<Integer> a,
  10.     ArrayList<Integer> b)
  11.     that merges two sorted array lists, producing a new sorted array list. Keep an index
  12.     into each array list, indicating how much of it has been processed already. Each time,
  13.     append the smallest unprocessed element from either array list, then advance the
  14.     index. For example, if a is
  15.     1 4 9 16
  16.     and b is
  17.     4 7 9 9 11
  18.     then mergeSorted returns the array list
  19.     1 4 4 7 9 9 9 11 16*/
  20.     public static void main(String[] args) {
  21.         ArrayList<Integer> firstList = newList(4, 99);
  22.         ArrayList<Integer> secondList = newList(5, 99);
  23.         ArrayList<Integer> mergeList = mergeSorted(firstList, secondList);
  24.          
  25.         printArray(firstList, "First List");
  26.         printArray(secondList, "Second List");
  27.         printArray(mergeList, "Merge Sorted List");
  28.     }
  29.  
  30.     public static ArrayList<Integer> mergeSorted(ArrayList<Integer> a,
  31.             ArrayList<Integer> b) {
  32.         ArrayList<Integer> tempList = newList(a.size() + b.size(), 1);
  33.         int pos1 = 0;
  34.         int pos2 = 1;
  35.         if (a.size() < b.size() || a.size() == b.size()) {
  36.             for (int i = 0; i < a.size(); i++) {
  37.                 tempList.set(pos1, a.get(i));
  38.                 pos1 += 2;
  39.             }
  40.             for (int i = 0; i < b.size(); i++) {
  41.                 if (pos2 < pos1) {
  42.                     tempList.set(pos2, b.get(i));
  43.                     pos2 += 2;
  44.                 } else {
  45.                     tempList.set(pos2 - 1, b.get(i));
  46.                     pos2++;
  47.                 }
  48.             }
  49.         } else {
  50.             for (int i = 0; i < b.size(); i++) {
  51.                 tempList.set(pos2, b.get(i));
  52.                 pos2 += 2;
  53.             }
  54.             for (int i = 0; i < a.size(); i++) {
  55.                 if (pos1 < pos2) {
  56.                     tempList.set(pos1, a.get(i));
  57.                     pos1 += 2;
  58.                 } else {
  59.                     tempList.set(pos1 - 1, a.get(i));
  60.                     pos1++;
  61.                 }
  62.             }
  63.         }
  64.         Collections.sort(tempList);
  65.         return  tempList;
  66.     }
  67.  
  68.     public static ArrayList<Integer> newList(int size, int rand) {
  69.         ArrayList<Integer> temp = new ArrayList<Integer>();
  70.         for (int i = 0; i < size; i++) {
  71.             if (rand == 0) {
  72.                 temp.add(i + 1);
  73.             } else {
  74.                 temp.add((int) (Math.random() * rand));
  75.             }
  76.         }
  77.         return temp;
  78.     }
  79.  
  80.     public static void printArray(ArrayList<Integer> arr, String abc) {
  81.         String name = abc;
  82.         System.out.printf("%12s: {", name);
  83.         for (int i = 0; i < arr.size(); i++) {
  84.             System.out.printf("%2d", arr.get(i));
  85.             if (i < arr.size() - 1) {
  86.                 System.out.print(",");
  87.             }
  88.         }
  89.         System.out.print("}; \n");
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement