Print only the numbers, which have been changed

Crezion :

I got curious about a Merge-sorting code.

Description: This code creates two auxillary arrays left and right and store alternate array elements in them and then copying all elements of left and right subarrays back to original array and printing them. So instead of printing back to the original array, how would it be possible to only print the moved numbers?

    class Project { 

    static void join(int arr[], int left[], int right[],int l, int m, int r){ 
        int i; 
        for (i = 0; i <= m - l; i++) 
            arr[i] = left[i]; 
        for (int j = 0; j < r - m; j++) 
            arr[i + j] = right[j]; 
    } 

    static void split(int arr[], int left[], int right[],int l, int m, int r) { 
        for (int i = 0; i <= m - l; i++) 
            left[i] = arr[i * 2]; 
        for (int i = 0; i < r - m; i++) 
            right[i] = arr[i * 2 + 1]; 
    } 

    static void generateWorstCase(int arr[], int l, int r) { 
        if (l < r) { 
            int m = l + (r - l) / 2; 
            int[] left = new int[m - l + 1]; 
            int[] right = new int[r - m]; 
            split(arr, left, right, l, m, r); 
            generateWorstCase(left, l, m); 
            generateWorstCase(right, m + 1, r); 
            join(arr, left, right, l, m, r); 
        } 
    } 
 public static void main (String[] args) { 
        int arr[] = { 10, 11, 12, 13, 14, 15, 16 }; 
        int n = arr.length; 
        System.out.println("Sorted array is"); 
        System.out.println(Arrays.toString(arr)); 
        generateWorstCase(arr, 0, n - 1); 
        System.out.println("\nInput array that will result in worst case of merge sort is: \n"); 
        System.out.println(Arrays.toString(arr)); 
    } 
 } 

Here's the output:

System.out.println(Arrays.toString(arr)); 

My question is.. I would ask, can you, based on the code, only have the output as, like the numbers being moved, and not the entire array?

Example:

The input is:  

{ 10 20 30 40 50 }

The output is: 

{ 10 50 30 20 40 }

My Desired Output:

{ 50 20 40 }

(The number of inputs varies according to the number of output)..

How would this happen?

Arvind Kumar Avinash :

Do it as follows:

public static void main(String[] args) {
    int arr[] = { 10, 11, 12, 13, 14, 15, 16 };
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < arr.length; i++) {
        map.put(arr[i], i);
    }
    int n = arr.length;
    System.out.println("Sorted array is");
    System.out.println(Arrays.toString(arr));
    generateWorstCase(arr, 0, n - 1);
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < arr.length; i++) {
        if (map.get(arr[i]) != i) {
            list.add(arr[i]);
        }
    }
    System.out.println("\nInput array that will result in worst case of merge sort is: \n" + list);
}

Output:

Sorted array is
[10, 11, 12, 13, 14, 15, 16]

Input array that will result in worst case of merge sort is: 
[14, 16, 11, 13]

Another solution:

public static void main(String[] args) {
    int arr[] = { 10, 11, 12, 13, 14, 15, 16 };
    int[] original = Arrays.copyOf(arr, arr.length);
    int n = arr.length;
    System.out.println("Sorted array is");
    System.out.println(Arrays.toString(arr));
    generateWorstCase(arr, 0, n - 1);
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < arr.length; i++) {
        if (original[i] != arr[i]) {
            list.add(arr[i]);
        }
    }
    System.out.println("\nInput array that will result in worst case of merge sort is: \n" + list);
}

Output:

Sorted array is
[10, 11, 12, 13, 14, 15, 16]

Input array that will result in worst case of merge sort is: 
[14, 16, 11, 13]

[Update]

You have requested to change the format of the output so that the numbers are not bounded by []. Note that this is how Arrays.toString or List::toString returns the string. If you do not want an array or a List, you can do it simply as:

public static void main(String[] args) {
    int arr[] = { 10, 11, 12, 13, 14, 15, 16 };
    int[] original = Arrays.copyOf(arr, arr.length);
    int n = arr.length;
    System.out.println("Sorted array is");
    System.out.println(Arrays.toString(arr));
    generateWorstCase(arr, 0, n - 1);
    StringBuilder s = new StringBuilder();
    int i;
    for (i = 0; i < arr.length; i++) {
        if (original[i] != arr[i]) {
            s.append(arr[i]).append(", ");
        }
    }
    String output = s.substring(0, s.lastIndexOf(","));
    System.out.println("\nInput array that will result in worst case of merge sort is: \n" + output);
}

Output:

Sorted array is
[10, 11, 12, 13, 14, 15, 16]

Input array that will result in worst case of merge sort is: 
14, 16, 11, 13

If you want to change the format of the output while keeping the List, you can do it as follows:

public static void main(String[] args) {
    int arr[] = { 10, 11, 12, 13, 14, 15, 16 };
    int[] original = Arrays.copyOf(arr, arr.length);
    int n = arr.length;
    System.out.println("Sorted array is");
    System.out.println(Arrays.toString(arr));
    generateWorstCase(arr, 0, n - 1);
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < arr.length; i++) {
        if (original[i] != arr[i]) {
            list.add(arr[i]);
        }
    }
    System.out.println("\nInput array that will result in worst case of merge sort is: \n"
            + list.toString().replace("[", "").replace("]", ""));
}

Output:

Sorted array is
[10, 11, 12, 13, 14, 15, 16]

Input array that will result in worst case of merge sort is: 
14, 16, 11, 13

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=18703&siteId=1