[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 ...
随机推荐
- python项目实践一:即时标记
转自:http://www.code123.cc/1317.html 这是<python基础教程>后面的实践,照着写写,一方面是来熟悉python的代码方式,另一方面是练习使用python ...
- Ubuntu如何更新源
Ubuntu的源其实就是更新各种软件包需要用到镜像网站, 当大家在虚拟机上安装Linux镜像的时候肯定会遇到各种Linux软件没有安装,当你用apt-get安装的时候它会提示无效的网址,这个时候你就需 ...
- (转)java中静态代码块的用法 static用法详解
一)java 静态代码块 静态方法区别一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序来 ...
- ABP JTable如何手动刷新子表数据
function getSubMaster() { _$masterTable.find('.jtable-child-table-container').jtable('reload'); }
- ZT 北大青鸟APTECH(南京泰思特)
北大青鸟APTECH(南京泰思特)授权培训中心诚招 高级软件测试/开发培训讲师 Posted on 2008-01-14 11:38 追求卓越 阅读(590) 评论(7) 编辑 收藏 北京阿博泰克北大 ...
- 自动生成查找组件的lua代码
本篇主要解决的问题是使用lua脚本编写unity业务逻辑时,自动生成一些查找组件及绑定控件事件的lua代码! 现在很多unity项目都是用ulua作为热更新解决方案,因此需要用lua来写相关的逻辑,经 ...
- python ide ---wing 注册机
注册机脚本代码如下: import sha import string BASE2 = '01' BASE10 = '0123456789' BASE16 = '0123456789ABCDEF' B ...
- Nginx-->基础-->理论-->002:Nginx进程介绍
一.Nginx进程介绍
- C#(asp.net )读取ASHX文件(一般处理程序)
c#后台获取asxh的返回数据,后台创建一个请求实例,获取请求实例的返回值 public string GetResponseByPost(string apiUrl, string queryStr ...
- mysql 用命令操作
本篇文章来源于http://c.biancheng.net/cpp/html/1441.html mysql:连接数据库 mysql命令用户连接数据库. mysql命令格式: mysql -h主机地址 ...