[UCSD白板题] Sorting: 3-Way Partition
Problem Introduction
The goal in this problem is to redesign a given implementation of the randomized quick sort algorithm so that it works fast even on sequences containing many equal elements.
Problem Description
Task.To force the given implementation of the quick sort algorithm to efficiently process sequences with few unique elements, your goal is replace 2-way partition with a 3-way partition. That is, your new partition procedure should partition the array into three parts: \(<x\) part, \(=x\) part, and \(>x\) part.
Input Format.The first line of the input contains an integer \(n\). The next line contains a sequence of \(n\) integers \(a_0, a_1, \cdots, a_{n-1}\).
Constraints.\(1 \leq n \leq 10^5; 1 \leq a_i \leq 10^9\) for all \(0 \leq i \leq n\).
Output Format.Output this sequence sorted in non-decreasing order.
Sample 1.
Input:
5
2 3 9 2 2
Output:
2 2 2 3 9
Solution
# Uses python3
import sys
import random
def partition3(a, l, r):
x = a[l]
m1, m2 = l, r
i = l+1
while i <= m2:
if a[i] < x:
a[i], a[m1] = a[m1], a[i]
m1 += 1
i += 1
elif a[i] > x:
a[i], a[m2] = a[m2], a[i]
m2 -= 1
else:
i += 1
return m1, m2
def partition2(a, l, r):
x = a[l]
j = l
for i in range(l + 1, r + 1):
if a[i] <= x:
j += 1
a[i], a[j] = a[j], a[i]
a[l], a[j] = a[j], a[l]
return j
def randomized_quick_sort(a, l, r):
if l >= r:
return
k = random.randint(l, r)
a[l], a[k] = a[k], a[l]
#use partition3
m1, m2 = partition3(a, l, r)
randomized_quick_sort(a, l, m1 - 1);
randomized_quick_sort(a, m2 + 1, r);
if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
randomized_quick_sort(a, 0, n - 1)
for x in a:
print(x, end=' ')
[UCSD白板题] Sorting: 3-Way Partition的更多相关文章
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- [UCSD白板题] Maximize the Value of an Arithmetic Expression
Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [UCSD白板题] Points and Segments
Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...
- [UCSD白板题] Number of Inversions
Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...
- [UCSD白板题] Majority Element
Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...
- [UCSD白板题] Binary Search
Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...
随机推荐
- Win32 OpenGL标准例子
在VS2008的MSDN中有一个标准的OpenGL例子,记录如下: /* * Example of a Win32 OpenGL program. * The OpenGL code is the s ...
- funny_python 01 import antigravity
在Python里面import antigravity 会发现另一个小彩蛋 Tada~ 其实就是打开了xkcd的一个python主题漫画 鼠标停留在图片上,还会看到标题: "I wrote ...
- 如何把apdu[decode_len]打印出来
memcpy(data, &apdu[decode_len], apdu_len - decode_len); int i = 0; for(i=0;i<apdu_len;i ...
- SAP 采购订单行项目中科目分配被隐藏,发现行项目设置中显示字段长度为0
1.sm30 维护 视图 TCVIEW 修改对应字段的显示长度
- EF 连接MySQL 数据库 保存中文数据后乱码问题
EF 连接MySQL 数据库 保存中文数据后乱码问题 采用Code First 生成的数据库,MySQL数据库中,生成的表的编码格式为***** 发现这个问题后,全部手动改成UTF8(图是另一个表的 ...
- SQL Server 数据库 'xxx' 正处于转换状态。请稍后再尝试该语句。
问题是这样的,最近因义务需要,公司更换了数据库服务器.数据库随之切换到新的服务器上. 服务器是 Windows Server 2012系统,数据库是SQL Server 2012 .上面有 多个数据库 ...
- z-index和transform
z-index和transform是CSS中的属性,但很少同学将二者联系到一起,感觉他们八杆子打不上.事实真的是这样吗?如果你也不能确认,这篇文章就值得你花点时间阅读.因为阅读完了,你会有所收获的. ...
- JavaScript中关于时间的知识点
1.计算时间差,天数,小时数,余数 var begintime_ms = Date.parse(new Date(begintime.replace(/-/g, "/"))); / ...
- (原创)通用查询实现方案(可用于DDD)[附源码] -- 简介
[声明] 写作不易,转载请注明出处(http://www.cnblogs.com/wiseant/p/3985353.html). [系列文章] 通用查询实现方案(可用于DDD)[附源码] -- ...
- Oracle 12c 的新功能:模式匹配查询
模式匹配SQL 在一系列的数据行中辨识出某种模式是一种广泛需求的功能,但在以前的SQL中是不可能的.有许多解决方法,但这些都很难写,很难理解,而且执行效率低下.在Oracle数据库中,从12c开始,你 ...