I've no questions about how an array are sorted. I'm asking how a randomly selected array I sent to the sort function is automatically sorted, even though I've assigned the output to a different array.
bubbleList = createRandomArray()
sortedList = BubbleSort(bubbleList)
print(bubbleList)
print(sortedList)
A sample output of the piece of code written as above is as follows:
[-15, -14, -12, -12, -9, -6, -5, -4, 0, 10]
[-15, -14, -12, -12, -9, -6, -5, -4, 0, 10]
But if I print the unsorted list without sending it to the function, there's no sorting. Then the output is as follows:
[-14, -6, -15, 0, -5, -9, -4, -12, -12, 10]
[-15, -14, -12, -12, -9, -6, -5, -4, 0, 10]
Could it be due to the automatic assignment made by the python? Many thanks.
Added BubbleSort:
def BubbleSort(inlist):
n = len(inlist)
for i in range(n-1, -1, -1):
for j in range(0, i):
if not(inlist[j] < inlist[j+1]):
inlist[j], inlist[j+1] = inlist[j+1], inlist[j]
return inlist