[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 ...
随机推荐
- Myeclipse2016部署tomcat服务(别的服务类似)配置环境
1.在工具MyEclipse的项目管理菜单中,右单机找Properties或者快捷键alt+enter,(或者直接搜索Runtimes)myEclipse/Targeted Runtimes 2.ne ...
- camstar --设备保养
- shell脚本中的反引号使用 `
反引号是~的英文切换 在shell脚本中,反引号允许将shell命令的输出赋值给变量. test=`date` 这样shell会执行反引号中的命令.并将执行的结果赋值给变量tests.
- python学习之——安装Beautifulsoup、requests、lxml
安装Beautiful soup: 1.下载安装包,解压到python的安装目录: 2.cmd 进入安装包解压后的存放位置: 3.使用命令:python setup.py build , pyt ...
- git 调用 Beyond Compare
转载自 http://www.jackness.org/2015/03/31/git-%E8%B0%83%E7%94%A8-%E7%AC%AC%E4%B8%89%E6%96%B9%E5%AF%B9% ...
- 记一次TFS 的 垃圾提示(无法下载 未获取项目 的 代码)
提示 “ 所有文件都是最新的 ”,但是在 源码管理 里面 确是 “未下载” 我艹,第一次遇到.如图.~~ 最后发现是 TFS 的项目权限设置问题. 你妈个马批的,啥子鸡巴破B提示,太阳你妈B 的 .要 ...
- 循环结构——whlie do whlie for for each
1. while循环 while(循环条件){ (特点为:先判断再执行) 循环操作 } 例题: 计算1+2+3+...+100 int i = 1; int sum = 0; wh ...
- 在本地创建angular-ui/bootstrap项目
在本地创建完整的angular-ui/Bootstrap项目 git clone the repo, then switch to the tag you want,then use grunt bu ...
- Asp.Net MVC4入门指南(6):验证编辑方法和编辑视图
在本节中,您将开始修改为电影控制器所新加的操作方法和视图.然后,您将添加一个自定义的搜索页. 在浏览器地址栏里追加/Movies, 浏览到Movies页面.并进入编辑(Edit)页面. Edit(编辑 ...
- Flask备注二(Configurations, Signals)
Flask备注二(Configuration, Signals) Flask是一个使用python开发Web程序的框架.依赖于Werkzeug提供完整的WSGI支持,以及Jinja2提供templat ...