zoukankan      html  css  js  c++  java
  • schwartzian sort

    #Timing test for "sort on fourth word"
    #Specifically,two lines>=4 words will be sorted
    #lexographically on the 4th,5th,etc..words
    #Any line with fewer than four words will sorted to
    # the end ,and will occur in "natural" order
    import sys,string,time
    wrerr = sys.stderr.write
    #native custom sort
    def fourth_word(ln1,ln2):
        lst1 = string.split(ln1)
        lst2 = string.split(ln2)
        #Compare "long" lines
        if(len(lst1)>=4 and len(lst2)>=4):
            return cmp(lst1[3:],lst2[3:])
        #Long lines before short lines
        elif(len(lst1)>=4 and len(lst2)<4):
            return -1
        #Short lines before long lines
        elif(len(ls1)<4 and len(lst2)>=4):
            return 1
        else:       #natural order
            return cmp(ln1,ln2)
    #Dont count the read itself in the time
    lines = open(sys.argv[1]).readlines()
    #Time the custom comparison sort
    start = time.time()
    lines.sort(fourth_word)
    end = time.time()
    wrerr("Custom comparison func in %3.2f secs" % (end-start))

    lines = open(sys.argv[1]).readlines()
    #Time the schwartzian sort
    start = time.time()
    for n in range(len(lines)):
        lst = string.split(lines[n])
        if(len(lsr>=4)):
            lines[n] = (lst[3:],lines[n])
        else:
            lines[n] = (['\377'],lines[n])
    lines.sort()

    for n in range(len(lines)):
        lines[n] = lines[n][1]

    end = time.time()
    wrerr("Schwartzian transform sort in %3.2f secs" % (end-start))

  • 相关阅读:
    使用命令xrandr设置当前系统的显示分辨率及显示的旋转脚本
    CODEFORCE 246 Div.2 B题
    Android数据的四种存储方式之SQLite数据库
    C语言默认參数值的实现
    Android开发环境搭建
    也谈OpenFlow, SDN, NFV
    解决设置redmineblacklog的按钮无效问题
    长方体的研究
    表面张力与浮力
    表面张力与浮力
  • 原文地址:https://www.cnblogs.com/fenix/p/2335823.html
Copyright © 2011-2022 走看看