teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

他の残りのコードも追記しました

2018/11/29 09:02

投稿

Miyuu_apple38
Miyuu_apple38

スコア12

title CHANGED
File without changes
body CHANGED
@@ -45,7 +45,7 @@
45
45
 
46
46
  public class MergeSort {
47
47
  public static void main(String[] args) {
48
- List l = new List(25);
48
+ List l = new List(15);
49
49
  l.makeList();
50
50
  l.printList();
51
51
  l.mergeSort();

1

全てのコードを追記しました。

2018/11/29 09:02

投稿

Miyuu_apple38
Miyuu_apple38

スコア12

title CHANGED
File without changes
body CHANGED
@@ -2,9 +2,10 @@
2
2
  2つのリストを1つのリストで返したいです。
3
3
 
4
4
  ```Java
5
+
5
- List Merge(List list1, List list2) {
6
+ List Merge(List list1, List list2) {
6
-
7
+
7
- Cell c1 = header.next;
8
+ Cell c1 = header.next;
8
9
  Cell c2 = list2.header;
9
10
  Cell c3 = c2.next;
10
11
 
@@ -35,4 +36,137 @@
35
36
  ```
36
37
  **2.問題点**
37
38
  最後から二行目がエラーになります。
38
- list3というlist1とlist2というマージされた1つのリストを返すには、list1にlist2を加えるという考え方ではないのでしょうか。アドバイスいただけると幸いです!
39
+ list3というlist1とlist2というマージされた1つのリストを返すには、list1にlist2を加えるという考え方ではないのでしょうか。アドバイスいただけると幸いです!
40
+
41
+ **3.追記**
42
+ 線形リスト(連結リスト)でのマージソートです。
43
+ ```Java
44
+ import java.util.Random;
45
+
46
+ public class MergeSort {
47
+ public static void main(String[] args) {
48
+ List l = new List(25);
49
+ l.makeList();
50
+ l.printList();
51
+ l.mergeSort();
52
+ l.printList();
53
+ }
54
+ }
55
+
56
+ class List {
57
+
58
+ Cell header = new Cell("HEADER");
59
+ Cell sentinel = new Cell("");
60
+ int dataNum;
61
+
62
+ List(int num) {
63
+ dataNum = num;
64
+ header.next = sentinel;
65
+ }
66
+
67
+ void makeList() {
68
+ int[] tmpList = new int[dataNum];
69
+
70
+ for (int i=0; i<dataNum; i++) {
71
+ tmpList[i]=i;
72
+ }
73
+
74
+ for (int i=0; i<dataNum-1; i++) {
75
+ Random rnd = new Random();
76
+ int offset = rnd.nextInt(dataNum-i);
77
+ int tmp;
78
+
79
+ tmp = tmpList[i];
80
+ tmpList[i] = tmpList[i + offset];
81
+ tmpList[i + offset] = tmp;
82
+ }
83
+
84
+ for (int i=0; i<dataNum; i++) {
85
+ insertTop(new Cell(new Integer(tmpList[i])));
86
+ }
87
+ }
88
+
89
+ void mergeSort() {
90
+ int center = dataNum/2;
91
+ Cell curr = header.next;
92
+
93
+ for (int i =0; i<center; i++) curr = curr.next;
94
+
95
+ List list1 = MergeSortRec(header.next,curr,center);
96
+ List list2 = MergeSortRec(curr,sentinel,dataNum-center);
97
+ List list3 = Merge(list1,list2);
98
+
99
+ header = list3.header;
100
+ sentinel = list3.sentinel;
101
+ }
102
+
103
+ List MergeSortRec(Cell start,Cell end, int num) {
104
+ int center2 = num/2;
105
+ start = header.next;
106
+ Cell c = header.next;
107
+
108
+ for (int i=0; i<center2 ; i++) start=start.next;
109
+ List list1 = MergeSortRec(c,start,center2);
110
+ if(center2==1){
111
+ if((Integer)c.data>(Integer)start.data){
112
+ swap(c,start);
113
+ }
114
+ }
115
+
116
+ List list2 = MergeSortRec(start,end,num-center2);
117
+ if(center2==1){
118
+ if((Integer)start.data>(Integer)end.data){
119
+ swap(start,end);
120
+ }
121
+ }
122
+
123
+ List list3 = Merge(list1,list2);
124
+
125
+ header = list3.header;
126
+ end = list3.sentinel;
127
+
128
+ return list3;
129
+
130
+ }
131
+
132
+ List Merge(List list1, List list2){
133
+ // 最初に示したコードが下に入ります
134
+ }
135
+
136
+ void swap(Cell c1,Cell c2) {
137
+ if ((c1 != null) && (c2 != null)) {
138
+ Object tmp = c1.data;
139
+ c1.data = c2.data;
140
+ c2.data = tmp;
141
+ }
142
+ }
143
+
144
+ void insertTop(Cell c) {
145
+ c.next = header.next;
146
+ header.next = c;
147
+
148
+ }
149
+
150
+ void printList() {
151
+ Cell curr = header.next;
152
+
153
+ while (curr.next !=null) {
154
+ System.out.print(curr.data + " ");
155
+ curr = curr.next;
156
+ }
157
+ System.out.println();
158
+ }
159
+ }
160
+
161
+ class Cell {
162
+ Cell next;
163
+ Object data;
164
+
165
+ Cell(Object obj) {
166
+ next = null;
167
+ data = obj;
168
+ }
169
+ }
170
+
171
+
172
+ ```