I have watched lecture 9 regarding the sorting methods and I am wondering why you can't just use the min function?
I used the following code to sort a list of numbers: def selSort1(L): sorted = [] while len(L) > 0: minVal = min(L) sorted.append(minVal) L.remove(minVal) L = sorted return L Is there a situation where this wouldn't work?
First, when you define function names its best to use Sel_Sort_1. You should return Sorted and not worry about L. Just assign a new list and assign it to the function with the paramater of your current list.
@Rogerdoger91. If you are promoting the Pep 8 Style guide then you don't be following your own recommendation. Or am I wrong? Doesn't Pep 8 say: "Function names should be lowercase, with words separated by underscores as necessary to improve readability." Thus, naming a function "Sel_Sort_1" as you recommend violates Pep 8. What am I missing?
Indeed. sel_sort_1 should be all lower case. Realistically select_sort would actually be the preferred name for your program. Its a good idea to focus on good programming style now. Heres a handy way to remember. define_function() ClassNames number_of_variables CONST_VARIABLES
I don't know what are the standards to name variables and functions in Python (as it seems that the two MIT professors are mixing styles), but in Java camelcase is the standard. Is it what you call Pep 8 ?
To give an answer to the original question, your program would run fine for sure. But think about the operations involved by the use of min: the program probably has to loop through the entire list to find the min. And you do that for each value of the original list. So you end up with an awfully slow program.
The point of the lecture on sorting is to understand the computational steps if you have to write a program from scratch. Using the min function is fine but it's relying partially on someone else's algorithm. It's the difference between building your own bookshelf from scratch or going to IKEA and purchasing one that is pre-fabricated. As a computer scientist you need to be able to do both. Put some things together yourself and then rely on others (through libraries) for other pieces.
malpaso's got a point; their Python implementation explicitly scans the sequence looking for the smallest item in the list to illustrate how selection sort really works.
Join our real-time social learning platform and learn together with your friends!