回答編集履歴

1

書き換え

2018/04/28 05:53

投稿

katoy
katoy

スコア22324

test CHANGED
@@ -1,93 +1,119 @@
1
1
  ```java
2
2
 
3
- public class IntBubbleSorter {
3
+ import java.util.ArrayList;
4
4
 
5
- final static int REPEAT_COUNT = 3;
5
+ import java.util.Collections;
6
6
 
7
+ import java.util.List;
8
+
7
- final static int ARRAY_SIZE = 100;
9
+ import java.util.Random;
8
10
 
9
11
 
10
12
 
13
+ public class Kadai03_3 {
14
+
15
+ private static final int NUM_NUMBER = 100;
16
+
11
17
  public static void main(String[] args) {
12
18
 
13
- int count = 0;
19
+ System.out.println("乱数100個 0~100)");
14
20
 
15
- int[] array = new int[ARRAY_SIZE];
21
+ int[] numbers = new int[NUM_NUMBER];
22
+
23
+ Random rand = new Random();
16
24
 
17
25
 
18
26
 
19
- for (int i = 0; i < REPEAT_COUNT; i++) {
27
+ for (int i = 0; i < numbers.length; i++) {
20
28
 
21
- int comp = bubbleSort(randum_array(array));
29
+ numbers[i] = rand.nextInt(100);
22
30
 
23
- System.out.println("number of comparison in bubble sort: " + comp);
31
+ if (i % 10 == 0) {
24
32
 
25
- count += comp;
33
+ System.out.print("\n");
34
+
35
+ }
36
+
37
+ System.out.print(numbers[i] + "\t");
26
38
 
27
39
  }
28
40
 
29
- System.out.println("平均比較回数:" + 1.0 * count / REPEAT_COUNT);
41
+ int min_num = Integer.MAX_VALUE;
30
42
 
43
+ int max_num = Integer.MIN_VALUE;
44
+
31
- }
45
+ double total = 0;
32
46
 
33
47
 
34
48
 
35
- public static int[] randum_array(int[] array) {
49
+ for (int i = 0; i < numbers.length; i++) {
36
50
 
37
- for (int i = 0; i < array.length; i++) {
51
+ if (numbers[i] < min_num) {
38
52
 
53
+ min_num = numbers[i];
54
+
55
+ }
56
+
39
- array[i] = (int)(Math.random() * 101);
57
+ if (max_num < numbers[i]) {
58
+
59
+ max_num = numbers[i];
60
+
61
+ }
62
+
63
+ total += numbers[i];
40
64
 
41
65
  }
42
66
 
43
- return array;
67
+ double ave_num = total / numbers.length;
44
-
45
- }
46
68
 
47
69
 
48
70
 
49
- public static int bubbleSort(int[] array) {
71
+ System.out.print("\n");
50
72
 
51
- int lastPos;
73
+ System.out.println("最小値:" + min_num);
52
74
 
53
- int index;
75
+ System.out.println("最大値:" + max_num);
54
76
 
55
- int temp;
56
-
57
- int comp = 0;
77
+ System.out.println("平均値:" + ave_num);
58
78
 
59
79
 
60
80
 
61
- for (lastPos = array.length - 1; lastPos >= 0; lastPos--) {
81
+ List<Integer> list = new ArrayList<>(numbers.length);
62
82
 
63
- for (index = 0; index <= lastPos - 1; index++) {
83
+ for (int x : numbers) {
64
84
 
65
- if (array[index] > array[index + 1]) {
66
-
67
- comp++;
68
-
69
- temp = array[index];
85
+ list.add(x);
70
-
71
- array[index] = array[index + 1];
72
-
73
- array[index + 1] = temp;
74
-
75
- }
76
-
77
- }
78
86
 
79
87
  }
80
88
 
89
+ min_num = Collections.min(list);
90
+
91
+ max_num = Collections.max(list);
92
+
93
+ total = 0;
94
+
95
+ for (int x : list) {
96
+
81
- return comp;
97
+ total += x;
98
+
99
+ }
100
+
101
+ ave_num = total / numbers.length;
102
+
103
+
104
+
105
+ System.out.print("\n");
106
+
107
+ System.out.println("最小値:" + min_num);
108
+
109
+ System.out.println("最大値:" + max_num);
110
+
111
+ System.out.println("平均値:" + ave_num);
82
112
 
83
113
  }
84
114
 
85
115
  }
86
116
 
87
-
88
-
89
117
  ```
90
118
 
91
- 実行例:
92
-
93
- ![イメージ説明](eba0d0747a528b093eea2067461d692f.png)
119
+ ![イメージ説明](9c9b1b4cbdb25b99eaa694254bd09085.png)