예제 #1
0
def main():
    # Create test array
    testArray = [random.randint(1, 100) for _ in range(20)]
    # measure the elapsed time for the algorithm
    start = time.time()
    merged = sort.merge(testArray)
    end = time.time()
    # print merge sort results
    print "Merge Sort: completed in: " + str(end - start) + " seconds"
    print testArray
    print str(merged) + '\n'
    # measure the elapsed time for the quick sort algorithm
    start = time.time()
    quickly = sort.quick(testArray)
    end = time.time()
    # print quick sort results
    print "Quick Sort: completed in: " + str(end - start) + " seconds"
    print testArray
    print str(quickly) + '\n'
    start = time.time()
    heaped = sort.heap(testArray)
    end = time.time()
    # print merge sort results
    print "Heap Sort: completed in: " + str(end - start) + " seconds"
    print testArray
    print str(heaped) + '\n'
예제 #2
0
 def test_heap(self):
     self.assertEqual(sort.heap([3, 7, 2, 1, 5, 4]), [1, 2, 3, 4, 5, 7])
예제 #3
0
        s = str(mes_deal).replace('u\'', '\'')
        #print "aaaaaaaaaaaaaaaaaaaaaaaaaa"
        if cnt % 10000 == 0:
            print "sentenses%d   %s" % (cnt, s.decode("unicode-escape"))
        insertMsg(rootNode, mes_deal, cnt)  # 插入字典树
        del mes_deal[:]
        cnt += 1
        mess = dir_file.readline()
        #if cnt == 50:
        #break

    print "===================================="
    mindeep = 5
    nowdeep = 1
    k = cnt / 3 + 1
    sortHeap = heap(k)
    #rootNode.Trans("")
    rootNode.heapSort("", mindeep, sortHeap, nowdeep)

    vis = [False] * cnt
    sortHeap.Trans(vis, [])
    notFound = []
    for i in range(0, cnt):
        if vis[i] == False:
            notFound.append(i)
    print "not found : %d" % (len(notFound))

    k_word.clear()
    del result[:]
    del temp_r[:]
    del check_sec[:]
예제 #4
0
def test_heap():
    a = random_array()
    expected = sorted(a)
    sort.heap(a)
    s = 'test_heap failed ({})'.format(a)
    assert str(expected) == str(a), s