-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA sorting Tale (sorting project)
More file actions
87 lines (70 loc) · 2.64 KB
/
A sorting Tale (sorting project)
File metadata and controls
87 lines (70 loc) · 2.64 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
#Script file
import utils
import sorts
bookshelf = utils.load_books('books_small.csv')
long_bookshelf = utils.load_books('books_large.csv')
bookshelf_v1 = bookshelf.copy()
bookshelf_v2 = bookshelf.copy()
def by_title_ascending(book_a,book_b):
return book_a['title_lower'] > book_b['title_lower']
def by_author_ascending(book_a,book_b):
return book_a['author_lower'] > book_b['author_lower']
def by_total_length(book_a,book_b):
return len(book_a['title_lower']) +len(book_a['author_lower']) > len(book_b['title']) + len(book_b['author_lower'])
#sort_1 = sorts.bubble_sort(bookshelf,by_title_ascending)
#for book in sort_1:
#print(book['title'])
#sort_2 = sorts.bubble_sort(bookshelf_v1,by_author_ascending)
#for book in sort_2:
#print(book['author'])
#sorts.quicksort(bookshelf_v2,0,len(bookshelf_v2)-1,by_author_ascending)
#for book in bookshelf_v2:
#print(book['author_lower'])
#sort_3 = sorts.bubble_sort(long_bookshelf,by_total_length)
#long_bookshelf_v1 = long_bookshelf.copy()
#sorts.quicksort(long_bookshelf_v1,0,len(long_bookshelf_v1)-1,by_total_length)
#for book in long_bookshelf_v1: print(len(book['author_lower'])+len(book['title_lower']))
#sorts file
import random
def bubble_sort(arr,comparison_function):
swaps = 0
sorted = False
while not sorted:
sorted = True
for idx in range(len(arr) - 1):
if comparison_function(arr[idx], arr[idx + 1]):
sorted = False
arr[idx], arr[idx + 1] = arr[idx + 1], arr[idx]
swaps += 1
print("\nBubble sort: There were {0} swaps\n".format(swaps))
return arr
def quicksort(list, start, end, comparison_function):
if start >= end:
return
pivot_idx = random.randrange(start, end + 1)
pivot_element = list[pivot_idx]
list[end], list[pivot_idx] = list[pivot_idx], list[end]
less_than_pointer = start
for i in range(start, end):
if comparison_function(pivot_element, list[i]):
list[i], list[less_than_pointer] = list[less_than_pointer], list[i]
less_than_pointer += 1
list[end], list[less_than_pointer] = list[less_than_pointer], list[end]
quicksort(list, start, less_than_pointer - 1, comparison_function)
quicksort(list, less_than_pointer + 1, end, comparison_function)
#utils file
import csv
# This code loads the current book
# shelf data from the csv file
def load_books(filename):
bookshelf = []
with open(filename) as file:
shelf = csv.DictReader(file)
for book in shelf:
# add your code here
book['author_lower'] = book['author'].lower()
book['title_lower'] = book['title'].lower()
bookshelf.append(book)
for book in bookshelf:
print(book['title'])
return bookshelf