嗨我有合并排序类我想绘制排序数组的直方图,当试图从屏幕上的排序类中检索数据时只显示未排序的数组然后排序数组。我如何重构我的排序类,以便在每次排序时都返回完整的数组,或者我如何检索合并(数组,左,右)(我认为这是我必须用来创建直方图的部分)
package mergeSort;
import java.util.*;
public class MergeSort {
public static void main(String[] args) {
int[] list = {14, 32, 67, 76, 23, 41, 58, 85};
System.out.println("before: " + Arrays.toString(list));
mergeSort(list);
System.out.println("after: " + Arrays.toString(list));
}
public static void mergeSort(int[] array) {
if (array.length > 1) {
// split array into two halves
int[] left = leftHalf(array);
int[] right = rightHalf(array);
// recursively sort the two halves
mergeSort(left);
mergeSort(right);
// merge the sorted halves into a sorted whole
merge(array, left, right);
}
}
// Returns the first half of the given array.
public static int[] leftHalf(int[] array) {
int size1 = array.length / 2;
int[] left = new int[size1];
for (int i = 0; i < size1; i++) {
left[i] = array[i];
}
return left;
}
// Returns the second half of the given array.
public static int[] rightHalf(int[] array) {
int size1 = array.length / 2;
int size2 = array.length - size1;
int[] right = new int[size2];
for (int i = 0; i < size2; i++) {
right[i] = array[i + size1];
}
return right;
}
public static void merge(int[] result, int[] left, int[] right) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
for (int i = 0; i < result.length; i++) {
if (i2 >= right.length || (i1 < left.length &&
left[i1] <= right[i2])) {
result[i] = left[i1]; // take from left
i1++;
} else {
result[i] = right[i2]; // take from right
i2++;
}
}
}
}
此外,当我打电话给例如冒泡排序一切正常,所以我认为我必须重组mergeClass 如果您有任何想法,请分享,谢谢
这是我的打印方法
int Value = 100;
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int edge= 50;
for (int i = 0; i < array.length; i++) {
g.drawLine(i + offSet, Value + edge, i + edge,
Value + edge- array[i]);
}
}
冒泡排序
public void bubbleSort(int[] a) throws InterruptedException {
for (int i = 0; i < a.length; i++) {
for (int j = 1; j < (a.length - i); j++) {
if (a[j - 1] > a[j])
swap(a, j - 1, j);
}
}
}
答案 0 :(得分:0)
保留每一步的记录(而不是每步返回)是否足够?如果是这样,以下内容将为您提供一个int []数组列表,每个数组都存储排序过程每一步的值。然后,您可以遍历列表并在闲暇时显示它 以下内容:
package mergeSort;
import java.util.*;
public class MergeSort {
public static void main(String[] args) {
int[] list = {14, 32, 67, 76, 23, 41, 58, 85};
List<int[]> listOfLists = new ArrayList<int[]>();
System.out.println("before: " + Arrays.toString(list));
mergeSort(list,listOfLists);
System.out.println("after: " + Arrays.toString(list));
System.out.println("Each step:");
for(int[] arrList : listOfLists)
System.out.println(Arrays.toString(arrList));
}
public static void mergeSort(int[] array, List<int[]> listOfLists) {
if (array.length > 1) {
// split array into two halves
int[] left = leftHalf(array);
int[] right = rightHalf(array);
// recursively sort the two halves
mergeSort(left,listOfLists);
mergeSort(right,listOfLists);
// merge the sorted halves into a sorted whole
merge(array, left, right);
listOfLists.add(array);
}
}
// Returns the first half of the given array.
public static int[] leftHalf(int[] array) {
int size1 = array.length / 2;
int[] left = new int[size1];
for (int i = 0; i < size1; i++) {
left[i] = array[i];
}
return left;
}
// Returns the second half of the given array.
public static int[] rightHalf(int[] array) {
int size1 = array.length / 2;
int size2 = array.length - size1;
int[] right = new int[size2];
for (int i = 0; i < size2; i++) {
right[i] = array[i + size1];
}
return right;
}
public static void merge(int[] result, int[] left, int[] right) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
for (int i = 0; i < result.length; i++) {
if (i2 >= right.length || (i1 < left.length &&
left[i1] <= right[i2])) {
result[i] = left[i1]; // take from left
i1++;
} else {
result[i] = right[i2]; // take from right
i2++;
}
}
}
}
会回复:
before: [14, 32, 67, 76, 23, 41, 58, 85]
after: [14, 23, 32, 41, 58, 67, 76, 85]
Each step:
[14, 32]
[67, 76]
[14, 32, 67, 76]
[23, 41]
[58, 85]
[23, 41, 58, 85]
[14, 23, 32, 41, 58, 67, 76, 85]
答案 1 :(得分:0)
好的,我想我理解你遇到的问题。这对你有用吗?在我放置println语句的地方你可以改为渲染当前数组。
package mergeSort;
import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) {
int[] list = { 14, 32, 76, 67, 41, 23, 58, 85 };
System.out.println("before: " + Arrays.toString(list));
mergeSort(list, 0, list.length - 1);
System.out.println("after: " + Arrays.toString(list));
}
public static void mergeSort(int[] array, int left, int right) {
int mid = 0;
if (right > left) {
mid = (right + left) / 2;
// sort left
mergeSort(array, left, mid);
// sort right
mergeSort(array, mid + 1, right);
// merge them
merge(array, left, mid+1, right);
// PUT YOUR HOOK TO DRAW THE ARRAY HERE
System.out.println("during: " + Arrays.toString(array));
}
}
public static void merge(int[] numbers, int left, int mid, int right) {
int[] temp = new int[numbers.length];
int i, left_end, num_elements, tmp_pos;
left_end = (mid - 1);
tmp_pos = left;
num_elements = (right - left + 1);
while ((left <= left_end) && (mid <= right)) {
if (numbers[left] <= numbers[mid])
temp[tmp_pos++] = numbers[left++];
else
temp[tmp_pos++] = numbers[mid++];
}
while (left <= left_end)
temp[tmp_pos++] = numbers[left++];
while (mid <= right)
temp[tmp_pos++] = numbers[mid++];
for (i = 0; i < num_elements; i++){
numbers[right] = temp[right];
right--;
}
}
}
产生的输出是:
before: [14, 32, 76, 67, 41, 23, 58, 85]
during: [14, 32, 76, 67, 41, 23, 58, 85]
during: [14, 32, 67, 76, 41, 23, 58, 85]
during: [14, 32, 67, 76, 41, 23, 58, 85]
during: [14, 32, 67, 76, 23, 41, 58, 85]
during: [14, 32, 67, 76, 23, 41, 58, 85]
during: [14, 32, 67, 76, 23, 41, 58, 85]
during: [14, 23, 32, 41, 58, 67, 76, 85]
after: [14, 23, 32, 41, 58, 67, 76, 85]