Question

def selectionSort(list1):
   for sixNumbers in range(len(list1) - 1, 0, -1):
       maxPos = 0
       for position in range(1, sixNumbers + 1):
           if list1[position] > list1[maxPos]:
               maxPos = position

       value = list1[sixNumbers]
       list1[sixNumbers] = list1[maxPos]
       list1[maxPos] = value

def main():
    list1 = [45, 7, 5, 24, 12, 1]
    selectionSort(list1)
    print(list1)

main()

How do I not use the len()? My instructor told me not to use the built-in sort function. Any suggestion? Also, do not give me the code, give me some tips, so I can try to rewrite it.

Était-ce utile?

La solution

A really dumb way of counting elements without using len() is to loop though the values using a for loop. Increment every time though the loop to get the count.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top