Skip to content

Selection sort exercise #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions algorithms/7_SelectionSort/selection_sort_exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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'}
]
```





[Solution](https://github.com/codebasics/data-structures-algorithms-python/blob/master/algorithms/7_SelectionSort/selection_sort_exercise_solution.py)
33 changes: 33 additions & 0 deletions algorithms/7_SelectionSort/selection_sort_exercise_solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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]


if __name__ == '__main__':
elements = [
{'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'}
]

print(f'Given unsorted array:', *elements, sep='\n')
multilevel_selection_sort(
elements, ['First Name', 'Last Name'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multilevel_selection_sort(
elements, ['First Name', 'Last Name']
)

print(f'Array after Multi-Level Sorting:', *elements, sep='\n')