-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProb007_method.java
More file actions
67 lines (59 loc) · 1.25 KB
/
Copy pathProb007_method.java
File metadata and controls
67 lines (59 loc) · 1.25 KB
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
64
65
66
67
package java0825_method.prob;
/*
* [출력결과]
* 내림 차순 결과
31
22
16
11
10
9
오름 차순 결과
9
10
11
16
22
31
*/
public class Prob007_method {
public static void main(String[] args) {
int[] arr = { 10, 22, 9, 16, 11, 31 };
int[] result1 = sort(arr, "desc");
System.out.println("내림 차순 결과");
for (int i = 0; i < result1.length; i++) {
System.out.println(result1[i]);
}
int[] result2 = sort(arr, "asc");
System.out.println("오름 차순 결과");
for (int i = 0; i < result2.length; i++) {
System.out.println(result2[i]);
}
}// end main( )
private static int[] sort(int[] array, String orderby) {
// 구현하시오.
int temp[] = new int[array.length];
if (orderby == "desc") {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] > array[j]) {
temp[i] = array[i];
array[i] = array[j];
array[j] = temp[i];
}
}
}
} else if (orderby == "asc") {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i] < array[j]) {
temp[i] = array[i];
array[i] = array[j];
array[j] = temp[i];
}
}
}
}
return array;
}// end sort( )
}