Mar-26-2018, 03:42 PM
Hi, here there's a quicksort code in Python 3.
It works but my problem is this: i understand what the partition function makes, but i don't understand how the quickSort function is able to change the list if the partition function returns an index, not an element of the list.
Has anyone any hint or advise in order to give me an insight?
It works but my problem is this: i understand what the partition function makes, but i don't understand how the quickSort function is able to change the list if the partition function returns an index, not an element of the list.
Has anyone any hint or advise in order to give me an insight?
def partition(alist,first,last): pivotvalue = alist[first] leftmark = first+1 rightmark = last done = False while not done: while leftmark <= rightmark and alist[leftmark] <= pivotvalue: leftmark = leftmark + 1 while alist[rightmark] >= pivotvalue and rightmark >= leftmark: rightmark = rightmark -1 if rightmark < leftmark: done = True else: temp = alist[leftmark] alist[leftmark] = alist[rightmark] alist[rightmark] = temp temp = alist[first] alist[first] = alist[rightmark] alist[rightmark] = temp return rightmark def quickSort(alist,first,last): if first<last: splitpoint = partition(alist,first,last) quickSort(alist,first,splitpoint-1) quickSort(alist,splitpoint+1,last) alist = [4,2,11,1,5] quickSort(alist,0,len(alist)-1) print(alist)
Output:[1, 2, 4, 5, 11]