OOP // OOP class for Lab 16 import chn.util.*; import java.util.*; public class Lab16OOP { public Lab16OOP() {} // Sort an array of integers from smallest to largest, using a merge sort algorithm. public void mergesort(ArrayList data, int first,int n) { int n1; // Size of the first half of the array int n2; // Size of the second half of the array if (n > 1) { // Compute sizes of the two halves n1 = n / 2; n2 = n - n1; mergesort(data, first, n1); // Sort data[first] through data[first+n1-1] mergesort(data, first + n1, n2); // Sort data[first+n1] to the end // Merge the two sorted halves. merge(data, first, n1, n2); } } // Precondition: data has at least n1 + n2 components starting at data[first]. The first // n1 elements (from data[first] to data[first + n1 – 1] are sorted from smallest // to largest, and the last n2 (from data[first + n1] to data[first + n1 + n2 - 1]) are also // sorted from smallest to largest. // Postcondition: Starting at data[first], n1 + n2 elements of data // have been rearranged to be sorted from smallest to largest. // Note: An OutOfMemoryError can be thrown if there is insufficient // memory for an array of n1+n2 ints. private void merge(ArrayList data, int first, int n1, int n2) { ArrayList temp = new ArrayList(); // Allocate the temporary array int copied = 0; // Number of elements copied from data to temp int copied1 = 0; // Number copied from the first half of data int copied2 = 0; // Number copied from the second half of data int i; // Array index to copy from temp back into data // Merge elements, copying from two halves of data to the temporary array. while ((copied1 < n1) && (copied2 < n2)) { if (data.get(first + copied1).intValue() < data.get(first + n1 + copied2).intValue()) { temp.add((copied++), data.get(first + (copied1++))); } else { temp.add((copied++),data.get(first + n1 + (copied2++))); } } // Copy any remaining entries in the left and right subarrays. while (copied1 < n1) { temp.add((copied++), data.get(first + (copied1++))); } while (copied2 < n2) { temp.add((copied++), data.get(first + n1 + ((copied2++)))); } // Copy from temp back to the data array. for (i = 0; i < n1+n2; i++) data.set(first + i, temp.get(i)); } public void two(ArrayList data) { int i = 0; int tem; ArrayList temp = new ArrayList(); while( i fillArray() { ArrayList arrayList = new ArrayList(); String fileName = "T:\\ap cs a\\old stuff\\lab 13\\numfile.txt"; FileInput file = new FileInput(fileName); arrayList.add(file.readInt()); int i = 0; while(file.hasMoreTokens()) { arrayList.add(file.readInt()); } return arrayList; } public static void main(String[] args) { ConsoleIO input = new ConsoleIO(); ArrayList arrayList = new ArrayList(); arrayList = fillArray(); System.out.println(arrayList); System.out.println("Size is "+arrayList.size()+"\n"); Lab16OOP poopy = new Lab16OOP(); int length = arrayList.size(); poopy.mergesort(arrayList, 0, length); System.out.println(arrayList); System.out.println("Size is "+arrayList.size()+"\n"); poopy.two(arrayList); System.out.println("\n"+arrayList); System.out.println("Size is "+arrayList.size()+"\n"); } } /* RUNNN */