Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 52 lines (44 sloc) 1.257 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#!/usr/bin/env python
#-*- coding=utf-8 -*-
#
# Copyright 2013 Jike Inc. All Rights Reserved.
# Author: liwei@jike.com


# Douch National Flag algorithm for 4 colors

# for dnf2 and dnf3, refer to http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Flag/

def dnf4(seq):
    n = len(seq)
    l0 = l1 = 0
    l2 = l3 = n - 1

    # assumes:
    # seq[0...l0-1] = 0
    # seq[l0...l1-1] = 1
    # seq[l1, l2] = unknown
    # seq[l2+1, l3] = 2
    # seq[l3+1, n-1] = 3
    while l1 <= l2:
        if seq[l1] == 0:
            seq[l0], seq[l1] = seq[l1], seq[l0]
            l0 += 1
            l1 += 1
        elif seq[l1] == 1:
            l1 += 1
        elif seq[l1] == 2:
            seq[l1], seq[l2] = seq[l2], seq[l1]
            l2 -= 1
        elif seq[l1] == 3:
            # What we actually do here is:
            # translate seq[l2+1,l3] one cell left and then swap(l3, l1),
            # which is equivalent to two swap
            seq[l1], seq[l2] = seq[l2], seq[l1]
            seq[l2], seq[l3] = seq[l3], seq[l2]

            l2 -= 1
            l3 -= 1

import random
seq = []
for i in xrange(1000):
    seq.append(random.randint(0, 3))

print "origin seq:"
print seq
dnf4(seq)
print "after dnf:"
print seq
Something went wrong with that request. Please try again.