「Java」選択ソート(Selection Sort)アルゴリズムを実装するサンプルコード

Javaコード
public class SelectSortAna {
static int i;
static int temp;
public static void selectSort(int array[]) {
for (i = 0; i < array.length; i++) {
int k = i;
for (int j = i + 1; j < array.length; j++) {
if (array[j] < array[k]) {
k = j;
}
}
if (k != i) {
temp = array[i];
array[i] = array[k];
array[k] = temp;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int array[] = { 28, 42, 55, 87, 35, 44, 25, 89 };
selectSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " “);
}
System.out.println(“\n");
}
}

Java

Posted by arkgame