java - How to set my ArrayList<integer> equal to a function? -
hello doing merge sort , think correct having hard time last 2 lines of merge_sort function... on second last line says "integer cannot resolved variable, arraylist cannot resolved variable, type mismatch: cannot convert arraylist int[]." , last line says: "type mismatch cannot convert int[] arraylist." how can fix this? appreciated!!
public static arraylist<integer> merge_sort(arraylist<integer> b) { if (b.size() <= 1) system.out.println(b); int midpoint = b.size()/2; arraylist<integer> left = new arraylist<integer>(midpoint); arraylist<integer> right; if(b.size() % 2 == 0) right = new arraylist<integer>(midpoint); else right = new arraylist<integer>(midpoint + 1); int[] result = new int[b.size()]; (int = 0; < midpoint; i++) left.set(i, b.get(i)); int x = 0; (int j = midpoint; j < b.size(); j++) { if(x < right.size()) right.set(x, b.get(j)); x++; } left = merge_sort(left); right = merge_sort(right); result = merge(left, right); return result; } public static arraylist<integer> merge(arraylist<integer> left, arraylist<integer> right) { int lengthresult = left.size() + right.size(); arraylist<integer> result = new arraylist<integer>(lengthresult); int indexl = 0; int indexr = 0; int indexres = 0; while (indexl < left.size() || indexr < right.size()) { if(indexl < left.size() && indexr < right.size()) { if (left.get(indexl) <= right.get(indexr)) { result.set(indexres, left.get(indexl)); indexl++; indexres++; } else { result.set(indexres, right.get(indexr)); indexr++; indexres++; } } else if (indexl < left.size()) { result.set(indexres, left.get(indexl)); indexl++; indexres++; } else if (indexr < right.size()) { result.set(indexres, right.get(indexr)); indexr++; indexres++; } } return result; }
remove line
int[] result = new int[b.size()];
and change this
list<integer> result = merge(left, right);
Comments
Post a Comment