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 7 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
28 changes: 28 additions & 0 deletions algorithms/7_SelectionSort/selection_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Copy link
Member

Choose a reason for hiding this comment

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

Remove this file

def selection_sort(elements):
for x in range(len(elements)):
#Set smallest index unsorted
minIndex=x
for y in range(x,len(elements)):
if elements[y]<elements[minIndex]:##Check if the element at y index is smaller than current minimum
minIndex=y##Set new Minimum Index
##Swap the minimum number with first unsorted number
if x!=minIndex:
elements[x],elements[minIndex]=elements[minIndex],elements[x]

if __name__ == '__main__':
#
tests = [
[11,9,29,7,2,15,28],
[3, 7, 9, 11],
[25, 22, 21, 10],
[29, 15, 28],
[],
[6]
]

for elements in tests:
print(f'Given unsorted array: {elements}')
selection_sort(elements)
print(f'Array after Sorting : {elements}')
print()
54 changes: 54 additions & 0 deletions algorithms/7_SelectionSort/selection_sort_exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Exercise: Selection Sort

Implement a Milti-Level Sort of a given list of dictionaries. 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.
Copy link
Member

Choose a reason for hiding this comment

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

Multi-level




For example, given the 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')