Моего учителя нет на этой неделе, и она дала нам этот код сортировки слиянием. Он написан для массива int[] и мы должны сделать его для массива String[].
public static void mergeSort(int[ ] a, int from, int to)
{ if (from == to) return;
int mid = (from + to) / 2;
// sort the first and the second half
mergeSort(a, from, mid);
mergeSort(a, mid + 1, to);
merge(a, from, mid, to); }// end mergeSort
public static void merge(int[ ] a, int from, int mid, int to)
{ int n = to - from + 1; // size of the range to be merged
int[ ] b = new int[n]; // merge both halves into a temporary array b
int i1 = from; // next element to consider in the first range
int i2 = mid + 1; // next element to consider in the second range
int j = 0; // next open position in b
// as long as neither i1 nor i2 past the end, move the smaller into b
while (i1
Подробнее здесь: [url]https://stackoverflow.com/questions/23064247/java-mergesort-with-strings[/url]
Моего учителя нет на этой неделе, и она дала нам этот код сортировки слиянием. Он написан для массива int[] и мы должны сделать его для массива String[].
Вот ее код:
[code]public static void mergeSort(int[ ] a, int from, int to) { if (from == to) return; int mid = (from + to) / 2; // sort the first and the second half mergeSort(a, from, mid); mergeSort(a, mid + 1, to); merge(a, from, mid, to); }// end mergeSort
public static void merge(int[ ] a, int from, int mid, int to) { int n = to - from + 1; // size of the range to be merged int[ ] b = new int[n]; // merge both halves into a temporary array b int i1 = from; // next element to consider in the first range int i2 = mid + 1; // next element to consider in the second range int j = 0; // next open position in b
// as long as neither i1 nor i2 past the end, move the smaller into b while (i1