递归选择排序(Java Eclipse Neon 2)
Recursive Selection Sorting (Java Eclipse Neon 2)
好的好的。我一直在研究 Java 中的递归选择排序。我已经完成阅读、谷歌搜索、堆栈溢出,但仍然无法弄清楚。我认为由于过于复杂,我花在代码上的时间越多,代码就越糟糕。我见过的所有示例都使用多个参数,而单个参数让我失望。
下面是递归方法和驱动。给出了前 3 个 if 语句,所以我假设是必需的。
public static void selectionSort_Rec(int[] arr)
{
if(arr == null) throw new NullPointerException();
if(arr.length == 0) throw new IllegalArgumentException();
if(arr.length == 1) return;
int startIndex = 0;
if ( startIndex >= arr.length - 1 )
return;
int minIndex = startIndex;
for ( int index = startIndex + 1; index < arr.length; index++ )
{
if (arr[index] < arr[minIndex] )
minIndex = index;
}
int temp = arr[startIndex];
arr[startIndex] = arr[minIndex];
arr[minIndex] = temp;
startIndex++;
selectionSort_Rec(arr);
}
// Driver method
public static void main(String args[])
{
int arr[] = {3, 1, 5, 2, 7, 0};
// Calling function
selectionSort_Rec(arr);
for(int i :arr){
System.out.print(i);
}
}
你的代码有问题。
首先你使用 startIndex
并从你的数组中找到合适的数字,然后在代码末尾递增它是多余的,因为当再次调用函数时,它再次使用 0 。每个函数调用都有自己的变量,因此下一次调用时,函数会创建新的 startIndex
并再次使用为零的变量。
您必须将它传递给函数并在每次下一个函数调用时递增。因此,您的基础检查不再为真,它会更改为检查,直到我们到达数组的末尾。
这行代码也是多余的,因为当我们到达这一行时,我们知道 arr.lenghth()
不止一个。 (但是我们的代码逻辑改变了,也没有必要)
if ( startIndex >= arr.length - 1 )
return;
return 当达到 1 这样的基本条件时更好,并且不需要抛出异常,因为当它是 1 时,我们 return 而不会降低。 (对于零或空数组,条件始终为假。您也不要从数组中删除任何内容)
我将递归函数定义为从发送给它的起始索引对数组进行排序的函数。
这是结果:
public class GFG
{
public static void selectionSort_Rec(int[] arr) {
selectionSortHelper(arr, 0);
}
static void selectionSortHelper(int[] arr, int startIndex) {
if (startIndex >= arr.length)
return;
int minIndex = startIndex;
for (int index = startIndex + 1; index < arr.length; index++)
{
if (arr[index] < arr[minIndex])
minIndex = index;
}
int temp = arr[startIndex];
arr[startIndex] = arr[minIndex];
arr[minIndex] = temp;
selectionSortHelper(arr, startIndex + 1);
}
// Driver method
public static void main(String args[]) {
int arr[] = {3, 1, 5, 2, 7, 0};
// Calling function
selectionSort_Rec(arr);
for (int i : arr)
{
System.out.print(i + " ");
}
}
}
希望这就是你想要的。
好的好的。我一直在研究 Java 中的递归选择排序。我已经完成阅读、谷歌搜索、堆栈溢出,但仍然无法弄清楚。我认为由于过于复杂,我花在代码上的时间越多,代码就越糟糕。我见过的所有示例都使用多个参数,而单个参数让我失望。
下面是递归方法和驱动。给出了前 3 个 if 语句,所以我假设是必需的。
public static void selectionSort_Rec(int[] arr)
{
if(arr == null) throw new NullPointerException();
if(arr.length == 0) throw new IllegalArgumentException();
if(arr.length == 1) return;
int startIndex = 0;
if ( startIndex >= arr.length - 1 )
return;
int minIndex = startIndex;
for ( int index = startIndex + 1; index < arr.length; index++ )
{
if (arr[index] < arr[minIndex] )
minIndex = index;
}
int temp = arr[startIndex];
arr[startIndex] = arr[minIndex];
arr[minIndex] = temp;
startIndex++;
selectionSort_Rec(arr);
}
// Driver method
public static void main(String args[])
{
int arr[] = {3, 1, 5, 2, 7, 0};
// Calling function
selectionSort_Rec(arr);
for(int i :arr){
System.out.print(i);
}
}
你的代码有问题。
首先你使用 startIndex
并从你的数组中找到合适的数字,然后在代码末尾递增它是多余的,因为当再次调用函数时,它再次使用 0 。每个函数调用都有自己的变量,因此下一次调用时,函数会创建新的 startIndex
并再次使用为零的变量。
您必须将它传递给函数并在每次下一个函数调用时递增。因此,您的基础检查不再为真,它会更改为检查,直到我们到达数组的末尾。
这行代码也是多余的,因为当我们到达这一行时,我们知道 arr.lenghth()
不止一个。 (但是我们的代码逻辑改变了,也没有必要)
if ( startIndex >= arr.length - 1 )
return;
return 当达到 1 这样的基本条件时更好,并且不需要抛出异常,因为当它是 1 时,我们 return 而不会降低。 (对于零或空数组,条件始终为假。您也不要从数组中删除任何内容)
我将递归函数定义为从发送给它的起始索引对数组进行排序的函数。
这是结果:
public class GFG
{
public static void selectionSort_Rec(int[] arr) {
selectionSortHelper(arr, 0);
}
static void selectionSortHelper(int[] arr, int startIndex) {
if (startIndex >= arr.length)
return;
int minIndex = startIndex;
for (int index = startIndex + 1; index < arr.length; index++)
{
if (arr[index] < arr[minIndex])
minIndex = index;
}
int temp = arr[startIndex];
arr[startIndex] = arr[minIndex];
arr[minIndex] = temp;
selectionSortHelper(arr, startIndex + 1);
}
// Driver method
public static void main(String args[]) {
int arr[] = {3, 1, 5, 2, 7, 0};
// Calling function
selectionSort_Rec(arr);
for (int i : arr)
{
System.out.print(i + " ");
}
}
}
希望这就是你想要的。