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;
}
}
嘿伙计们,我正在尝试使用递归来实现插入排序,它在少量单词上运行良好,但是在实现它之后我得到了 stackoverflow,因为我正在排序的文件大小有很多大约 10,000 个单词。请建议我应该怎么做才能消除错误。
These are the methods I am using for insertion sort using recursion and I am calling them in my constructor.