MergeSort not working Helppp please :(
I've been working on this mergeSort that has to read from a file then give me the size and then print out the sorted array. However it's giving me the size and printing out the list without sorting it... Helppp please :)
Here's my code:
Code :
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
*
* @author fluffy
*/
public class Numbers {
int[] data;
int size;
Numbers(String fileName) throws IOException {
data = openFile(fileName);
data = mergeSort(data);
for (int i = 0; i < data.length; i = i + 1) {
System.out.println("*" + data[i]);
}
}
public int[] openFile(String fileName) throws
IOException, ArrayIndexOutOfBoundsException,
InputMismatchException, NoSuchElementException {
int[] a = new int[10];
int i = 0;
int k = 0;
try {
File inFile = new File(fileName);
Scanner in = new Scanner(inFile);
//while(in.hasNext()){
do {
try {
k = in.nextInt();
try {
a[i] = k;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array bound error");
a = grow(a);
a[i] = k;
}
i = i + 1;
} catch (InputMismatchException e) {
in.next();
}
} while (true);
} catch (IOException e) {
//System.out.println("File 2 not found in openFile3 " + e.getMessage());
} catch (NoSuchElementException e) {
size = i - 1;
}
System.out.println("Size of the array " + size);
return a;
}
private static int[] grow(int[] a) {
int[] b = new int[(a.length) * 2];
for (int i = 0; i <
a.length; i =
i + 1) {
b[i] = a[i];
}
return b;
}
public static int[] mergeSort(int[] data) {
if (data.length > 1) {
int mid = data.length / 2;
int[] first = new int[mid];
int[] second = new int[data.length - mid];
for (int i = 0; i < first.length; i = i + 1) {
first[i] = data[i];
}
for (int i = 0; i < second.length; i = i + 1) {
second[i] = data[i + mid];
}
first = mergeSort(first);
second = mergeSort(second);
data = merge(first, second);
}
return data;
}
public static int[] merge(int[] first, int[] second) {
int i = 0, j = 0;
int k = 0;
int[] mergeData = new int[first.length + second.length];
if (i < first.length && j < second.length) {
if (first[i] < second[j]) {
mergeData[k] = first[i];
i = i + 1;
k = k + 1;
} else {
mergeData[k] = second[j];
j = j + 1;
k = k + 1;
}
for (; i < first.length; i = i + 1) {
mergeData[k] = first[i];
k = k + 1;
}
for (; j < second.length; j = j + 1) {
mergeData[k] = second[j];
k = k + 1;
}
}
return mergeData;
}
}
Re: MergeSort not working Helppp please :(
Your MergeSort method never returns the sorted list.
Code :
public static int[] mergeSort(int[] data) {
if (data.length > 1) {
int mid = data.length / 2;
int[] first = new int[mid];
int[] second = new int[data.length - mid];
for (int i = 0; i < first.length; i = i + 1) {
first[i] = data[i];
}
for (int i = 0; i < second.length; i = i + 1) {
second[i] = data[i + mid];
}
first = mergeSort(first);
second = mergeSort(second);
data = merge(first, second);
[b]return data;[/b] // need this line
}
Re: MergeSort not working Helppp please :(
it does have the return data; after that } because if i put it where u placed it at the beggining its going to tell me im missing the return statement... i tried it =/
Re: MergeSort not working Helppp please :(
The sorting code is buggy - it does partially sort the list, but not fully.
I suggest you pick a trivial set of values and step through each method by hand keeping track of what happens on paper.
Re: MergeSort not working Helppp please :(
Ahh, didn't look through it that closely ::blush::
dlorde's right, there is a flaw in your merge method. Do what he says and see if you can figure out what it is :)