public static void insertionSortRecursion(String a[], int first, int last) {
if (first < last) {
//sort all but last
insertionSortRecursion(a, first, last - 1);
insertInOrder(a[last], a, first, last -1);
}
}
private static void insertInOrder(String element, String[] a, int first, int last) {
// System.out.println(last - 1);
// System.out.println(element);
// System.out.println(a[last]);
if (element.compareTo(a[last]) >= 0) {
a[last + 1] = element;
} else if(first < last) {
a[last + 1] = a[last];
insertInOrder(element, a, first, last - 1);
} else {
a[last + 1] = a[last];
a[last] = element;
}
}
嗨, 我想那種使用遞歸這是對少數詞的做工精細,以實現插入,但我實現它,因爲文件的大小,我以後越來越計算器排序有大約10,000個單詞。請建議我應該怎麼做以消除錯誤。#1的錯誤而執行插入排序使用遞歸
These are the methods I am using for insertion sort using recursion and I am calling them in my constructor.
你爲什麼要使用遞歸實現它,你可以很容易地編寫迭代插入排序(無需遞歸地使用堆棧)。 – Michael
'請建議我應該怎麼做以消除錯誤'。不要使用遞歸。 – UmNyobe
順便說一句:插入排序在這樣大的數組上效率不高 – Michael