Java 数组的选择排序方法
我从一个关于选择排序的网站上得到了这个方法,我需要检查它是如何工作的:Java 数组的选择排序方法,java,arrays,recursion,Java,Arrays,Recursion,我从一个关于选择排序的网站上得到了这个方法,我需要检查它是如何工作的: import java.util.Arrays; public class SelectionSort { public static void selectionSort(int[] data, int low, int high) { if (low < high) { swap(data, low, findMinIndex(data, low)); selectionS
import java.util.Arrays;
public class SelectionSort {
public static void selectionSort(int[] data, int low, int high) {
if (low < high) {
swap(data, low, findMinIndex(data, low));
selectionSort(data, low + 1, high);
}
}
public static void swap(int[] array, int index1, int index2) {
int tmp = array[index1];
array[index1] = array[index2];
array[index2] = tmp;
}
public static int findMinIndex(int[] data, int index) {
int minIndex;
if (index == data.length - 1)
return index;
minIndex = findMinIndex(data, index + 1);
if (data[minIndex] < data[index])
return minIndex;
else
return index;
}
public static void main (String[] args) {
int[] numbers = {3, 15, 1, 9, 6, 12, 21, 17, 8};
SelectionSort.selectionSort(numbers, 0, numbers.length);
System.out.println(Arrays.toString(numbers));
}
}
导入java.util.array;
公共类选择排序{
公共静态无效选择排序(int[]数据、int低、int高){
如果(低<高){
交换(数据,低位,findMinIndex(数据,低位));
选择排序(数据,低+1,高);
}
}
公共静态无效交换(int[]数组,int index1,int index2){
int tmp=数组[index1];
数组[index1]=数组[index2];
数组[index2]=tmp;
}
公共静态int findMinIndex(int[]数据,int索引){
int-minIndex;
if(index==data.length-1)
收益指数;
minIndex=findMinIndex(数据,索引+1);
if(数据[minIndex]<数据[index])
返回minIndex;
其他的
收益指数;
}
公共静态void main(字符串[]args){
int[]数字={3,15,1,9,6,12,21,17,8};
SelectionSort.SelectionSort(数字、0、数字、长度);
System.out.println(Arrays.toString(numbers));
}
}
你能帮我解开谜团吗?为什么int高是数组的最后一个索引?怎么做?这是数组的大小,用来知道什么时候结束 每个周期: 第一:检查索引高于low(low从0开始)的任何元素是否较低,如果是这种情况,则交换它们。 第二:低增长 当low不低于high(数组大小)时,它停止 请参见选择排序的定义:
它是数组的大小,用于知道何时结束 每个周期: 第一:检查索引高于low(low从0开始)的任何元素是否较低,如果是这种情况,则交换它们。 第二:低增长 当low不低于high(数组大小)时,它停止 请参见选择排序的定义:
在此特定代码中
findMinIndex
将给定索引中的元素与它前面的所有元素(索引较高的元素)进行比较,直到数组的最后一个元素
因此,如果您有一个数组:
int[]a={7,4,2,6}代码>
然后调用findMinIndex(a,0)代码>它将首先检查索引0之后是否有元素。这就是这部分所做的index==data.length-1
。如果后面没有元素,它只返回传递的索引。但是索引0之后显然有一个元素,因为数组的长度为4
既然我们已经确认在我们的索引
之后有元素,现在是时候获得索引
之后最小的元素的索引了。通过这种方式,我们可以将索引处的元素与前面的所有元素进行比较,以查看在索引
到数组.length-1(包括)的范围内哪个元素最小。这是通过递归实现的:
minIndex=findMinIndex(数据,索引+1)代码>
所以接下来的几个电话是这样的:
findMinIndex(data, 1);
// is there an element after 1? There is. So we end up calling findMinIndex again...
findMinIndex(data, 2); // is there an element after 2? Yes. Recurse...
findMinIndex(data, 3); // is there an element after 3? No. That's the end of the array
// remember this part? it's used now to finally terminate the recursion
if (index == data.length - 1)
return index; // this equals 3
// low == 1 and high == 4
if (1 < 4) { // true
swap(a, 1, findMinIndex(data, 1));
selectionSort(a, 1 + 1, 4);
}
现在递归调用开始展开
// index == 2 because the 2nd to last index is 2. remember our array has length 4 and indices 0-3.
minIndex = 3; // this is the index of the last element
if (data[3] < data[2]) { // look at our array 'a', is 6 less than 2?
return 3; // No it is not. so this is not returned
} else {
return 2; // we end up return the index (2) of the smaller element (2)
}
请记住,我们发现0(包括0)之后的最小元素的索引为2。因此,上述代码可以替换为:
if (0 < 4) { // True of course
swap(a, 0, 2);
selectionSort(data, 0 + 1, 4);
}
记住我们的数组现在是{2,4,7,6}
。这意味着索引1(值4)之后的最小元素实际上只有4。因此,上述代码等于:
// low == 1 and high == 4
if (1 < 4) { // true
swap(a, 1, 1);
selectionSort(a, 1 + 1, 4);
}
// low == 2 and high == 4
if (2 < 4) { // true
swap(a, 2, 3);
selectionSort(a, 2 + 1, 4);
}
// low == 3 and high == 4
if (3 < 4) { // true
swap(a, 3, 3);
selectionSort(a, 3 + 1, 4);
}
还记得在findMinIndex
中,它检查给定索引之后是否有任何元素吗?索引3之后没有元素,所以它只返回3。这意味着上述代码等于:
// low == 1 and high == 4
if (1 < 4) { // true
swap(a, 1, 1);
selectionSort(a, 1 + 1, 4);
}
// low == 2 and high == 4
if (2 < 4) { // true
swap(a, 2, 3);
selectionSort(a, 2 + 1, 4);
}
// low == 3 and high == 4
if (3 < 4) { // true
swap(a, 3, 3);
selectionSort(a, 3 + 1, 4);
}
//低==3,高==4
如果(3<4){//true
互换(a、3、3);
选择排序(a,3+1,4);
}
这种交换没有任何作用。正如您所看到的,还有另一个递归调用!这将是最后一个
// low == 4 and high == 4
if (4 < 4) { // false, 4 is not less than 4
swap(a, 4, findMinIndex(a, 4)); // none of this happens
selectionSort(a, 4 + 1, 4); // no recursion
}
// finally returns void
//低==4,高==4
如果(4<4){//false,则4不小于4
swap(a,4,findMinIndex(a,4));//这些都不会发生
selectionSort(a,4+1,4);//无递归
}
//最终返回无效
结束
与递归相比,使用循环进行选择排序更容易理解
findMinIndex
将给定索引中的元素与它前面的所有元素(索引较高的元素)进行比较,直到数组的最后一个元素
因此,如果您有一个数组:
int[]a={7,4,2,6}代码>
然后调用findMinIndex(a,0)代码>它将首先检查索引0之后是否有元素。这就是这部分所做的index==data.length-1
。如果后面没有元素,它只返回传递的索引。但是索引0之后显然有一个元素,因为数组的长度为4
既然我们已经确认在我们的索引
之后有元素,现在是时候获得索引
之后最小的元素的索引了。通过这种方式,我们可以将索引处的元素与前面的所有元素进行比较,以查看在索引
到数组.length-1(包括)的范围内哪个元素最小。这是通过递归实现的:
minIndex=findMinIndex(数据,索引+1)代码>
所以接下来的几个电话是这样的:
findMinIndex(data, 1);
// is there an element after 1? There is. So we end up calling findMinIndex again...
findMinIndex(data, 2); // is there an element after 2? Yes. Recurse...
findMinIndex(data, 3); // is there an element after 3? No. That's the end of the array
// remember this part? it's used now to finally terminate the recursion
if (index == data.length - 1)
return index; // this equals 3
// low == 1 and high == 4
if (1 < 4) { // true
swap(a, 1, findMinIndex(data, 1));
selectionSort(a, 1 + 1, 4);
}
现在递归调用开始展开
// index == 2 because the 2nd to last index is 2. remember our array has length 4 and indices 0-3.
minIndex = 3; // this is the index of the last element
if (data[3] < data[2]) { // look at our array 'a', is 6 less than 2?
return 3; // No it is not. so this is not returned
} else {
return 2; // we end up return the index (2) of the smaller element (2)
}
请记住,我们发现0(包括0)之后的最小元素的索引为2。因此,上述代码可以替换为:
if (0 < 4) { // True of course
swap(a, 0, 2);
selectionSort(data, 0 + 1, 4);
}
记住我们的数组现在是{2,4,7,6}
。这意味着索引1(值4)之后的最小元素实际上只有4。因此,上述代码等于:
// low == 1 and high == 4
if (1 < 4) { // true
swap(a, 1, 1);
selectionSort(a, 1 + 1, 4);
}
// low == 2 and high == 4
if (2 < 4) { // true
swap(a, 2, 3);
selectionSort(a, 2 + 1, 4);
}
// low == 3 and high == 4
if (3 < 4) { // true
swap(a, 3, 3);
selectionSort(a, 3 + 1, 4);
}
还记得在findMinIndex
中,它检查给定索引之后是否有任何元素吗?索引3之后没有元素,所以它只返回3。这意味着上述代码等于:
// low == 1 and high == 4
if (1 < 4) { // true
swap(a, 1, 1);
selectionSort(a, 1 + 1, 4);
}
// low == 2 and high == 4
if (2 < 4) { // true
swap(a, 2, 3);
selectionSort(a, 2 + 1, 4);
}
// low == 3 and high == 4
if (3 < 4) { // true
swap(a, 3, 3);
selectionSort(a, 3 + 1, 4);
}
//低==3,高==4
如果(3<4){//true
互换(a、3、3);
选择