0
\$\begingroup\$

I am learning Python and for practice I have written this function which sorts a list.

Can you please provide feedback on how I have structured it and what better ways could be there to write it for optimization/structuring?

originallist=[5, 4, 0, 3, 2,1]
lowervalue=int()

duplicatelist=originallist[:] ## This list is used to compare each element of the list with the first index of original list
sortedlist=[] ## Sorted list 

while len(originallist)>0: # See if there are any elements left in list original list
  lowervalue=0.01   
  for i in duplicatelist:  
   
    
    if originallist[0]<=i:
      temp=originallist[0]
      print('Temp: ', temp)

    elif originallist[0]>i:
      temp=i
      print('Temp: ', temp)

    if lowervalue>temp or lowervalue==0.01:
      lowervalue=temp
      print('LowerValue: ', lowervalue)

  sortedlist.append(lowervalue)
  print('Sorted List: ', sortedlist)
  duplicatelist.remove(lowervalue)
  print('Duplicate List: ', duplicatelist)
  originallist.remove(lowervalue)
  print('original List: ', originallist, ' \n')
  

print(f'Sorted List :' , sortedlist)

Thank you!

\$\endgroup\$
5
  • \$\begingroup\$ You've implemented an insertion sort (I think), which is inherently not really an optimal way to sort things. Do you want answers within the context of implementing insertion sorting in python or optimal sorting of a list in general? (i.e. originallist.sort()) :) \$\endgroup\$
    – JeffUK
    Dec 1 '21 at 19:22
  • \$\begingroup\$ Thank you for the feedback @JeffUk! It would be great if you could share insights on optimal sorting of list in general. list.sort(). And also any feedback to improve the thinking pattern to develop the algorithms based on the above code e.g. if I should be changing the mindset to approach a problem etc \$\endgroup\$ Dec 1 '21 at 19:33
  • \$\begingroup\$ @JeffUK It's selection sort. \$\endgroup\$ Dec 1 '21 at 19:36
  • \$\begingroup\$ @KellyBundy I was just about to change my comment after doing some research. Should have taken Compsci instead of IT.. \$\endgroup\$
    – JeffUK
    Dec 1 '21 at 19:46
  • 1
    \$\begingroup\$ I have added the reinventing-the-wheel tag as otherwise the answer "use the Timsort built into Python from Python 2.3 by using list.sort() or sorted" is 'the best' but possibly not too helpful answer. \$\endgroup\$
    – Peilonrayz
    Dec 1 '21 at 22:17
3
\$\begingroup\$

There's some odd features to this code, even for a coding practice exercise. Python of course has a couple of list sorting techniques which you should absolutely use in normal circumstances.

So accepting that you are doing this for practice, I wonder what the point of the duplicatelist is? Or indeed temp? Are these development fossils of some earlier process that you have modified? Looping across this list just seems to find the minimum value in the remaining original list anyway, so

originallist=[5, 4, 8, 2, 3, 6, 0, 1, 7]

sortedlist=[] ## Sorted list 

while len(originallist)>0: # See if there are any elements left in original list

  lowervalue = originallist[0]
  for i in originallist:  
    if lowervalue > i:
      lowervalue = i
      print('New LowerValue: ', lowervalue)

  sortedlist.append(lowervalue)
  print('Growing Sorted List: ', sortedlist)
  originallist.remove(lowervalue)
  print('Reduced Original List: ', originallist, ' \n')
  

print('Sorted List :' , sortedlist)

would apparently do the same.

Of course there is also a min function that would find lowervalue quickly. This eliminates the inner loop (is this maybe why you didn't use it?):

originallist = [5, 4, 8, 2, 3, 6, 0, 1, 7]

sortedlist=[] ## Sorted list 

while len(originallist)>0: # See if there are any elements left in original list

  lowervalue = min(originallist)
  print('LowerValue: ', lowervalue)

  sortedlist.append(lowervalue)
  print('Growing Sorted List: ', sortedlist)
  originallist.remove(lowervalue)
  print('Reduced Original List: ', originallist, ' \n')
  

print('Sorted List :' , sortedlist)

Using remove from a list can be slow - effectively rewriting the remainder of the list. For the purposes you have here, I don't think it makes an appreciable difference and has the slight advantage of being easy to understand.

One construct you will often see in a reducing list is phrasing the while loop like this:

while originallist:

... any non-empty list evaluates as True in such a test, so this is the same test as you are making.

\$\endgroup\$
4
  • \$\begingroup\$ @Syed You have to be careful with magic numbers as sentinel values. If I put 0.01 into your original list, it doesn't get sorted. \$\endgroup\$
    – Teepeemm
    Dec 2 '21 at 1:42
  • \$\begingroup\$ Thank you soo much Joffan! Your code makes a lot more sense. It is just that I was viewing the problem from very different perspective (given I have started with python recently with no back experience in algorithms and coding). The initial thought was to compare the originallist elements with itself and to visualize it better I went ahead with duplicating the list and comparing the elements. \$\endgroup\$ Dec 2 '21 at 9:15
  • \$\begingroup\$ The thought process behind the variable temp was there to store the smaller value when ever it compared 2 elements (the one from original list being compared to the one from duplicate). And then compare it with lowervalue to see if it is smaller among the list till that point in the iteration. But I can see your method is much simpler and efficient. I just could not see it that way while coding. I did not use the min function intentionally as I was trying to write code without python helping with the built in functions for practice. \$\endgroup\$ Dec 2 '21 at 9:16
  • \$\begingroup\$ Yes Teepeemm! I was struggling with what value to initialize for lowervalue but now i see we should do it with the first value of originallist as in the above code! \$\endgroup\$ Dec 2 '21 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.