Skip to content

Create selectionsort.py #2

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

akito-shine
Copy link

Added selection sort algorithm code to data structure and algorithm. Looking forward to feedback.

Added selection sort algorithm code to data structure
Copy link

@PhooPyae PhooPyae left a comment

Choose a reason for hiding this comment

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

Pls check my comments

@@ -0,0 +1,40 @@
# Here we use dictionary data structure to store our count of values mapped to respective names in the above photos
singers={"Selena Gomez": 112, "Taylor Swift": 178,"Ed Sheeran": 230,"Justin Bieber": 90,"John Legend": 190,"Beyoncé": 80}
Copy link

Choose a reason for hiding this comment

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

please write global variable in

if name == 'main':

you can check in Jan's Binary Tree code.

Copy link
Author

Choose a reason for hiding this comment

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

Will do and I searched for why is it necessary to add this, but nothing could clearly show me the reason. Could you please tell me why we should include this in ,sis?

Copy link
Author

Choose a reason for hiding this comment

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

I updated if name == 'main':, but I wonder if the place I put in is correct

singers={"Selena Gomez": 112, "Taylor Swift": 178,"Ed Sheeran": 230,"Justin Bieber": 90,"John Legend": 190,"Beyoncé": 80}

# We then convert dictionary to list of only count values and in singers_list, it will be like this[112,178,230,90,190,80]
singers_list=list(singers.values())
Copy link

Choose a reason for hiding this comment

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

same as first one

Copy link
Author

Choose a reason for hiding this comment

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

Oh so, it's needed to add if name == 'main': two times?


# Starting from here, we define a function for selection sort
def selectionSort(singers_list):
#This is an empty list which will later be filled with sorted counting values
Copy link

Choose a reason for hiding this comment

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

write docstring instead of comments
Please check Jan's code.
docstring should follow this format
'''
description of the function
INPUT:
OUPUT:
'''

Copy link
Author

Choose a reason for hiding this comment

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

As my comments are a lot, would it be fine if I added every line of comments in docstring?

Copy link

Choose a reason for hiding this comment

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

You don't need to write all. Just descriptive explanation is ok.

Copy link

@PhooPyae PhooPyae Nov 9, 2021

Choose a reason for hiding this comment

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

Example docstring
'''
Sort the array by finding the largest elements and swapping the elements according to the index.
INPUT:
singer_list: list of singers corresponding to play counts
OUTPUT:
sorted_singer_lsit: list of sorted singer lists by play counts
'''

return singers_rating
# You remember largest variable above that calls for this function? Here we define it with the following code
def findLargest(singers_list):
#We assume the first value of singers' list as the largest value
Copy link

Choose a reason for hiding this comment

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

write docstring instead of comments
Make a short description. You don't need to explain everything in the function in detail. You can just describe some points which might be confusing.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, sis I thought I could do . But I am concerned about other beginners who don't know at all about code. So, I took a lot of time to write line by line. How do you want to further suggest me?

Copy link

Choose a reason for hiding this comment

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

we should follow the correct coding style. As long as the code is clear enough, they will understand.
Check out the coding styles of python.
https://www.python.org/dev/peps/pep-0008/
https://www.python.org/dev/peps/pep-0257/

#After updating our largest_index, return it to the function. Don't forget to look back at two skipped lines above :xD
return largest_index
#After applying selection sort algorithm, we add it to the variable name called favorite_singers
favorite_singers=selectionSort(singers_list)
Copy link

Choose a reason for hiding this comment

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

call function from
if name == 'main':

Copy link
Author

Choose a reason for hiding this comment

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

Done

# Then we print favorite_singers and get the following output
print(favorite_singers)

Output:[230, 190, 178, 112, 90, 80]
Copy link

Choose a reason for hiding this comment

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

what does this output mean? Please remove this if not necessary.

Copy link
Author

Choose a reason for hiding this comment

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

It's sth code testers will get after they have run code in the editor. I think I had better write it in comment.

Copy link

@PhooPyae PhooPyae Nov 9, 2021

Choose a reason for hiding this comment

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

They will get the output when they run. You can write the output in docstring output if you wish to.
For example.
'''
....
OUTPUT:
sorted_list: .....
Output [230, 190, 178, 112, 90, 80]
'''

#We call findLargest function and search for largest value in the sigers' list and I recommend to look first at next third line
largest = findLargest(singers_list)
#We got the largest value by calling findLargest function below and we afterwards remove that largest value from singers_list and then add to our empty list declared above
singers_rating.append(singers_list.pop(largest))
Copy link

Choose a reason for hiding this comment

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

you can simply add singers_list[index] -> pop is not required here.

Copy link
Author

@akito-shine akito-shine Nov 8, 2021

Choose a reason for hiding this comment

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

sis, all this code exists in selection sort chapter of Grokking algorithm. What I changed is only names. Every line of code is the same

Here is the source code
def findSmallest(arr):
smallest = arr[0] Stores the smallest value
smallest_index = 0 Stores the index of the smallest value
for i in range(1, len(arr)):
if arr[i] < smallest:
smallest = arr[I]
smallest_index = I
return smallest_index
Now you can use this function to write selection sort:
def selectionSort(arr): Sorts an array
newArr = []
for i in range(len(arr)):
smallest = findSmallest(arr)
newArr.append(arr.pop(smallest))
return newArr
print selectionSort([5, 3, 6, 2, 10])

#As we consider the first value of singers' list as the largest value, we make it as the largest index of 0
largest_index = 0
#We start from index 1 to the end within the length of singers' list
for i in range(1, len(singers_list)):
Copy link

Choose a reason for hiding this comment

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

you don't need to compare everything from the 1 to the last index.
what you need to do is compare i+1 to the last index because 0 to 1 assumes to be sorted after one iteration is over.

https://www.geeksforgeeks.org/selection-sort/

#After applying selection sort algorithm, we add it to the variable name called favorite_singers
favorite_singers=selectionSort(singers_list)
# Then we print favorite_singers and get the following output
print(favorite_singers)
Copy link

Choose a reason for hiding this comment

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

if you want to give the variable names 'favourite_singers', the output should be singer names.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, I wanna output singers' names instead of list of values. I am still finding ways to do it ,sis. But if you have recommendation on how to do it, I'm looking forward to it.

Copy link

Choose a reason for hiding this comment

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

if you use a dictionary, instead of an array, you need to sort the dictionary instead of a list. (key-value pair)

Copy link

Choose a reason for hiding this comment

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

you can write lambda function to sort of key-value pair.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants