-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.java
64 lines (53 loc) · 1.89 KB
/
1.java
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
63
내가 만든것
import java.util.*;
public class Select_Sort{
public static void main(String []args){
int array[] = {1, 10, 5, 8, 7, 6, 4, 3, 2, 9};
int temp = 0;
for (int i = 0; i < array.length - 1; i++)
{
for (int j = 0; j < array.length - 1; j++)
{
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
else
continue;
}
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
/*
기존의 예제 답은 배열의 크기를 주고 가장 작은 함수로 min_index를 선언해주고, 앞의값보다 뒤의값이 작을때만 if문이 돌아가게 하여 함수의 길이가 짧아 보인다
배열을 끝가지 지나가고 가장 작은값을 min_index에 저장해 둔 다음에 맨앞, 작은수부터 값을 확정시키는것이 나랑 달랐다.
*/
===================================================================================================================================================================
기존의 예제 답
import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 10;
int[] arr = {7, 5, 9, 0, 3, 1, 6, 2, 4, 8};
for (int i = 0; i < n; i++) {
int min_index = i; // 가장 작은 원소의 인덱스
for (int j = i + 1; j < n; j++) {
if (arr[min_index] > arr[j]) {
min_index = j;
}
}
// 스와프
int temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
for(int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}