簡單的選擇排序,簡單選擇排序
1 package com.hp.io;
2
3 import java.util.Scanner;
4
5 public class XuanZe {
6 public static void main(String[] args) {
7 Scanner s = new Scanner(System.in);
8 System.out.println("請輸入數組的個數");
9 int a = s.nextInt();
10 int[] b = new int[a];
11 System.out.println("輸入數組中的元素:");
12 for (int i = 0; i < b.length; i++) { //數組中的元素進行操作後都要遍歷
13 b[i] = s.nextInt();
14 }
15 for (int i = 0; i < b.length - 1; i++) {
16 for (int j = i + 1; j < b.length; j++) {
17 if (b[i] > b[j]) {
18 int temp = b[i];
19 b[i] = b[j];
20 b[j] = temp;
21 }
22 }
23 }
24 for (int k = 0; k < b.length; k++) {
25 System.out.println(b[k] + " ");
26 }
27 }
28 }
1 package com.hp.io;
2
3 import java.util.Scanner;
4
5 public class MaoPao {
6 public static void main(String[] args) {
7 Scanner s = new Scanner(System.in);
8 System.out.println("請輸入數組的個數");
9 int a = s.nextInt();
10 int[] b = new int[a];
11 System.out.println("輸入數組中的元素:"); //對數組中元素進行遍歷
12 for (int i = 0; i < b.length; i++) {
13 b[i] = s.nextInt(); //數組b的元素就是輸入的元素
14 }
15 for (int i = 0; i < b.length - 1; i++) {
16 for (int j = i + 1; j < b.length; j++) {
17 if (b[i] > b[j]) {
18 int temp = b[i];
19 b[i] = b[j];
20 b[j] = temp;
21 }
22 }
23 }
24 for (int k = 0; k < b.length; k++) { //打印的時候還還要求數組進行遍歷
25 System.out.println(b[k] + " ");
26 }
27 }