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的更多相关文章

  1. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  2. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  3. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  4. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  5. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  6. [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 ...

  7. [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 ...

  8. [UCSD白板题] Majority Element

    Problem Introduction An element of a sequence of length \(n\) is called a majority element if it app ...

  9. [UCSD白板题] Binary Search

    Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...

随机推荐

  1. AJAX - onreadystatechange

    [AJAX - onreadystatechange] 参考:http://www.w3school.com.cn/ajax/ajax_xmlhttprequest_onreadystatechang ...

  2. Elasticsearch 字段数据类型

    Elasticsearch 可以支持单个document中含有多个不同的数据类型. 核心数据类型(Core datatypes) 字符型(String datatype):string 数字型(Num ...

  3. oracle 调用java

    1.首先在PL/SQL中创建JAVA类,并编译 例如:这个是到的一个查询目录下面文件列表的java类 创建此java 类用: create or replace and compile java so ...

  4. Java OOP中的字符串篇

    字符串的三大特征: String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 一.定义 查看 API 会发现,String.St ...

  5. JSP开发模式2_JSP/Servlet/JavaBean(简单注册功能)

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegisterBean {    privat ...

  6. WebLogic 中的基本概念

    完全引用自: WebLogic 中的基本概念 WebLogic 中的基本概念 上周参加了单位组织的WebLogic培训,为了便于自己记忆,培训后,整理梳理了一些WebLogic的资料,会陆续的发出来, ...

  7. 解决: maven编译项目报“非法字符: \65279 ”错误

    打包maven项目的时候,出现异常: [INFO] ------------------------------------------------------------------------ [ ...

  8. myeclipse中如何导入mysql-connector-java-5.1.8-bin.jar【环境配置和工具使用】

    前提:我建立了一个java project,工程名字为Test,现在需要连接mysql数据库,所以提前从网上将java操作mysql数据库的mysql-connector-java-5.1.8-bin ...

  9. WebService基本概念及原理

    一.Web Service基本概念 WebService是一种跨编程语言和跨操作系统平台的远程调用技术.Web Service也叫XML Web Service WebService是一种可以接收从I ...

  10. 原生js arguments 用法

    http://note.youdao.com/noteshare?id=fcd201e872e1ede16ce0057a1909f613