-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDblyCrcLinkedList.java
More file actions
572 lines (399 loc) · 12.8 KB
/
DblyCrcLinkedList.java
File metadata and controls
572 lines (399 loc) · 12.8 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import java.text.DecimalFormat;
/**
*
*/
/**
* @author ryan knight
*
*/
public class DblyCrcLinkedList {
private class Node {
private Node next;
private Node prev;
private Comparable data;
private Node(Comparable data, Node prev, Node next) {
this.data = data;
this.prev = prev;
this.next = next;
}
private Node() {
this(null, null, null);
}
}
private Node head;
private int size;
public int dataAssign;
public int dataComp;
public int loopContAssign;
public int loopContComp;
public int other;
public String name;
public int totalOps;
public double timing;
final DecimalFormat df = new DecimalFormat("#0.000000000");
public DblyCrcLinkedList(String name) {
this.head = new Node();
this.head.prev = this.head; // Points back to head
this.head.next = this.head;
this.name = name;
}
public DblyCrcLinkedList() {
this.head = new Node();
this.head.prev = this.head; // Points back to head
this.head.next = this.head;
}
/*-------------------------------------
* Methods
*-------------------------------------*/
public void ResetCounters() {
this.dataAssign = 0;
this.dataComp = 0;
this.loopContAssign = 0;
this.loopContComp = 0;
this.other = 0;
}
public int getSize() {
return this.size;
}
public String getTiming() {
return df.format(nanotimeToSeconds());
}
public int getTotalOps() {
return totalOpsSum();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDataAssign() {
return dataAssign;
}
public void setDataAssign(int dataAssign) {
this.dataAssign = dataAssign;
}
public int getDataComp() {
return dataComp;
}
public void setDataComp(int dataComp) {
this.dataComp = dataComp;
}
public int getLoopContAssign() {
return loopContAssign;
}
public void setLoopContAssign(int loopContAssign) {
this.loopContAssign = loopContAssign;
}
public int getLoopContComp() {
return loopContComp;
}
public void setLoopContComp(int loopContComp) {
this.loopContComp = loopContComp;
}
public int getOther() {
return other;
}
public void setOther(int other) {
this.other = other;
}
public double nanotimeToSeconds() {
timing = this.timing / 100000000;
return timing;
}
public int totalOpsSum() {
totalOps = this.dataAssign + this.dataComp + this.loopContAssign + this.loopContComp + this.other;
return totalOps;
}
public boolean isEmpty() {
return this.head == this.head.next; // if this.head points back to
// itself its empty
}
public String toString() {
String res = "";
for (Node curr = this.head.next; curr != this.head; curr = curr.next) {
res += curr.data + "\n";
}
return res;
}
public String toStringRaw() {
String res = this.name + ", " + this.size + ", " + this.getDataAssign() + ", " + this.getDataComp() + ", " + this.getLoopContAssign() + ", " + this.getLoopContComp() + ", " + this.getOther() + ", " + this.getTotalOps();
return res;
}
public String toStringRawTime() {
String res = this.name + ", " + this.size + ", " + this.getTiming() + " Seconds";
return res;
}
public void addFirst(Comparable data) {
Node temp = new Node(data, null, null);
temp.next = this.head.next; // temp wired in
temp.prev = this.head;
this.head.next.prev = temp;
this.head.next = temp; // update list
size++;
}
public void addLast(Comparable data) {
Node temp = new Node(data, this.head.prev, this.head);
this.head.prev.next = temp;
this.head.prev = temp;
size++;
}
public void addIndex(int index, Comparable data) {
if (index < 0 || index > this.size)
throw new IndexOutOfBoundsException("Index was invalid, Size: " + this.size + " Index: " + index);
int i = 0;
Node curr = this.head.next;
while (i < index) {
curr = curr.next;
i++;
}
Node temp = new Node(data, curr.prev, curr);
curr.prev.next = temp;
curr.prev = temp;
size++;
}
public DblyCrcLinkedList clone(String sortName) {
DblyCrcLinkedList twin = new DblyCrcLinkedList(sortName);
Node curr = this.head.next;
while (curr != this.head) {
twin.addLast(curr.data);
curr = curr.next;
}
return twin;
}
/*
*
* This is the clean Sorts with System.nanoTime()
*
*
*
*/
public void smartBubble() {
this.timing = 0;
double startTime = System.nanoTime();
if (this.size < 2) {
return;
}
Node offSet = this.head;
boolean sorted;
Node left, right;
Comparable temp;
do {
sorted = true;
for (left = this.head.next, right = left.next; right != offSet; left = left.next, right = right.next) {
if (left.data.compareTo(right.data) > 0) {
sorted = false;
temp = left.data;
left.data = right.data;
right.data = temp;
}
} // end forLoop
offSet = offSet.prev;
} while (!sorted);
this.timing = System.nanoTime() - startTime;
}
public void selectionSort() {
this.timing = 0;
double startTime = System.nanoTime();
if (this.size < 2) {
return;
}
Node start, smallest, curr;
Comparable temp;
for (start = this.head.next; start != this.head.prev; start = start.next) {
smallest = start;
for (curr = start.next; curr != this.head; curr = curr.next) {
if (curr.data.compareTo(smallest.data) < 0) {
smallest = curr;
}
}
temp = start.data;
start.data = smallest.data;
smallest.data = temp;
}
this.timing = System.nanoTime() - startTime;
}
public void insertionSort() {
this.timing = 0;
double startTime = System.nanoTime();
if(this.size < 2) {
return;
}
Node lastSorted, firstUnsorted, sortedWalker;
Comparable dataToInsert;
for(lastSorted = this.head.next; lastSorted.next != this.head; lastSorted = lastSorted.next) {
firstUnsorted = lastSorted.next;
dataToInsert = firstUnsorted.data;
for(sortedWalker = lastSorted; sortedWalker != this.head && sortedWalker.data.compareTo(dataToInsert) > 0; sortedWalker= sortedWalker.prev) {
sortedWalker.next.data = sortedWalker.data;
}
sortedWalker.next.data = dataToInsert;
}
this.timing = System.nanoTime() - startTime;
}
public void insertionSortCut() {
this.timing = 0;
double startTime = System.nanoTime();
if(this.size < 2) {
return;
}
Node lastSorted, firstUnsorted, sortedWalker;
for (lastSorted = this.head.next; lastSorted.next != this.head;) {
firstUnsorted = lastSorted.next;
sortedWalker = lastSorted;
while(sortedWalker != this.head && sortedWalker.data.compareTo(firstUnsorted.data) > 0) {
sortedWalker = sortedWalker.prev;
} // end while loop
if (sortedWalker != lastSorted) {
firstUnsorted.next.prev = lastSorted; // Rewired list where firstUnsorted lives
lastSorted.next = firstUnsorted.next;
firstUnsorted.next = sortedWalker.next; // making firstUnsorted point to the right spot
firstUnsorted.prev = sortedWalker;
sortedWalker.next.prev = firstUnsorted; // wire unsorted node into sorted part of the list
sortedWalker.next = firstUnsorted;
} else {
lastSorted = lastSorted.next;
}
} // end for loop
this.timing = System.nanoTime() - startTime;
}
/*
*
* This section begins the Sorts with the counting routines written in.
*
*
*
*/
public void smartBubbleCounter() {
ResetCounters();
this.other++; // for if block
if (this.size < 2) {
this.other++; // for return
return;
}
this.other++;
this.loopContAssign++; // for offset declaration, for offset assign
Node offSet = this.head;
this.other++; // for sorted declaration
boolean sorted;
this.other += 2; // for left/right declaration
Node left, right;
this.other++; // for comparable declaration
Comparable temp;
do {
this.loopContAssign++; // for sorted assign (true)
sorted = true;
this.loopContAssign += 2; // for left/ right assign in loop
for (left = this.head.next, right = left.next; right != offSet; left = left.next, right = right.next) {
this.loopContComp++; // for right != offset (true)
this.dataComp++; // for if block
if (left.data.compareTo(right.data) > 0) {
this.loopContAssign++; // for sorted assign (false)
sorted = false;
this.dataAssign += 3; // for data assign temp =, left.data = , right.data =
temp = left.data;
left.data = right.data;
right.data = temp;
}
this.loopContAssign +=2; // for right = right.next, left = left.nexr in for loop
} // end forLoop
this.loopContComp++; // for right != offset (false)
this.loopContAssign++; // for offSet assign
offSet = offSet.prev;
this.loopContComp++; // for while(conditional)
} while (!sorted);
}
public void selectionSortCount() {
ResetCounters();
this.other ++; // for if block
if (this.size < 2) {
this.other ++; // for return
return;
}
this.other +=4; // for start, smallest, curr, temp declaration
Node start, smallest, curr;
Comparable temp;
this.loopContAssign ++; // for start assignment
for (start = this.head.next; start != this.head.prev; start = start.next) {
this.loopContComp++; // for start != this.head.prev
this.dataAssign ++; // for smallest = start data assign
smallest = start;
this.loopContAssign ++; //for cur = start.n
for (curr = start.next; curr != this.head; curr = curr.next) {
this.loopContComp++; // for cur != this.head
this.dataComp ++; // for comparing curr data to smallest data
if (curr.data.compareTo(smallest.data) < 0) {
smallest = curr;
}
this.loopContAssign ++; //for cur = cur.n in inner for loop
}
this.loopContAssign ++; // for start = start.next assignment in outer for loop
this.dataAssign += 3; // swaps x3
temp = start.data;
start.data = smallest.data;
smallest.data = temp;
}
}
public void insertionSortCounter() {
this.other++; // for if block
if(this.size < 2) {
this.other++; // for return
return;
}
this.other += 4; // for lastSorted, firstSorted, sortedWalker, and dataToInsert declarations
Node lastSorted, firstUnsorted, sortedWalker;
Comparable dataToInsert;
this.loopContAssign ++; //for lastSorted = this.h.n
for(lastSorted = this.head.next; lastSorted.next != this.head; lastSorted = lastSorted.next) {
this.loopContComp++; // for lastSoted != this.head
this.other ++; //for re-pointing nodes
firstUnsorted = lastSorted.next;
this.dataAssign ++; ////for re-pointing nodes
dataToInsert = firstUnsorted.data;
this.loopContAssign ++; //for sortedWalker = lastSorted
for(sortedWalker = lastSorted; sortedWalker != this.head && sortedWalker.data.compareTo(dataToInsert) > 0; sortedWalker= sortedWalker.prev) {
this.loopContComp += 2; // for sortedWalker != this.head && sortedWalker.data.compareTo(dataToInsert) > 0
this.dataAssign ++; //for sortedWalker.next.data = sortedWalker.data;
sortedWalker.next.data = sortedWalker.data;
this.loopContAssign ++; // lastSorted = lastSorted.next
}
this.other ++; // for node re-pointing
sortedWalker.next.data = dataToInsert;
this.loopContAssign ++; // lastSorted = lastSorted.next
}
}
public void insertionSortCutCounter() {
this.other ++; // for if block
if(this.size < 2) {
this.other ++; // for return
return;
}
this.other += 3; // for declaration of lastSorted, firstUnsorted, sortedWalker;
Node lastSorted, firstUnsorted, sortedWalker;
this.loopContAssign ++; //for lastSorted = this.h.n
for (lastSorted = this.head.next; lastSorted.next != this.head;) {
this.loopContComp++; // for lastSoted != this.head
this.other += 2; // for re-pointing nodes x2
firstUnsorted = lastSorted.next;
sortedWalker = lastSorted;
while(sortedWalker != this.head && sortedWalker.data.compareTo(firstUnsorted.data) > 0) {
this.loopContComp += 2; // for (sortedWalker != this.head && sortedWalker.data.compareTo(firstUnsorted.data) > 0)
this.other ++; // for re-pointing nodes
sortedWalker = sortedWalker.prev;
} // end while loop
this.loopContComp ++; // for sortedWalker != lastSorted
if (sortedWalker != lastSorted) {
this.other += 6; // for cutting and moving nodes (NO DATA ASSIGNMENTS HERE)
firstUnsorted.next.prev = lastSorted; // Rewired list where firstUnsorted lives
lastSorted.next = firstUnsorted.next;
firstUnsorted.next = sortedWalker.next; // making firstUnsorted point to the right spot
firstUnsorted.prev = sortedWalker;
sortedWalker.next.prev = firstUnsorted; // wire unsorted node into sorted part of the list
sortedWalker.next = firstUnsorted;
} else {
this.loopContAssign ++; // for lastSorted = lastSorted.next
lastSorted = lastSorted.next;
}
} // end for loop
}
}