자바 선택 정렬에서 내부 루프 길이에 대한 기본적인 질문이 있습니다. 다음은 선택 정렬에 일반적으로 사용되는 코드입니다.Java에서 선택 정렬 루프 질문
package com.java2novice.algos;
public class MySelectionSort {
public static int[] doSelectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++)
/* why not the length is not arr.length-1? I think the length is
exactly the same as what it is, the array's length is a
static number as array.length-1, but when it comes to the inner
loop why it plus 1 to the array length? */
if (arr[j] < arr[index])
index = j;
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
return arr;
}
}
정확하게 무엇을하려고하고 있으며 어떤 문제가 있는지 설명하십시오. 정확히 작동하지 않는 것은 무엇입니까? – Keara
내부 루프의 길이는 배열 길이와 같아야한다고 생각합니다. array.length -1과 같이 안정적인 값입니다. 배열의 길이에 1을 더하는 이유는 무엇입니까? ? –