forked from codebasics/data-structures-algorithms-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.py
More file actions
155 lines (135 loc) · 6.01 KB
/
7.py
File metadata and controls
155 lines (135 loc) · 6.01 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
# # Exercise: Selection Sort
# Implement a Multi-Level Sort of a given list of dictionaries based on a given sorting order.
# If user wants to sort dictionary based on First Key 'A', Then Key 'B',
# they shall pass list of keys in the order of preference as a list ['A','B'].
# Your code should be able to sort list of dictionaries for any number of keys in sorting order list.
# Using this multi-level sort, you should be able to sort any list of dictionaries based on sorting order preference
# Example:
# A single dictionary entry contains two keys 'First Name' and 'Last Name'. the list should be sorted first based on 'First Name', then based on 'Last Name', w.r.t. common/same 'First Name' entries.
# for this, one shall past sorting order of preference list [ 'First Name' , 'Last Name' ]
# For this, Given the following sequence List:
# ```
# [
# {'First Name': 'Raj', 'Last Name': 'Nayyar'},
# {'First Name': 'Suraj', 'Last Name': 'Sharma'},
# {'First Name': 'Karan', 'Last Name': 'Kumar'},
# {'First Name': 'Jade', 'Last Name': 'Canary'},
# {'First Name': 'Raj', 'Last Name': 'Thakur'},
# {'First Name': 'Raj', 'Last Name': 'Sharma'},
# {'First Name': 'Kiran', 'Last Name': 'Kamla'},
# {'First Name': 'Armaan', 'Last Name': 'Kumar'},
# {'First Name': 'Jaya', 'Last Name': 'Sharma'},
# {'First Name': 'Ingrid', 'Last Name': 'Galore'},
# {'First Name': 'Jaya', 'Last Name': 'Seth'},
# {'First Name': 'Armaan', 'Last Name': 'Dadra'},
# {'First Name': 'Ingrid', 'Last Name': 'Maverick'},
# {'First Name': 'Aahana', 'Last Name': 'Arora'}
# ]
# ```
# Your algorithm should generate sorted list:
# ```
# [
# {'First Name': 'Aahana', 'Last Name': 'Arora'}
# {'First Name': 'Armaan', 'Last Name': 'Dadra'}
# {'First Name': 'Armaan', 'Last Name': 'Kumar'}
# {'First Name': 'Ingrid', 'Last Name': 'Galore'}
# {'First Name': 'Ingrid', 'Last Name': 'Maverick'}
# {'First Name': 'Jade', 'Last Name': 'Canary'}
# {'First Name': 'Jaya', 'Last Name': 'Seth'}
# {'First Name': 'Jaya', 'Last Name': 'Sharma'}
# {'First Name': 'Karan', 'Last Name': 'Kumar'}
# {'First Name': 'Kiran', 'Last Name': 'Kamla'}
# {'First Name': 'Raj', 'Last Name': 'Nayyar'}
# {'First Name': 'Raj', 'Last Name': 'Sharma'}
# {'First Name': 'Raj', 'Last Name': 'Thakur'}
# {'First Name': 'Suraj', 'Last Name': 'Sharma'}
# ]
# ```
def selection_sort(arr):
size = len(arr)
for i in range(size-1):
min_index = i
for j in range(min_index+1,size):
if arr[j] < arr[min_index]:
min_index = j
if i != min_index:
arr[i], arr[min_index] = arr[min_index], arr[i]
# def selection_sort_multi(arr,keys):
# size = len(arr)
# # for key in keys[-1::-1]:
# # for i in range(size):
# # min_index = i
# # for j in range(i+1,size):
# # if arr[j][key] < arr[min_index][key]:
# # min_index = j
# # arr[i], arr[min_index] = arr[min_index], arr[i]
# for key in reversed(keys):
# for i in range(size):
# min_or_max_index = i
# for j in range(i+1, size):
# if arr[j][key] < arr[min_or_max_index][key]:
# min_or_max_index = j
# # Swap the found minimum element with the first element
# arr[i], arr[min_or_max_index] = arr[min_or_max_index], arr[i]
# def selection_sort_multi(elements, sort_by_list):
# for sort_by in sort_by_list[-1::-1]:
# for x in range(len(elements)):
# min_index = x
# for y in range(x, len(elements)):
# if elements[y][sort_by] < elements[min_index][sort_by]:
# min_index = y
# if x != min_index:
# elements[x], elements[min_index] = elements[min_index], elements[x]
def selection_sort_multi(arr, keys):
size = len(arr)
# Adjusted loop to clearly iterate through keys in reversed order
for key in reversed(keys):
# Selection sort adapted for multiple keys
for i in range(size):
# Initially, min_index is the starting index
min_index = i
# Find the minimum element in remaining unsorted array
for j in range(i+1, size):
# Check condition for current sorting key
if arr[j][key] < arr[min_index][key]:
min_index = j
# Swap the found minimum element with the first element
arr[i], arr[min_index] = arr[min_index], arr[i]
def multilevel_selection_sort(elements, sort_by_list):
for sort_by in sort_by_list[-1::-1]:
for x in range(len(elements)):
min_index = x
for y in range(x, len(elements)):
if elements[y][sort_by] < elements[min_index][sort_by]:
min_index = y
if x != min_index:
elements[x], elements[min_index] = elements[min_index], elements[x]
tests = [
[89, 78, 61, 53, 23, 21, 17, 12, 9, 7, 6, 2, 1],
[],
[1,5,8,9],
[234,3,1,56,34,12,9,12,1300],
[78, 12, 15, 8, 61, 53, 23, 27],
[5]
]
# for elements in tests:
# selection_sort(elements)
# print(elements)
element2 = [
{'First Name': 'Raj', 'Last Name': 'Nayyar'},
{'First Name': 'Suraj', 'Last Name': 'Sharma'},
{'First Name': 'Karan', 'Last Name': 'Kumar'},
{'First Name': 'Jade', 'Last Name': 'Canary'},
{'First Name': 'Raj', 'Last Name': 'Thakur'},
{'First Name': 'Raj', 'Last Name': 'Sharma'},
{'First Name': 'Kiran', 'Last Name': 'Kamla'},
{'First Name': 'Armaan', 'Last Name': 'Kumar'},
{'First Name': 'Jaya', 'Last Name': 'Sharma'},
{'First Name': 'Ingrid', 'Last Name': 'Galore'},
{'First Name': 'Jaya', 'Last Name': 'Seth'},
{'First Name': 'Armaan', 'Last Name': 'Dadra'},
{'First Name': 'Ingrid', 'Last Name': 'Maverick'},
{'First Name': 'Aahana', 'Last Name': 'Arora'}
]
multilevel_selection_sort(element2,['First Name','Last Name'])
print(element2)