티스토리 뷰
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 |
import java.util.Scanner;
public class Sort1 {
static int[] bubbleSort(int[] arr) {
int temp = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
static int[] selectionSort(int[] arr) {
int temp = 0;
int standardIndex = 0;
int idx=0;
while(standardIndex < arr.length-1){
int min=arr[standardIndex];
boolean swap = false;
for(int i=standardIndex+1; i<arr.length; i++){
if(min > arr[i]){
idx=i;
min=arr[i];
swap = true;
}
}
if(swap){
temp = arr[standardIndex];
arr[standardIndex] = arr[idx];
arr[idx] = temp;
}
standardIndex++;
}
return arr;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[6];
for (int i = 0; i < arr.length; i++) {
System.out.print("정렬시킬 숫자입력(총" + arr.length + "개(" + (i + 1)+ ")번째) : ");
arr[i] = scanner.nextInt();
}
arr = selectionSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
}
}
|
Comments