東京大学 新領域創成科学研究科 メディカル情報生命専攻 2016年8月実施 問題9
Author
Description
Suppose that
A) Select and remove an element
B) Divide
C) Sort
Answer the following questions.
(1) How would you implement Step B in quicksort?
(2) In Step A, if we always set the first element in
(3) In Step A, if we select a position in
(4) Design an algorithm that calculates the
假设
A) 从
B) 将
C) 使用快速排序按升序排列
回答以下问题。
(1) 你将如何在快速排序中实现步骤 B?
(2) 在步骤 A 中,如果我们总是将
(3) 在步骤 A 中,如果我们在
(4) 设计一个算法,在
Kai
(1)
To implement Step B, we need to partition the array
- Initialize an index
to track the boundary of elements less than or equal to the pivot. - Traverse the array from left to right, comparing each element with the pivot.
- Swap elements to ensure all elements less than or equal to the pivot are on its left, and all elements greater than the pivot are on its right.
Here is a Python function to achieve this:
def partition(arr, low, high):
pivot = arr[high] # Choose the last element as pivot
i = low - 1 # i: Index of smaller element
for j in range(low, high):
if arr[j] <= pivot:
i += 1 # Increment index of smaller element
arr[i], arr[j] = arr[j], arr[i]
# Swap the current element to teh end of the smaller half of arr
arr[i+1], arr[high] = arr[high], arr[i+1] # Place pivot in correct position
return i+1 # Return the partition index
(2)
If we always select the first element as the pivot, the worst-case scenario occurs when the array is already sorted (either in ascending or descending order).
Example of Worst-case Input:
Proof of
- In the first call, the pivot is the smallest element (1), and the array is divided into
and . - In the second call, the pivot is the smallest element in
(2), and the array is divided into and . - This process continues, making
comparisons in the first step, in the second step, and so on.
The total number of comparisons is:
(3)
To rigorously prove that the expected time complexity of Quicksort with a random pivot selection is
Definitions and Assumptions
- Let
be the expected time complexity of Quicksort on an array of size . - Each pivot is chosen uniformly at random from the array.
is the number of comparisons made by Quicksort on an array of size .
Key Observations
-
When a pivot
is chosen, it partitions the array into two subarrays and . The sizes of and depend on the position of in the sorted array. -
If the pivot is the
-th smallest element, will have elements and will have elements. -
The number of comparisons required to partition the array around the pivot is
.
Expected Comparisons
We aim to find
The recurrence relation for
where
The expected number of comparisons is:
Taking Expectation
Since the pivot is chosen randomly, the expected sizes of
This simplifies to:
Solving the Recurrence Relation
To solve the recurrence relation, we will use the concept of telescoping sums and known techniques for analyzing recurrence relations.
First, we need to assume that
Assume
We can approximate the sum
Compute the integral:
Evaluate the integral from
At
At
So the integral result is approximately:
Simplifying this, we get:
Thus,
Substitute this back into the recurrence relation:
Simplify:
Since lower-order terms are negligible when considering asymptotic behavior, we get:
Thus, the expected number of comparisons
(4)
This can be achieved using the Quick-select algorithm, which is similar to Quicksort but only recurses into one partition.
Algorithm:
- Choose a pivot element
randomly. - Partition the array into
and as described in Step B. - If the size of
is , then is the -th smallest element. - If the size of
is greater than , recurse into . - Otherwise, recurse into
with the adjusted value.
Python Code:
import random
def quickselect(arr, low, high, k):
if low == high:
return arr[low]
pivot_index = random.randint(low, high)
pivot_index = partition(arr, low, high)
if k == pivot_index:
return arr[k]
elif k < pivot_index:
return quickselect(arr, low, pivot_index - 1, k)
else:
return quickselect(arr, pivot_index + 1, high, k)
def find_kth_smallest(arr, k):
return quickselect(arr, 0, len(arr) - 1, k - 1)
Proof of
The Quickselect algorithm has the same recurrence as Randomized Quicksort, but only recurses into one subarray. Hence, the expected time complexity is:
This solves to
Knowledge
快速排序 时间复杂度 分治算法
重点词汇
- Quicksort 快速排序
- Pivot 枢轴
- Partition 分区
- Expected time complexity 期望时间复杂度
- Recurrence relation 递推关系
参考资料
- Introduction to Algorithms, Cormen, Leiserson, Rivest, and Stein, 3rd Edition, Chap. 7 (Quicksort), Chap. 9 (Medians and Order Statistics)