Я сделал этот код, чтобы отобразить пример сортировки выбора и сортировки слиянием.Я получил это, чтобы сделать свою работу должным образом, но у меня, кажется, есть проблема с форматированием для вывода программы.Под образцом сортировки выбора между оригиналом и отсортированным массивом есть пробел, которого не должно быть.Я почти уверен, что это определенный метод, метод 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