测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A60993. 有n位同学的成绩已经从小到大排好序,现在对它执行下面这段以第一个元素为 pivot 的快速排序,请问此次排序的时间复杂度是( )。1 def quicksort(a, l, r)

单选题

题目描述

有n位同学的成绩已经从小到大排好序,现在对它执行下面这段以第一个元素为 pivot 的快速排序,请问此次排序的时间复杂度是( )。

1 def quicksort(a, l, r):
2  if l >= r:
3   return
4  pivot = a[l]
5  i, j = l, r
6
7  while i < j:
8   while i < j and a[j] >= pivot:
9    j -= 1
10   while i < j and a[i] <= pivot:
11    i += 1
12   if i < j:
13    a[i], a[j] = a[j], a[i]
14
15  a[l], a[i] = a[i], a[l]
16
17  quicksort(a, l, i - 1)
18  quicksort(a, i + 1, r)
19
20 if __name__ == "__main__":
21  scores = [60, 70, 80, 90, 100]
22  print("排序前:", scores)
23  quicksort(scores, 0, len(scores)-1)
24  print("排序后:", scores) # 输出:[60, 70, 80, 90, 100]
25  def quicksort_with_log(a, l, r, depth=0):
26   if l >= r:
27    return
28   print(f"递归深度{depth},处理区间[{l},{r}],数组:{a[l:r+1]}")
29   pivot = a[l]
30   i, j = l, r
31   while i < j:
32    while i < j and a[j] >= pivot: j -= 1
33    while i < j and a[i] <= pivot: i += 1
34    if i < j: a[i], a[j] = a[j], a[i]
35   a[l], a[i] = a[i], a[l]
36   quicksort_with_log(a, l, i-1, depth+1)
37   quicksort_with_log(a, i+1, r, depth+1)
38
39  scores2 = [60,70,80,90,100]
40  print("\n递归过程:")
41  quicksort_with_log(scores2, 0, 4)


选项(单选)