user10327024
user10327024

Reputation:

Unknown Spacing Issue in Java Output

I have made this code to display an example of both a selection sort and a merge sort. I have gotten it to do it's job properly, but I seem to have a formatting issue for the output of the program. Underneath the sample of the selection sort, there is a space between the original and the sorted array when there should not be. I'm pretty sure it's in a certain method, the sort2 method, but I'm not sure. Appreciate the help!

    /*
 * 
 * 
 */

package asgn03;

import static java.lang.System.arraycopy;
import static java.lang.System.out;
import java.util.Arrays;
import java.util.Random;

/**
 *
 * @author millerh9
 */
public class Asgn03 {
    final static Random rand = new Random();

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    // TODO code application logic here
    int[] arr = new int[20];
    for (int j = 0; j < arr.length; j++) {
        arr[j] = rand.nextInt(100);
    }
    out.println("CPS 151 Assignment 3 By Harper Miller");
    out.println();
    out.println("Demonstrate Merge Sort");
    out.println("Original array: " + Arrays.toString(arr));
    sort(arr);
    out.println("Sorted array: " + Arrays.toString(arr));
    out.println();
    out.println("Demonstrate Selection Sort");
    out.println("Original array: " + Arrays.toString(arr));
    sort2(arr);
    out.println("\nSorted array: " + Arrays.toString(arr));

} // end main

    // sorts an array of integers (shell function for public use)
public static void sort(final int[] arr) {
    sort(arr, 0, arr.length - 1, 0);
} // end public sort

// sort the segment arr[low:high], its length is (high - low + 1)
private static void sort(final int[] arr, final int low, final int high, 
        int level) {
    // Base case: a segment of length <= 1 is sorted by definition
    if (high - low <= 0)
        return;

    // trace output



    // Recursive case: segment length > 1
    // Consider arr[low:high] split into arr[low:mid] and arr[mid+1:high]
    // and sort them recursively where mid = (low + high) / 2
    final int mid = (low + high) / 2;
    sort(arr, low, mid, level + 1);
    sort(arr, mid + 1, high, level + 1);

    // merge arr[low:mid] and arr[mid+1:high] into arr[low:mid]
    merge(arr, low, high, level);
} // end private sort

private static void merge(final int[] arr, final int low, final int high, 
        int level) {
    // array segment arr[low:high] contains two sorted subsegments arr[low:mid]
    // and arr[mid+1:high] where mid = (low + high) / 2
    final int mid = (low + high) / 2;
    // temporary array into which the segments arr[low:mid] and arr[mid+1:high]
    // are merged. The size needed is high - low + 1
    final int[] tempArr = new int[high - low + 1];

    // index variables set into segments to merge and into the tempArray
    int index1 = low, index2 = mid + 1, index3 = 0;




    while (index1 <= mid && index2 <= high) {
        if (arr[index1] <= arr[index2]) {   // copy from first segment
            tempArr[index3] = arr[index1];
            index1++;
        } else {                                         // copy from second segment
            tempArr[index3] = arr[index2];
            index2++;
        } // end if
        index3++;
    } // end loop

    // number of values left over (not copied into tempArr yet)
    // from first segment: mid - index1 + 1
    // from second segment: high - index2 + 1
    // Note that only one segment will have leftovers so 
    // one of the following arraycopy's will not do anything
    arraycopy(arr, index1, tempArr, index3, mid - index1 + 1);
    arraycopy(arr, index2, tempArr, index3, high - index2 + 1);

    // copy back from tempArr to arr[low:high]
    arraycopy(tempArr, 0, arr, low, tempArr.length);
} // end merge

// sort the segment arr[low:high], its length is (high - low + 1)
private static String segmentToString(int[] arr, int low, int high) {
    if (low > high)
        return "[]";

    // at least one value
    String str = "[" + arr[low];
    for (int index = low + 1; index <= high; index++)
        str = str + ", " + arr[index];

    return str + "]";
} // end segmentToString
private static void sort2(int[] arr) {
    final int n = arr.length;     // size of array

    for (int k = 0; k < n - 1; k++) {
        int minIndex = getIndexOfMin(arr, k, n - 1);
        swap(arr, k, minIndex);

    } // end outer loop
} // end sort

private static int getIndexOfMin(int[] arr, int startAt, int lastIndex) {
    int minIndex = startAt;

    for (int index = startAt + 1; index <= lastIndex; index++) {
        if (arr[index] < arr[minIndex])     // found a smaller value
            minIndex = index;
    } // end loop
    return minIndex;
} // end method

private static void swap(int[] arr, int k, int i) {
    int temp = arr[i];
    arr[i] = arr[k];
    arr[k] = temp;
} // end swap


} // end class

Upvotes: 1

Views: 32

Answers (2)

GBlodgett
GBlodgett

Reputation: 12819

there is a space between the original and the sorted array when there should not be.

This is because you have a newline character in your println:

out.println("Original array: " + Arrays.toString(arr));
//Newline printed by the println statement
sort2(arr);
out.println("\nSorted array: " + Arrays.toString(arr));
         // ^^^newline right here

Which results in the extra space. Simply change it to:

out.println("Sorted array: " + Arrays.toString(arr));

Upvotes: 2

KameeCoding
KameeCoding

Reputation: 723

out.println("Original array: " + Arrays.toString(arr));
sort2(arr);
out.println("\nSorted array: " + Arrays.toString(arr));

println prints to a new line and you start your string with a new line \nSorted

Upvotes: 0

Related Questions