我编写了这段代码,以显示选择排序和合并排序的示例。我已经得到它来正确执行它的工作,但是我似乎在程序输出中存在格式问题。在选择排序的样本下,原始数组和排序数组之间不应有空格。我很确定它在某种方法中,即sort2方法,但是我不确定。感谢帮助!
/*
*
*
*/
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
答案 0 :(得分:2)
原始数组和排序后的数组之间应该有一个空格。
这是因为在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
这将导致多余的空间。只需将其更改为:
out.println("Sorted array: " + Arrays.toString(arr));
答案 1 :(得分:0)
out.println("Original array: " + Arrays.toString(arr));
sort2(arr);
out.println("\nSorted array: " + Arrays.toString(arr));
println打印到新行,然后以新行\nSorted