简体   繁体   中英

MergeSort java Graphical representation

Hi I have got merge Sort class I want to plot histogram of sorting array and when try to retrieve data from sorting class on screen appearing only unsorted array and then sorted array. How I have to restructure my sorting class so the full array will be returned on every occurrence of sorting or how I can retrieve merge(array, left, right) (I think this is the part I have to use to create histogram)

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++;
            }
        }
    }
 }

Also when I call for example bubble sort everything working fine so I think I must restructure the mergeClass Please share if you have an idea thank you

This is my printing method

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]);
        }
    }

Bubble sort

    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);
        }
    }
}

Would keeping a record of each step (rather than returning it each step) suffice? If so the following would give you a List of int[] arrays, each storing the value at each step of the sorting process. You could then iterate over the list and display it at your leisure.
The following:

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++;
            }
        }
    }
 }

Would return this:

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]

Ok, I think I understand the problem you have. Does this work for you? Where I've put the println statement you can instead render the current array.

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--;
        }
    }

}

The output that is produced is this:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM