First, if I do not specify the -f parameter, the script never runs the usage() function, it only tells "No such file or directory: ''". Why isn't my script running the usage() function?
getopt() doesn't know which flags are required and which are optional. It merely checks that you don't pass unspecified flags or omit an argument if a flag requires one with :
.
It's up to you to check that -f
is passed in if you require it.
Also, the bubble sort algorithm doesn't seem to work properly. If I run the script, the numbers aren't sorted properly. I can, for example, see 3998 before 403 in the list. But, I have noticed that the numbers ARE sorted, but only from the left of the numbers.
That's because your code is actually sorting strings rather than numbers, so it's putting them in lexicographic order. Try converting them to numbers when you read the file in:
unsortedList = [int(line.rstrip('\n')) for line in inputFile]
^^^^ ^
Also, I have to implement the algorithm in situ, which means I have to only use one list/array/etc without allocating any other temporary list/array/etc or variable to hold one or a part of all the numbers to sort in the algorithm.
In that case, I'd recommend removing the return statement from the bubble sort function. The best way to show that you're only using one array is not to create a second variable called sortedList
.
bubble(unsortedList)
If your bubble sort call is simply that, without the assignment, then it's clear you must be modifying the original array.