您能否在下面解释几行(我在每行需要解释的前面加上?)。提前致谢 !我知道:对具有n个元素的输入序列S进行合并排序包括三个步骤:划分:将S划分为两个序列S1和S2,每个序列分别约n / 2个元素递归:递归地对S1和S2进行排序征服:合并S1和S2成为唯一的排序序列
但是,当我阅读代码时,我迷失了方向,请您指导我。
public class MergeSort {
public static int[] mergeSort(int [] list) {
if (list.length <= 1) {
return list;
}
// Split the array in half
int[] first = new int[list.length / 2]; // ok
int[] second = new int[list.length - first.length]; // ?
System.arraycopy(list, 0, first, 0, first.length); // ?
System.arraycopy(list, first.length, second, 0, second.length); // not sure ?
// Sort each half
mergeSort(first); // ok
mergeSort(second); // ok
// Merge the halves together, overwriting the original array
merge(first, second, list); // ok
return list; // ok
}
private static void merge(int[] first, int[] second, int [] result) { // explain in general ?
// Merge both halves into the result array
// Next element to consider in the first array
int iFirst = 0;
// Next element to consider in the second array
int iSecond = 0;
// Next open position in the result
int j = 0;
// As long as neither iFirst nor iSecond is past the end, move the
// smaller element into the result.
while (iFirst < first.length && iSecond < second.length) { // ??!!
if (first[iFirst] < second[iSecond]) {
result[j] = first[iFirst];
iFirst++;
} else {
result[j] = second[iSecond];
iSecond++;
}
j++;
}
// copy what's left
System.arraycopy(first, iFirst, result, j, first.length - iFirst);
System.arraycopy(second, iSecond, result, j, second.length - iSecond);
}
public static void main(String args[]) throws Exception
{
String list="";
int i=0,n=0;
MergeSort s= new MergeSort();
ArrayList<Integer> arrlist=new ArrayList<Integer>();
System.out.println(" ");
System.out.println(" ");
System.out.println("Please enter the list of elements,one element per line");
System.out.println(" write 'STOP' when list is completed ");
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
while(!(list=bf.readLine()).equalsIgnoreCase("stop")){
int intelement=Integer.parseInt(list);
arrlist.add(intelement);
}
int elementlist[] = new int[arrlist.size()];
Iterator<Integer> iter = arrlist.iterator();
for (int j=0;iter.hasNext();j++) {
elementlist[j] = iter.next();
}
elementlist=mergeSort(elementlist); // ?
System.out.println(" ");
System.out.println(" ");
System.out.println(" ");
System.out.println("Values after Merge Sort : ");
for (int j=0;j<elementlist.length;j++) {
System.out.println(elementlist[j]+" ");
}
}
}
在int[] first = new int[list.length / 2];
andint[] second = new int[list.length - first.length];
方法中,将列表分为2个数组,可以说您的列表的长度为5,第一个的大小为2,第二个为3。在
System.arraycopy(list, 0, first, 0, first.length);
System.arraycopy(list, first.length, second, 0, second.length);
您从列表数组中填充第list[0],list[1]
一个数组和list[2],list[3],list[4]
第二个数组(复制到第一个数组和第二个数组)。
在private static void merge(int[] first, int[] second, int [] result)
合并first
和second
数组并result
在while
循环中覆盖数组的过程中,您将第一个和第二个数组的第一个元素进行比较,然后移动最小的元素,result[0]
然后通过使用if and else
语句将指针相应地增加到数组,现在这两个元素中的元素都最小first
以及second
放置在中的数组result[0]
。
您增加j
,这意味着在此迭代中,您将放入第二个最小的元素result[1]
,依此类推。
在elementlist=mergeSort(elementlist);
您elementlist
通过调用您的mergeSort(int [] list);
方法进行排序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句