我试图让代码使用三种方法的中位数进行排序

时间:2018-03-13 18:39:44

标签: python-3.x

我试图让代码使用三种方法的中位数进行排序,并且我遇到了一些问题。

alist[first], alist[pivotindex] = alist[pivotindex], alist[first]

返回了无效的语法,我不知道为什么。

def quickSort(alist):
   quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
   if first<last:

       splitpoint = partition(alist,first,last)

       quickSortHelper(alist,first,splitpoint-1)
       quickSortHelper(alist,splitpoint+1,last)


def partition(alist,first,last):
   pivotindex = median(alist, first, last, (first + last //2)
   alist[first], alist[pivotindex] = alist[pivotindex], alist[first]
   pivotvalue = alist[first]

   leftmark = first+1
   rightmark = last

   done = False
   while not done:

       while leftmark <= rightmark and \
               alist[leftmark] <= pivotvalue:
           leftmark = leftmark + 1
           print(alist)

       while alist[rightmark] >= pivotvalue and \
               rightmark >= leftmark:
           rightmark = rightmark -1
           print(alist)

       if rightmark < leftmark:
           done = True
       else:
           temp = alist[leftmark]
           alist[leftmark] = alist[rightmark]
           alist[rightmark] = temp
           print(alist)

   temp = alist[first]
   alist[first] = alist[rightmark]
   alist[rightmark] = temp


   return rightmark

def median (a, i, j, k):
    if a [i] < a[j]:
        return j if a [j] < a[k] else k
    else:
        return i if a[i] < a[k] else k

alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)

1 个答案:

答案 0 :(得分:0)

因为它上面的一行缺少)