I created the class Sorter
which sets the variable self.list
in the __init__
equal to a given argument. I then created a function selectionSort
which should copy the value of self.list
into a new variable unsortedList
. That worked out, but when I then change unsortedList
, the self.list
variable changes as well.
Here's my code:
class Sorter:
def __init__(self, list):
self.list = list
def selectionSort(self):
unsortedList = self.list
sortedList = []
indexSmallest = 0
while len(unsortedList)>0:
for i in range(len(unsortedList)):
if unsortedList[i] <= unsortedList[indexSmallest]:
indexSmallest = i
sortedList.append(unsortedList[indexSmallest])
unsortedList.pop(indexSmallest)
indexSmallest = 0
return sortedList
sorter = Sorter([2,6,1,8,5])
print(sorter.selectionSort())
I expect self.list
to be the same as before calling the selectionSort()
function but the result I get is an empty self.list
variable.