-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_sort_main.py
630 lines (537 loc) · 22.7 KB
/
book_sort_main.py
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# -*- coding: utf-8 -*-
"""Mini Project - Linux Pro
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1N3wB_O9ze_vuqUK1w8z5eJ_NOW3ZU-r6
"""
"""
TODO:
- [ ] Import local files
- [ ] Make GUI to import csv files
- [ ] Add options to choose which sorting algorithm to go for
- [ ] Add more sortation
- [ ] Visualize the sortation
"""
from timeit import default_timer as time
"""
#When used with Google Colab to mount the Google Drive to the program
#make google colab connect to google drive
from google.colab import drive
drive.mount('/content/gdrive')
#give the directory of where the module is
import os, sys
sys.path.append('/content/gdrive/MyDrive/Colab Notebooks')
#python modules
import utils
import sorts
bookshelf = utils.load_book('/content/gdrive/MyDrive/Colab Notebooks/books_new - books_new.csv')
"""
####### Comparsion Functions #######
def by_title_ascending(book_a, book_b):
return book_a['title_lowercase'] > book_b['title_lowercase']
def by_author_ascending(book_a, book_b):
return book_a['author_lowercase'] > book_b['author_lowercase']
def by_genre_ascending(book_a, book_b):
return book_a['genre_lowercase'] > book_b['genre_lowercase']
def by_total_length_of_book(book_a, book_b):
return len(book_a['author_lowercase']) + len(book_a['title_lowercase']) > len(book_b['author_lowercase']) + len(book_b['title_lowercase'])
####### Outputs Functions #######
####### Bubble Sort #######
print(f"\n==== Bubble Sort - Title A-Z ====\n")
start = time()
sort_1 = sorts.bubble_sort(bookshelf, by_title_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_1:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes: \n", end - start, "s \n")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Bubble Sort - Title Z-A ====\n")
start = time()
sort_2 = sorts.bubble_sort(bookshelf, by_title_ascending)
sort_2.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes: \n", end - start, "s \n")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Bubble Sort - Author A-Z ====\n")
start = time()
sort_3 = sorts.bubble_sort(bookshelf, by_author_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_3:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes: \n", end - start, "s \n")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Bubble Sort - Author Z-A ====\n")
start = time()
sort_4 = sorts.bubble_sort(bookshelf, by_author_ascending)
sort_4.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes: \n", end - start, "s \n")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Bubble Sort - Genre A-Z ====\n")
start = time()
sort_3 = sorts.bubble_sort(bookshelf, by_genre_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_3:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Bubble Sort - Genre Z-A ====\n")
start = time()
sort_4 = sorts.bubble_sort(bookshelf, by_genre_ascending)
sort_4.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Bubble Sort - Total Length of Book (low to high) ====\n")
start = time()
sort_5 = sorts.bubble_sort(bookshelf, by_total_length_of_book)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Bubble Sort - Total Length of Book (high to low) ====\n")
start = time()
sort_6 = sorts.bubble_sort(bookshelf, by_total_length_of_book)
sort_6.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
####### Quick Sort #######
print(f"\n==== Quick Sort - Title A-Z ====\n")
start = time()
sort_1_2 = sorts.quick_sort(bookshelf, 0, len(bookshelf) - 1, by_title_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_1_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:", end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Quick Sort - Title Z-A ====\n")
start = time()
sort_2_2 = sorts.quick_sort(bookshelf, 0, len(bookshelf) - 1, by_title_ascending)
sort_2_2.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_2_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Quick Sort - Author A-Z ====\n")
start = time()
sort_3_2 = sorts.quick_sort(bookshelf, 0, len(bookshelf) - 1, by_author_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_3_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Quick Sort - Author Z-A ====\n")
start = time()
sort_4_2 = sorts.quick_sort(bookshelf, 0, len(bookshelf) - 1, by_author_ascending)
sort_4_2.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_4_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Quick Sort - Genre A-Z ====\n")
start = time()
sort_5_2 = sorts.quick_sort(bookshelf, 0, len(bookshelf) - 1, by_genre_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_5_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Quick Sort - Genre Z-A ====\n")
start = time()
sort_6_2 = sorts.quick_sort(bookshelf, 0, len(bookshelf) - 1, by_genre_ascending)
sort_6_2.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_6_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Quick Sort - Total Length of Book (low to high) ====\n")
start = time()
sort_7_2 = sorts.quick_sort(bookshelf, 0, len(bookshelf) - 1, by_total_length_of_book)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_7_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Quick Sort - Total Length of Book (high to low) ====\n")
start = time()
sort_8_2 = sorts.quick_sort(bookshelf, 0, len(bookshelf) - 1, by_total_length_of_book)
sort_8_2.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_8_2:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
####### Selection Sort #######
print(f"\n==== Selection Sort - Title A-Z ====\n")
start = time()
sort_1_4 = sorts.selection_sort(bookshelf, len(bookshelf), by_title_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_1_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:", end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Selection Sort - Title Z-A ====\n")
start = time()
sort_2_4 = sorts.selection_sort(bookshelf, len(bookshelf), by_title_ascending)
sort_2_4.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_2_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Selection Sort - Author A-Z ====\n")
start = time()
sort_3_4 = sorts.selection_sort(bookshelf, len(bookshelf), by_author_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_3_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Selection Sort - Author Z-A ====\n")
start = time()
sort_4_4 = sorts.selection_sort(bookshelf, len(bookshelf), by_author_ascending)
sort_4_4.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_4_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Selection Sort - Genre A-Z ====\n")
start = time()
sort_5_4 = sorts.selection_sort(bookshelf, len(bookshelf), by_genre_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_5_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Selection Sort - Genre Z-A ====\n")
start = time()
sort_6_4 = sorts.selection_sort(bookshelf, len(bookshelf), by_genre_ascending)
sort_6_4.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_6_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Selection Sort - Total Length of Book (low to high) ====\n")
start = time()
sort_7_4 = sorts.selection_sort(bookshelf, len(bookshelf), by_total_length_of_book)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_7_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print(end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Selection Sort - Total Length of Book (high to low) ====\n")
start = time()
sort_8_4 = sorts.selection_sort(bookshelf, len(bookshelf), by_total_length_of_book)
sort_8_4.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_8_4:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
####### Insertion Sort #######
print(f"\n==== Insertion Sort - Title A-Z ====\n")
start = time()
sort_1_5 = sorts.insertion_sort(bookshelf, by_title_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_1_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:", end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Insertion Sort - Title Z-A ====\n")
start = time()
sort_2_5 = sorts.insertion_sort(bookshelf, by_title_ascending)
sort_2_5.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_2_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "S")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Insertion Sort - Author A-Z ====\n")
start = time()
sort_3_5 = sorts.insertion_sort(bookshelf, by_author_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_3_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Insertion Sort - Author Z-A ====\n")
start = time()
sort_4_5 = sorts.insertion_sort(bookshelf, by_author_ascending)
sort_4_5.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_4_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Insertion Sort - Genre A-Z ====\n")
start = time()
sort_5_5 = sorts.insertion_sort(bookshelf, by_genre_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_5_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Insertion Sort - Genre Z-A ====\n")
start = time()
sort_6_5 = sorts.insertion_sort(bookshelf, by_genre_ascending)
sort_6_5.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_6_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Insertion Sort - Total Length of Book (low to high) ====\n")
start = time()
sort_7_5 = sorts.insertion_sort(bookshelf, by_total_length_of_book)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_7_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Insertion Sort - Total Length of Book (high to low) ====\n")
start = time()
sort_8_5 = sorts.insertion_sort(bookshelf, by_total_length_of_book)
sort_8_5.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_8_5:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
####### Heap Sort #######
print(f"\n==== Heap Sort - Title A-Z ====\n")
start = time()
sort_1_6 = sorts.heap_sort(bookshelf, by_title_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_1_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:", end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Heap Sort - Title Z-A ====\n")
start = time()
sort_2_6 = sorts.heap_sort(bookshelf, by_title_ascending)
sort_2_6.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_2_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINSIHED ---")
print(f"\n==== Heap Sort - Author A-Z ====\n")
start = time()
sort_3_6 = sorts.heap_sort(bookshelf, by_author_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_3_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Heap Sort - Author Z-A ====\n")
start = time()
sort_4_6 = sorts.heap_sort(bookshelf, by_author_ascending)
sort_4_6.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_4_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Heap Sort - Genre A-Z ====\n")
start = time()
sort_5_6 = sorts.heap_sort(bookshelf, by_genre_ascending)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_5_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Heap Sort - Genre Z-A ====\n")
start = time()
sort_6_6 = sorts.heap_sort(bookshelf, by_genre_ascending)
sort_6_6.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_6_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Heap Sort - Total Length of Book (low to high) ====\n")
start = time()
sort_7_6 = sorts.heap_sort(bookshelf, by_total_length_of_book)
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_7_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
print(f"\n==== Heap Sort - Total Length of Book (high to low) ====\n")
start = time ()
sort_8_6 = sorts.heap_sort(bookshelf, by_total_length_of_book)
sort_8_6.reverse()
print('{:<55} | {:>23} | {:>10}\n'.format("Title of the book", "Author", "Genre"))
for book in sort_8_6:
print('{:<55} | {:>23} | {:>10}'.format(book['Title'], book['Author'], book['Genre']))
end = time()
print("Sorting time takes:",end - start, "s")
print(f"--- SORTING FINISHED ---\n")
"""| |Bubble Sort |Quick Sort | Selection Sort | Insertion Sort | Heap Sort |
|-|-|-|-|-|-|
|Average time execution (seconds)|0.0369|0.0281| 0.0334 | 0.1731 |0.0333|
"""
# -*- coding: utf-8 -*-
"""sorts
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1M6Rw33CbJqWZBwpTai3tJkBX9BMMQGv4
"""
#sorts.py module
import random
#optimized bubble sort algorithm
def bubble_sort(array, comparison_function_used):
swaps = 0
sorted = False
while not sorted:
sorted = True
for idx in range(len(array) - 1):
if comparison_function_used(array[idx], array[idx + 1]): #if left > right
sorted = False
array[idx], array[idx + 1] = array[idx + 1], array[idx] #swap function
swaps += 1
print("Bubble Sort: There were {0} swaps in the operation".format(swaps))
return array
#random pivoted point quick sort algorithm
def partition(array, low, high, comparison_function_used):
i = (low-1) # index of smaller element
pivot = array[high] # pivot
for j in range(low, high):
# If current element is smaller than or
# equal to pivot
if comparison_function_used(pivot, array[j]):
# increment index of smaller element
i = i+1
array[i], array[j] = array[j], array[i]
array[i+1], array[high] = array[high], array[i+1]
return (i+1)
def quick_sort(array, low, high, comparison_function_used):
if len(array) == 1:
return array
if low < high:
pi = partition(array, low, high, comparison_function_used)
quick_sort(array, low, pi-1, comparison_function_used)
quick_sort(array, pi+1, high, comparison_function_used)
return array
#selection sort algorithm (might be in reverse by default?)
def selection_sort(array, size, comparison_function_used):
for step in range(size):
min_index = step
min_string = array[step]
for i in range(step + 1, size):
if comparison_function_used(min_string, array[i]):
min_string = array[i]
min_index = i
if min_index != 1:
array[step], array[min_index] = array[min_index], array[step]
return array
#insertion sort algorithm
def insertion_sort(array, comparison_function_used):
for step in range(1, len(array)):
key = array[step]
j = step - 1
while j>=0 and comparison_function_used(array[j], key):
array[j + 1] = array[j]
j -= 1
array[j+1] = key
return array
#heap sorting algorithm
def heapify(array, n, i, comparison_function_used):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and comparison_function_used(array[l], array[i]):
largest = l
if r < n and comparison_function_used(array[r], array[largest]):
largest = r
if largest != i:
array[i], array[largest] = array[largest], array[i]
heapify(array, n, largest, comparison_function_used)
return array
def heap_sort(array, comparison_function_used):
n = len(array)
for i in range(n//2, -1, -1):
heapify(array, n, i, comparison_function_used)
for i in range(n-1, 0, -1):
array[i], array[0] = array[0], array[i]
heapify(array, i, 0, comparison_function_used)
return array
# -*- coding: utf-8 -*-
"""utils.py
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/16h5Xf4OSr-4jHxiW3aTBoWiD8ZHxhv41
"""
"""
script that load the book that the user called to open
then make the title of the book, author, and the genre lowercase.
return the naming as bookshelf
"""
import csv
#bookshelf data from the csv file
#credit: https://gist.github.com/jaidevd/23aef12e9bf56c618c41
def load_book(filename):
bookshelf = []
with open(filename, 'r') as file:
shelf = csv.DictReader(file)
for book in shelf:
book['author_lowercase'] = book['Author'].lower()
book['title_lowercase'] = book['Title'].lower()
book['genre_lowercase'] = book['Genre'].lower()
#update the bookshelf with new file list
bookshelf.append(book)
return bookshelf