java - What's wrong with my modified merge sort implementation? -
i'm trying write modified merge sorting excercise. stuck in why code doesn't work , cannot se problem?! hint or appreciate. in advance.
here code:
public class mergesort { public static void mergesort(int[] list) { mergesort(list, 0, list.length - 1); } private static void mergesort(int[] list, int low, int high) { if (low < high) { int middle = (high + low) / 2; mergesort(list, low, middle); mergesort(list, middle + 1, high); int[] temp = merge(list, low, high); system.arraycopy(temp, 0, list, low, high - low + 1); } } private static int[] merge(int[] list, int low, int high) { int low1 = low; int high1 = high; int mid = (low + high) / 2; int end_low = mid; int start_high = mid + 1; while ((low <= end_low) && (start_high <= high1)) { if (list[low] < list[start_high]) { low++; } else { int temp = list[high - low + 1]; (int k = start_high - 1; k >= low; k--) { list[k + 1] = list[k]; } list[low] = temp; low++; end_low++; start_high++; } } return list; } public static void main(string[] args) { int[] list = { 2, 3, 2, 5, 6, 1, -2, 3, 14, 12, -5 }; mergesort(list); (int = 0; < list.length; i++) system.out.print(list[i] + " "); } }
Comments
Post a Comment