バブルソート、選択ソート、挿入ソート(java版)
1444 ワード
package sort;
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arrays = { 1, 4, 7, 3, 9, 0 };
for (int i = arrays.length - 1; i > 0; i--) { // : -->
for (int j = 0; j < i; j++) {// : -->
if (arrays[j] > arrays[j + 1]) { //
int temp = arrays[j + 1];
arrays[j + 1] = arrays[j];
arrays[j] = temp;
}
}
}
System.out.println(Arrays.toString(arrays));
}
}
package sort;
import java.util.Arrays;
public class SelectSort {
public static void main(String[] args) {
int[] arrays = { 1, 4, 7, 3, 9, 0 };
for (int i = 0; i < arrays.length - 1; i++) {
int minIndex = i; //
for (int j = i + 1; j < arrays.length; j++) {
if (arrays[minIndex] > arrays[j]) {
minIndex = j;
}
}
int temp = arrays[i];
arrays[i] = arrays[minIndex];
arrays[minIndex] = temp;
}
System.out.println(Arrays.toString(arrays));
}
}
package sort;
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] arrays = { 1, 4, 7, 3, 9, 0 };
for (int i = 1; i < arrays.length; i++) {
int marked = arrays[i];//
while (i > 0 && marked < arrays[i - 1]) {
arrays[i] = arrays[i - 1];//
i = i - 1;
}
arrays[i] = marked;
}
System.out.println(Arrays.toString(arrays));
}
}