単純ソートとは順番に値を比べていって入れ替えていく方式です。
![](https://sasukepg.jp/wp-content/uploads/2024/05/image20.jpg)
実際のJavaでの使用例
public class Sort { public static void main(String[] args) { int n[] = {23, 5, 69, 87, 4, 2, 62, 1}; int k, m,t,s,i,j; System.out.println("--ソート前の値を表示します--"); //ソート前の値を表示 for (i = 0; i lt; n.length - 1; i++) { System.out.println(n[i]); } for (i = 0; i lt; n.length - 1; i++) { m = n[i]; //n[i]を最小値に設定 s = i; //添字番号を保存 for (j = i + 1; j lt; n.length; j++) { if (n[j] lt; m) { //最小値が現れたら入れ替える m = n[j]; s = j; } } t = n[i]; //n[i]と最小値を交換 n[i] = n[s]; n[s] = t; } System.out.println("--ソート後の値を表示します--"); //ソート後の値を表示 for(i = 0; i lt; n.length - 1; i++){ System.out.println(n[i]); } } }
実行結果
--ソート前の値を表示します-- 23 5 69 87 4 2 62 --ソート後の値を表示します-- 1 2 4 5 23 62 69