Sie sind auf Seite 1von 5

/*

* The main method of this class creates a MergeSort object,


* calls its init() method, then calls sort() on the result,
* then calls printData() on the object returned by sort(),
* and finally prints the value returned by printData().
*/

class MergeSortTest {

public static void main(String[] args) {


// TO BE COMPLETED
MergeSort m = new MergeSort();
m.init();
m.sort();
System.out.println(m.printData());
}
}

/*
* Objects containing an array of integers together
* with methods for carrying out a merge-sort.
*/
class MergeSort {

// the data values: DO NOT CHANGE THIS DECLARATION


int[] data;

/*
* Initialise the data array with some test data.
* Return the updated MergeSort object.
* DO NOT CHANGE THIS METHOD
*/

public MergeSort init() {


data = new int[10];
data[0] = 5;
data[1] = 67;
data[2] = 0 - 12;
data[3] = 7;
data[4] = 1;
data[5] = 2;
data[6] = 99;
data[7] = 3;
data[8] = 4;
data[9] = 67;
return this;
}

/*
* Sort the data array into ascending order.
* Return the updated MergeSort object.
* DO NOT CHANGE THIS METHOD
*/
public MergeSort sort() {
data = this.mergeSort(data);
return this;
}

/*
* Print the contents of the data array from left to right.
* Return the length of the data array.
*/
public int printData() {
// TO BE COMPLETED
for (int i : data) {

System.out.println(i);
}
return data.length;
}

/*
* Create and return a new array containing an in-order merge of the values
* from a1 and a2.
* This method should assume that a1 and a2 have already been sorted
* into ascending order.
*/
public int[] merge(int[] a1, int[] a2) {
// TO BE COMPLETED
int length = a1.length + a2.length;
int[] sort = new int[length];
int indexa1 = 0, indexa2 = 0, indexRes = 0;

while (indexa1 < a1.length || indexa2 < a2.length) {


if (indexa1 < a1.length && indexa2 < a2.length) {
if (a1[indexa1] <= a2[indexa2]) {
sort[indexRes] = a1[indexa1];
indexa1++;
indexRes++;
} else {
sort[indexRes] = a2[indexa2];
indexa2++;
indexRes++;
}
} else if (indexa1 < a1.length) {
sort[indexRes] = a1[indexa1];
indexa1++;
indexRes++;

} else if (indexa2 < a2.length) {


sort[indexRes] = a2[indexa2];
indexa2++;
indexRes++;
}
}
return sort;
}

/*
* Create and return an array which contains the same data as arr
* but sorted into ascending order.
* It is permitted, but not required, for this method to simply return
* arr if arr is already sorted in ascending order.
*/
public int[] mergeSort(int[] arr) {
// TO BE COMPLETED MAKING USE OF THE merge METHOD
// The basic idea of merge-sort is to split the array
// into two roughly equal parts, recursively call mergeSort
// to sort the two halves, then merge the sorted halves to
// produce the final result);
if (arr.length <= 1) {
return arr;
}
int mid = arr.length / 2;
int[] a1 = new int[mid];
int[] a2;
if (arr.length % 2 == 0) {
a2 = new int[mid];
} else {
a2 = new int[mid + 1];
}

int[] sorted = new int[arr.length];


for (int i = 0; i < mid; i++) {
a1[i] = arr[i];
}
int z = 0;
for (int y = mid; y < arr.length; y++) {
if (z < a2.length) {
a2[z] = arr[y];
z++;
}
}
a1 = mergeSort(a1);
a2 = mergeSort(a2);

sorted = merge(a1, a2);

return sorted;
}
}

Das könnte Ihnen auch gefallen