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. 【项目】搜索广告CTR预估(二)

    项目介绍 给定查询和用户信息后预测广告点击率 搜索广告是近年来互联网的主流营收来源之一.在搜索广告背后,一个关键技术就是点击率预测-----pCTR(predict the click-through ...

  2. sql server 数据遍历插入表变量

    )) DECLARE @str VARCHAR(MAX) ,) ,@start INT ,@end INT ,) SET @str = '1,2,3,4,5,6,7,8' SET @split = ' ...

  3. JavaScript内置对象之数组

    一.JavaScript对象之数组 1.创建数组的方式 (1)使用Array构造函数 语法:new Array() 小括号()说明: -预先知道数组要保存的项目数量 -向Array构造函数中传递数组应 ...

  4. iOS启动页设置

    点击项目->TARGETS->App Icons and Launch Images->Launch Images Source->Use Asset Catalog...-& ...

  5. Codeforces Round #385 //再遇状压

    敲完三题挂机一小时.....  也没懂DE什么意思  rank600上了一波分... A. Hongcow Learns the Cyclic Shift 给一个字符串,每次可以把最后一个字符拿到开头 ...

  6. java写入和写出EXCEL(含源代码)

    这两天帮老师做一个数据库,将所有实验交易的数据导入到数据库中,但是不想天天在实验室里面待着,气氛太压抑,就想着先把数据读进EXCEL中,哪天带到实验室导进去 数据原来是这样的,不同的实验有一个专门的文 ...

  7. jeasyui datagrid控件的一个小问题

    页面上用了datagrid,但今天把easyui更新到1.4.2以后出了个错,Cannot read property 'width' of null,以前用1.3.6的时候没有这个问题. 由于表格中 ...

  8. C++中数据对齐

    大体看了看数据对齐,不知道是否正确,总结如下: struct A { char name; double dHeight; int age; }; sizeof(A) = (1+7+8+4+4) =  ...

  9. httpServletRequest对象、filter、servlet、servlet容器、catalina、tomcat、以及web容器之间的关系

    学习servlet的时候经常感到疑惑 HttpServletRequest是服务器创建的?还是servlet容器创建的? 过滤器是服务器创建的?还是servlet容器创建的? serlet容器和tom ...

  10. Modern C++ CHAPTER 2(读书笔记)

    CHAPTER 2 Recipe 2-1. Initializing Variables Recipe 2-2. Initializing Objects with Initializer Lists ...