Skip to content

Commit c13c86f

Browse files
added selection sort code
1 parent db3eaec commit c13c86f

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

7.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# # Exercise: Selection Sort
2+
3+
# Implement a Multi-Level Sort of a given list of dictionaries based on a given sorting order.
4+
# If user wants to sort dictionary based on First Key 'A', Then Key 'B',
5+
# they shall pass list of keys in the order of preference as a list ['A','B'].
6+
# Your code should be able to sort list of dictionaries for any number of keys in sorting order list.
7+
8+
# Using this multi-level sort, you should be able to sort any list of dictionaries based on sorting order preference
9+
10+
# Example:
11+
# 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.
12+
13+
# for this, one shall past sorting order of preference list [ 'First Name' , 'Last Name' ]
14+
15+
# For this, Given the following sequence List:
16+
17+
# ```
18+
# [
19+
# {'First Name': 'Raj', 'Last Name': 'Nayyar'},
20+
# {'First Name': 'Suraj', 'Last Name': 'Sharma'},
21+
# {'First Name': 'Karan', 'Last Name': 'Kumar'},
22+
# {'First Name': 'Jade', 'Last Name': 'Canary'},
23+
# {'First Name': 'Raj', 'Last Name': 'Thakur'},
24+
# {'First Name': 'Raj', 'Last Name': 'Sharma'},
25+
# {'First Name': 'Kiran', 'Last Name': 'Kamla'},
26+
# {'First Name': 'Armaan', 'Last Name': 'Kumar'},
27+
# {'First Name': 'Jaya', 'Last Name': 'Sharma'},
28+
# {'First Name': 'Ingrid', 'Last Name': 'Galore'},
29+
# {'First Name': 'Jaya', 'Last Name': 'Seth'},
30+
# {'First Name': 'Armaan', 'Last Name': 'Dadra'},
31+
# {'First Name': 'Ingrid', 'Last Name': 'Maverick'},
32+
# {'First Name': 'Aahana', 'Last Name': 'Arora'}
33+
# ]
34+
# ```
35+
36+
37+
# Your algorithm should generate sorted list:
38+
39+
# ```
40+
# [
41+
# {'First Name': 'Aahana', 'Last Name': 'Arora'}
42+
# {'First Name': 'Armaan', 'Last Name': 'Dadra'}
43+
# {'First Name': 'Armaan', 'Last Name': 'Kumar'}
44+
# {'First Name': 'Ingrid', 'Last Name': 'Galore'}
45+
# {'First Name': 'Ingrid', 'Last Name': 'Maverick'}
46+
# {'First Name': 'Jade', 'Last Name': 'Canary'}
47+
# {'First Name': 'Jaya', 'Last Name': 'Seth'}
48+
# {'First Name': 'Jaya', 'Last Name': 'Sharma'}
49+
# {'First Name': 'Karan', 'Last Name': 'Kumar'}
50+
# {'First Name': 'Kiran', 'Last Name': 'Kamla'}
51+
# {'First Name': 'Raj', 'Last Name': 'Nayyar'}
52+
# {'First Name': 'Raj', 'Last Name': 'Sharma'}
53+
# {'First Name': 'Raj', 'Last Name': 'Thakur'}
54+
# {'First Name': 'Suraj', 'Last Name': 'Sharma'}
55+
# ]
56+
# ```
57+
58+
def selection_sort(arr):
59+
size = len(arr)
60+
for i in range(size-1):
61+
min_index = i
62+
for j in range(min_index+1,size):
63+
if arr[j] < arr[min_index]:
64+
min_index = j
65+
if i != min_index:
66+
arr[i], arr[min_index] = arr[min_index], arr[i]
67+
68+
# def selection_sort_multi(arr,keys):
69+
# size = len(arr)
70+
# # for key in keys[-1::-1]:
71+
# # for i in range(size):
72+
# # min_index = i
73+
# # for j in range(i+1,size):
74+
# # if arr[j][key] < arr[min_index][key]:
75+
# # min_index = j
76+
# # arr[i], arr[min_index] = arr[min_index], arr[i]
77+
# for key in reversed(keys):
78+
# for i in range(size):
79+
# min_or_max_index = i
80+
# for j in range(i+1, size):
81+
# if arr[j][key] < arr[min_or_max_index][key]:
82+
# min_or_max_index = j
83+
# # Swap the found minimum element with the first element
84+
# arr[i], arr[min_or_max_index] = arr[min_or_max_index], arr[i]
85+
86+
# def selection_sort_multi(elements, sort_by_list):
87+
# for sort_by in sort_by_list[-1::-1]:
88+
# for x in range(len(elements)):
89+
# min_index = x
90+
# for y in range(x, len(elements)):
91+
# if elements[y][sort_by] < elements[min_index][sort_by]:
92+
# min_index = y
93+
# if x != min_index:
94+
# elements[x], elements[min_index] = elements[min_index], elements[x]
95+
96+
def selection_sort_multi(arr, keys):
97+
size = len(arr)
98+
99+
# Adjusted loop to clearly iterate through keys in reversed order
100+
for key in reversed(keys):
101+
# Selection sort adapted for multiple keys
102+
for i in range(size):
103+
# Initially, min_index is the starting index
104+
min_index = i
105+
# Find the minimum element in remaining unsorted array
106+
for j in range(i+1, size):
107+
# Check condition for current sorting key
108+
if arr[j][key] < arr[min_index][key]:
109+
min_index = j
110+
111+
# Swap the found minimum element with the first element
112+
arr[i], arr[min_index] = arr[min_index], arr[i]
113+
114+
def multilevel_selection_sort(elements, sort_by_list):
115+
for sort_by in sort_by_list[-1::-1]:
116+
for x in range(len(elements)):
117+
min_index = x
118+
for y in range(x, len(elements)):
119+
if elements[y][sort_by] < elements[min_index][sort_by]:
120+
min_index = y
121+
if x != min_index:
122+
elements[x], elements[min_index] = elements[min_index], elements[x]
123+
124+
tests = [
125+
[89, 78, 61, 53, 23, 21, 17, 12, 9, 7, 6, 2, 1],
126+
[],
127+
[1,5,8,9],
128+
[234,3,1,56,34,12,9,12,1300],
129+
[78, 12, 15, 8, 61, 53, 23, 27],
130+
[5]
131+
]
132+
133+
# for elements in tests:
134+
# selection_sort(elements)
135+
# print(elements)
136+
137+
element2 = [
138+
{'First Name': 'Raj', 'Last Name': 'Nayyar'},
139+
{'First Name': 'Suraj', 'Last Name': 'Sharma'},
140+
{'First Name': 'Karan', 'Last Name': 'Kumar'},
141+
{'First Name': 'Jade', 'Last Name': 'Canary'},
142+
{'First Name': 'Raj', 'Last Name': 'Thakur'},
143+
{'First Name': 'Raj', 'Last Name': 'Sharma'},
144+
{'First Name': 'Kiran', 'Last Name': 'Kamla'},
145+
{'First Name': 'Armaan', 'Last Name': 'Kumar'},
146+
{'First Name': 'Jaya', 'Last Name': 'Sharma'},
147+
{'First Name': 'Ingrid', 'Last Name': 'Galore'},
148+
{'First Name': 'Jaya', 'Last Name': 'Seth'},
149+
{'First Name': 'Armaan', 'Last Name': 'Dadra'},
150+
{'First Name': 'Ingrid', 'Last Name': 'Maverick'},
151+
{'First Name': 'Aahana', 'Last Name': 'Arora'}
152+
]
153+
154+
multilevel_selection_sort(element2,['First Name','Last Name'])
155+
print(element2)

0 commit comments

Comments
 (0)