1、随机生成1万个整数,范围在0-10万之间,分别进行简单选择排序、快速排序(自行递归实现的)以及内置sort函数3种排序,打印出3种排序的运行时间。

假设有快速排序算法quick_sort(seq),可以实现快速排序。

令left_seq = [], right_seq = [] 令待排序序列区间的第一个元素为p,即p=seq[0] 对seq的[start+1,end]区间中的每一个元素: 如果元素 < p: 将该元素加入到left_seq中 否则: 将该元素加入到right_seq中

如left_seq非空,利用快速排序算法quick_sort,对left_seq进行快速排序
如right_seq非空,利用快速排序算法quick_sort,对right_seq进行快速排序

返回:left_seq + p + right_seq

In [25]:
import random
import time

def simple_sort(numbers):
    for i in range(len(numbers)):
        for j in range(i+1,len(numbers)):
            min=i
            if numbers[min]>numbers[j]:
                min=j
        numbers[i],numbers[min]=numbers[min],numbers[i]


def quick_sort(seq):
    left_seq=[]
    right_seq=[]
    p=seq[0]
    start=0
    end=len(seq)
    for i in range(start+1,end):
        if seq[i]<=p:
            left_seq.append(seq[i])
        else :
            right_seq.append(seq[i])
    if len(left_seq)!=0:
        quick_sort(left_seq)
    elif len(right_seq)!=0:
        quick_sort(right_seq)
    else :
        left_seq.append(p)
        left_seq.extend(right_seq)
        return (left_seq)



n=[]
for i in range(100000):
    n.append(random.randint(1,100000))
nums1=[]
nums2=[]
nums1.extend(n)
nums2.extend(n)


start_time=time.time()
quick_sort(nums2)
end_time=time.time()
print("time for quick-sort:",end_time-start_time,"-"*30)

start_time=time.time()
num=sorted(n)
end_time=time.time()
print("time for sort-function:",end_time-start_time,"-"*30)

#start_time=time.time()
#simple_sort(nums1)
#end_time=time.time()
#print("简单排序所用时间:",end_time-start_time,"-"*30)
print("too much time for the simple-sort! I have no patience for the outcome(facepalm) though I've coded it above.")


time for quick-sort: 0.0680234432220459 ------------------------------
time for sort-function: 0.059975624084472656 ------------------------------
too much time for the simple-sort! I have no patience for the outcome(facepalm) though I've coded it above.

2、随机生成1万个整数,范围在0-10万之间,求其中每个整数出现的次数。并按照整数大小排序输出整数及出现次数。


In [34]:
import random
from collections import Counter


def numbers_and_freq(numbers):
    list_needed=Counter()
    for i in range(len(numbers)):
        list_needed += Counter(i for i in numbers)
    return list_needed

n=[random.randint(0,100000) for i in range(100)]#test for list of lenth 100
the_list=numbers_and_freq(n)
print(the_list)


Counter({56872: 100, 97824: 100, 1759: 100, 62081: 100, 74962: 100, 79413: 100, 1075: 100, 24188: 100, 25795: 100, 45171: 100, 14959: 100, 1833: 100, 35102: 100, 72922: 100, 59435: 100, 9237: 100, 42951: 100, 92364: 100, 2557: 100, 38106: 100, 55860: 100, 23084: 100, 95520: 100, 77399: 100, 39866: 100, 45658: 100, 7736: 100, 5015: 100, 22571: 100, 41665: 100, 55163: 100, 46165: 100, 5152: 100, 64038: 100, 48933: 100, 37066: 100, 20106: 100, 51833: 100, 66452: 100, 74617: 100, 84349: 100, 74131: 100, 74733: 100, 24452: 100, 36233: 100, 93399: 100, 65218: 100, 54011: 100, 9589: 100, 55584: 100, 99185: 100, 36540: 100, 28442: 100, 67269: 100, 99044: 100, 93298: 100, 7879: 100, 79120: 100, 15056: 100, 96683: 100, 86337: 100, 45368: 100, 92776: 100, 29229: 100, 98128: 100, 611: 100, 85358: 100, 51338: 100, 73012: 100, 59958: 100, 39283: 100, 473: 100, 22390: 100, 7948: 100, 18128: 100, 75861: 100, 23480: 100, 53840: 100, 60757: 100, 22188: 100, 19840: 100, 82116: 100, 85408: 100, 24688: 100, 93769: 100, 38229: 100, 69046: 100, 33284: 100, 18421: 100, 23004: 100, 76906: 100, 59655: 100, 2759: 100, 40229: 100, 47478: 100, 58837: 100, 98467: 100, 52440: 100, 22602: 100, 22329: 100})

3、对本任务中的语料.txt文件,随机抽取其5001-10000行存为test1.txt文件,写函数,可得到其与本任务中test.txt文件的共用字以及独用字(相关概念自行百度)。


In [ ]:
from collections import defaultdict
import random

#制造test1.txt
def chose_lines(yuliao.txt):
    num_of_line=0
    randomline=[]
    with open(file) as fh:
        fhh=[line for line in fh.split("/")]
        num_of_line+=1
    for i in range(5001):
        randomline=[fhh[i] for i in random.randint(num_of_line)]
    return randomline


def practice3(file,file2):