码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法比较

时间:2018-10-19 13:54:33      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:指定   style   rand   nts   selected   数字   res   二分   常用   

一、生成测试数据,存入文件。

 

# -*- coding: UTF-8 -*-
import re
import math
import random
from random import randint
import time
import os
myListD = []


myListA6 = [(n+1) for n in range(0,int(1e6))]
myListB6 = [(int(1e6)-n) for n in range(0,int(1e6))]
myListC6 = [random.randint(1,int(1e6)) for n in range(0,int(1e6))]

myListA2 = [(n+1) for n in range(0,int(1e2))]
myListB2 = [(int(1e2)-n) for n in range(0,int(1e2))]
myListC2 = [random.randint(1,int(1e2)) for n in range(0,int(1e2))]

myListA3 = [(n+1) for n in range(0,int(1e3))]
myListB3 = [(int(1e3)-n) for n in range(0,int(1e3))]
myListC3 = [random.randint(1,int(1e3)) for n in range(0,int(1e3))]
myListD3 = [random.randint(1,int(1e8)) for n in range(0,int(1e3))]

myListA5 = [(n+1) for n in range(0,int(1e5))]
myListB5 = [(int(1e5)-n) for n in range(0,int(1e5))]
myListC5 = [random.randint(1,int(1e5)) for n in range(0,int(1e5))]
myListD5 = [random.randint(1,int(1e8)) for n in range(0,int(1e5))]

myListA4 = [(n+1) for n in range(0,int(1e4))]
myListB4 = [(int(1e4)-n) for n in range(0,int(1e4))]
myListC4 = [random.randint(1,int(1e4)) for n in range(0,int(1e4))]

# print(myListA)
# print()
# print(myListB)
# print()
# print(myListC)

# file1 = open("e:/py/1e6-123.json","w")
# file1.write(‘[‘+str(myListA[0]))
# file1.close
# file1 = open("e:/py/1e6-123.json","a")
# for s in range(1,len(myListA)):
#     file1.write(‘,‘+str(myListA[s]))
# file1.write(‘]‘)
# file1.close


# file=open(‘e:/py/1e6-321.json‘,‘w‘)
# file.write(str(myListB));
# file.close()

file1 = open("e:/py/1e6-123.json","w")
for s in range(len(myListA6)):
    file1.write(str(myListA6[s])+\n)
file1.close

file2 = open("e:/py/1e6-321.json","w")
for s in range(len(myListB6)):
    file2.write(str(myListB6[s])+\n)
file2.close

file3 = open("e:/py/1e6-231.json","w")
for s in range(len(myListC6)):
    file3.write(str(myListC6[s])+\n)
file3.close



file1 = open("e:/py/1e5-123.json","w")
for s in range(len(myListA5)):
    file1.write(str(myListA5[s])+\n)
file1.close

file2 = open("e:/py/1e5-321.json","w")
for s in range(len(myListB5)):
    file2.write(str(myListB5[s])+\n)
file2.close

file3 = open("e:/py/1e5-231.json","w")
for s in range(len(myListC5)):
    file3.write(str(myListC5[s])+\n)
file3.close

file4 = open("e:/py/1e5-2318.json","w")
for s in range(len(myListD5)):
    file4.write(str(myListD5[s])+\n)
file4.close


file1 = open("e:/py/1e4-123.json","w")
for s in range(len(myListA4)):
    file1.write(str(myListA4[s])+\n)
file1.close

file2 = open("e:/py/1e4-321.json","w")
for s in range(len(myListB4)):
    file2.write(str(myListB4[s])+\n)
file2.close

file3 = open("e:/py/1e4-231.json","w")
for s in range(len(myListC4)):
    file3.write(str(myListC4[s])+\n)
file3.close


file1 = open("e:/py/1e3-123.json","w")
for s in range(len(myListA3)):
    file1.write(str(myListA3[s])+\n)
file1.close

file2 = open("e:/py/1e3-321.json","w")
for s in range(len(myListB3)):
    file2.write(str(myListB3[s])+\n)
file2.close

file3 = open("e:/py/1e3-231.json","w")
for s in range(len(myListC3)):
    file3.write(str(myListC3[s])+\n)
file3.close

file4 = open("e:/py/1e3-2318.json","w")
for s in range(len(myListD3)):
    file4.write(str(myListD3[s])+\n)
file4.close



file1 = open("e:/py/1e2-123.json","w")
for s in range(len(myListA2)):
    file1.write(str(myListA2[s])+\n)
file1.close

file2 = open("e:/py/1e2-321.json","w")
for s in range(len(myListB2)):
    file2.write(str(myListB2[s])+\n)
file2.close

file3 = open("e:/py/1e2-231.json","w")
for s in range(len(myListC2)):
    file3.write(str(myListC2[s])+\n)
file3.close


# def loadDatadet2(infile,k):
#     f=open(infile,‘r‘)
#     sourceInLine=f.readlines()
#     dataset=[]
#     for line in sourceInLine:
#         temp1=line.strip(‘\n‘)
#         temp2=temp1.split(‘\t‘)
#         dataset.append(temp2)
#     for i in range(0,len(dataset)):
#         for j in range(k):
#             dataset[i].append(int(dataset[i][j]))
#         del(dataset[i][0:k])
#     return dataset

def loadDatadet(infile):
    f=open(infile,r)
    sourceInLine=f.readlines()
    dataset=[]
    for line in sourceInLine:
        temp1=line.strip(\n)
        #temp2=temp1.strip(‘\n‘)
        #temp2=temp1.split(‘,‘)
        dataset.append(int(temp1))
    f.close
    return dataset

infile=e:/py/1e6-123.json
myList=loadDatadet(infile)

# def loadDatadet3(infile):
#     f=open(infile,‘r‘)
#     sourceInLine=f.read()
#     dataset=[]
#     temp1=sourceInLine.replace("]","")
#     temp2=temp1.replace("[","")
#     temp3=temp2.split(‘,‘)
#     #temp3=sourceInLine.strip(‘[\n]‘)
#     dataset.append(temp3)
#     f.close
#     return dataset    


# infile=‘e:/py/1e6-123.json‘
# myList=loadDatadet(infile)

#readfile=loadDatadet(infile)

# print(‘dataset=‘,readfile)

print(完成!)





‘‘‘
rs = os.path.exists(‘e:/py/1e6-321.json‘)
if rs==True:
    print(‘已找到文件!‘)
    file=open(‘e:/py/1e6-321.json‘,‘r‘)
    lines= file.readlines()
    myListD = []
    for line in lines:
        temp = line.replace(‘\n‘,‘‘).split(‘\n‘)
        # del(temp[0])
        # del(temp[2])
        myListD.append(temp)
        print(myListD)


    # for contents in file.readlines():
    #     #contents=contents.strip(‘\n‘)
    #     #s+=contents
    #     myListD = contents.split(‘,‘)
        #
    
    # for s in contents:
    #     
    #     myListD.append(s)
    file.close
else:
    print(‘未找到文件‘)
print(myListD)
‘‘‘

   

二、导入文件数据,进行比较。

 

# -*- coding: UTF-8 -*-
import re
import math
import random
from random import randint
import time
import sys

sys.setrecursionlimit(100000) 
#QuickSort by Alvin
#
#
#制定测试数据
infile=e:/py/1e5-123.json

def chinese(data):
    count = 0
    for s in data:
        if ord(s) > 127:
            count += 1
    return count
#
def loadDatadet(infile):
    f=open(infile,r)
    sourceInLine=f.readlines()
    dataset=[]
    for line in sourceInLine:
        temp1=line.strip(\n)
        dataset.append(int(temp1))
    f.close
    return dataset



‘‘‘
一趟快速排序的算法是:
1)设置两个变量i、j,排序开始的时候:i=0,j=N-1;
2)以第一个数组元素作为关键数据,赋值给key,即key=A[0];
3)从j开始向前搜索,即由后开始向前搜索(j--),找到第一个小于key的值A[j],将A[j]和A[i]互换;
4)从i开始向后搜索,即由前开始向后搜索(i++),找到第一个大于key的A[i],将A[i]和A[j]互换;
5)重复第3、4步,直到i=j; (3,4步中,没找到符合条件的值,即3中A[j]不小于key,4中A[i]不大于key的时候改变j、i的值,使得j=j-1,i=i+1,直至找到为止。找到符合条件的值,进行交换的时候i, j指针位置不变。另外,i==j这一过程一定正好是i+或j-完成的时候,此时令循环结束)。
 
时间复杂度:O(nlgn)
‘‘‘

def random_quicksort(a,left,right):
    if(left<right):
        mid = random_partition(a,left,right)
        random_quicksort(a,left,mid-1)
        random_quicksort(a,mid+1,right)


def random_partition(a,left,right): 
    t = random.randint(left,right)     #生成[left,right]之间的一个随机数
    a[t],a[right] = a[right],a[t]    
    x = a[right]
    i = left-1                         #初始i指向一个空,保证0到i都小于等于 x
    for j in range(left,right):        #j用来寻找比x小的,找到就和i+1交换,保证i之前的都小于等于x
        if(a[j]<=x):
            i = i+1
            a[i],a[j] = a[j],a[i]
    a[i+1],a[right] = a[right],a[i+1]  #0到i 都小于等于x ,所以x的最终位置就是i+1
    return i+1

def QuickSort5(myList,start,end):
    #判断low是否小于high,如果为false,直接返回
    if start < end:
        i,j = start,end
        #设置基准数
        k=int((i+j)/2)
        base = myList[k]
        #base = myList[k]



        while i < j:
            #如果列表后边的数,比基准数大或相等,则前移一位直到有比基准数小的数出现
            while (i < j) and (myList[j] >= base):
                j = j - 1

            #如找到,则把第j个元素赋值给第i个元素i,此时表中i,j个元素相等
            myList[i] = myList[j]

            #同样的方式比较前半区
            while (i < j) and (myList[i] <= base):
                i = i + 1
            myList[j] = myList[i]
        #做完第一轮比较之后,列表被分成了两个半区,并且i=j,需要将这个数设置回base
        myList[i] = base

        #递归前后半区
        QuickSort5(myList, start, i - 1)
        QuickSort5(myList, j + 1, end)
    return myList

def QuickSort0(myList,start,end):
    #判断low是否小于high,如果为false,直接返回
    if start < end:
        i,j = start,end
        #设置基准数
        
        k=start
        
        base = myList[k]
        #base = myList[k]

        while i < j:
            #如果列表后边的数,比基准数大或相等,则前移一位直到有比基准数小的数出现
            while (i < j) and (myList[j] >= base):
                j = j - 1

            #如找到,则把第j个元素赋值给第i个元素i,此时表中i,j个元素相等
            myList[i] = myList[j]

            #同样的方式比较前半区
            while (i < j) and (myList[i] <= base):
                i = i + 1
            myList[j] = myList[i]
        #做完第一轮比较之后,列表被分成了两个半区,并且i=j,需要将这个数设置回base
        myList[i] = base

        #递归前后半区
        QuickSort0(myList, start, i - 1)
        QuickSort0(myList, j + 1, end)
    return myList


def quick_sort1(nums):
    if not nums:
        return []
    else:
        div=nums[0]
        left=quick_sort1([l for l in nums[1:] if l<=div])
        right=quick_sort1([r for r in nums[1:] if r>div])
        return left+[div]+right




def quick_sort2(nums):

    if not nums:
        return []
    else:
        lst=[nums[0],nums[-1],nums[int(len(nums)/2)]]
        div=sorted(lst)[1]
        index=nums.index(div)
        left=quick_sort2([l for l in (nums[:index]+nums[index+1:]) if l<=div])
        right=quick_sort2([r for r in (nums[:index]+nums[index+1:]) if r>div])
        return left+[div]+right

def qsort(L):
    if len(L) <= 1: return L
    return qsort([lt for lt in L[1:] if lt < L[0]]) + L[0:1]+     qsort([ge for ge in L[1:] if ge >= L[0]])

def InsertSort(myList):
    #获取列表长度
    length = len(myList)

    for i in range(1,length):
        #设置当前值前一个元素的标识
        j = i - 1
        
        #如果当前值小于前一个元素,则将当前值作为一个临时变量存储,将前一个元素后移一位
        if(myList[i] < myList[j]):
            temp = myList[i]
            myList[i] = myList[j]
            
            #继续往前寻找,如果有比临时变量大的数字,则后移一位,直到找到比临时变量小的元素或者达到列表第一个元素
            j = j-1
            while j>=0 and myList[j] > temp:
                myList[j+1] = myList[j]
                j = j-1

            #将临时变量赋值给合适位置
            myList[j+1] = temp

def BubbleSort(myList):
    #首先获取list的总长度,为之后的循环比较作准备
    length = len(myList)
    
    #一共进行几轮列表比较,一共是(length-1)轮
    for i in range(0,length-1):
        #每一轮的比较,注意range的变化,这里需要进行length-1-长的比较,注意-i的意义(可以减少比较已经排好序的元素)
        for j in range(0,length-1-i):
            
            #交换
            if myList[j] > myList[j+1]:
                tmp = myList[j]
                myList[j]=myList[j+1]
                myList[j+1] = tmp

def SelectedSort(myList):
    #获取list的长度
    length = len(myList)
    #一共进行多少轮比较
    for i in range(0,length-1):
        #默认设置最小值得index为当前值
        smallest = i
        #用当先最小index的值分别与后面的值进行比较,以便获取最小index
        for j in range(i+1,length):
            #如果找到比当前值小的index,则进行两值交换
            if myList[j]<myList[smallest]:
                tmp = myList[j]
                myList[j] = myList[smallest]
                myList[smallest]=tmp
‘‘‘
归并排序是建立在归并操作上的一种有效的排序算法,
该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。
将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,
再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。

归并过程为:比较a[i]和a[j]的大小,若a[i]≤a[j],
则将第一个有序表中的元素a[i]复制到r[k]中,并令i和k分别加上1;
否则将第二个有序表中的元素a[j]复制到r[k]中,并令j和k分别加上1,
如此循环下去,直到其中一个有序表取完,
然后再将另一个有序表中剩余的元素复制到r中从下标k到下标t的单元。
归并排序的算法我们通常用递归实现,先把待排序区间[s,t]以中点二分,
接着把左边子区间排序,再把右边子区间排序,
最后把左区间和右区间用一次归并操作合并成有序的区间[s,t]。
‘‘‘
def merge(left, right):
    i, j = 0, 0
    result = []
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1
    result += left[i:]
    result += right[j:]
    return result
 
def merge_sort(lists):
    # 归并排序
    if len(lists) <= 1:
        return lists
    num = int(len(lists) / 2)
    left = merge_sort(lists[:num])
    right = merge_sort(lists[num:])
    return merge(left, right)

‘‘‘
希尔排序(Shell Sort)是插入排序的一种。
也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本。
希尔排序是非稳定排序算法。该方法因DL.Shell于1959年提出而得名。 
希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;
随着增量逐渐减少,每组包含的关键词越来越多,
当增量减至1时,整个文件恰被分成一组,算法便终止。
‘‘‘
def shell_sort(lists):
    # 希尔排序
    count = len(lists)
    step = 2
    group = int(count/step)
    while group > 0:
        for i in range(0,group):
            j = i + group
            while j < count:
                k = j - group
                key = lists[j]
                while k >= 0:
                    if lists[k] > key:
                        lists[k + group] = lists[k]
                        lists[k] = key
                    k -= group
                j += group
        group = int(group/step)
    return lists

‘‘‘
堆排序
堆排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,
它是选择排序的一种。可以利用数组的特点快速定位指定索引的元素。
堆分为大根堆和小根堆,是完全二叉树。
大根堆的要求是每个节点的值都不大于其父节点的值,
即A[PARENT[i]] >= A[i]。
在数组的非降序排序中,需要使用的就是大根堆,
因为根据大根堆的要求可知,最大的值一定在堆顶。
‘‘‘
def adjust_heap(lists, i, size):
    lchild = 2 * i + 1
    rchild = 2 * i + 2
    max = i
    if i < size / 2:
        if lchild < size and lists[lchild] > lists[max]:
            max = lchild
        if rchild < size and lists[rchild] > lists[max]:
            max = rchild
        if max != i:
            lists[max], lists[i] = lists[i], lists[max]
            adjust_heap(lists, max, size)
 
def build_heap(lists, size):
    for i in range(0, int(size/2))[::-1]:
        adjust_heap(lists, i, size)
 
def heap_sort(lists):
    size = len(lists)
    build_heap(lists, size)
    for i in range(0, size)[::-1]:
        lists[0], lists[i] = lists[i], lists[0]
        adjust_heap(lists, 0, i)

‘‘‘
基数排序
基数排序(radix sort)属于“分配式排序”(distribution sort),
又称“桶子法”(bucket sort)或bin sort,顾名思义,
它是透过键值的部份资讯,将要排序的元素分配至某些“桶”中,
藉以达到排序的作用,基数排序法是属于稳定性的排序,
其时间复杂度为O (nlog(r)m),其中r为所采取的基数,
而m为堆数,在某些时候,基数排序法的效率高于其它的稳定性排序法。
‘‘‘

#def radix_sort(lists, radix=10):
#     k = int(math.ceil(math.log(max(lists), radix)))
#     bucket = [[] for i in range(radix)]
#     for i in range(1, k+1):
#         for j in lists:
#             bucket[int(int(j/(radix**(i-1))) % int(radix**i))].append(j)
#         del lists[:]
#         for z in bucket:
#             lists += z
#             del z[:]
#     return lists

def RadixSort(list,d):    
    for k in range(d):#d轮排序
        s=[[] for i in range(10)]#因为每一位数字都是0~9,故建立10个桶
        ‘‘‘对于数组中的元素,首先按照最低有效数字进行
           排序,然后由低位向高位进行。‘‘‘
        for i in list:
            ‘‘‘对于3个元素的数组[977, 87, 960],第一轮排序首先按照个位数字相同的
               放在一个桶s[7]=[977],s[7]=[977,87],s[0]=[960]
               执行后list=[960,977,87].第二轮按照十位数,s[6]=[960],s[7]=[977]
               s[8]=[87],执行后list=[960,977,87].第三轮按照百位,s[9]=[960]
               s[9]=[960,977],s[0]=87,执行后list=[87,960,977],结束。‘‘‘
            s[int(i/(10**k)%10)].append(i) #977/10=97(小数舍去),87/100=0
        list=[j for i in s for j in i]
    return list

# def quick_sort(nums):
#     global k1
    
#     if not nums:
#         return []
#     elif k1<500:
#         div=nums[0]
#         k1=k1+1
#         left=quick_sort([l for l in nums[1:] if l<=div])
#         right=quick_sort([r for r in nums[1:] if r>div])
#         return left+[div]+right
        
#     else:
#         #print(nums)
#         print(‘转为基数:‘)
#         nums=RadixSort(nums,7)
#         return nums

def quick_sortD(array, l, r):
    if l >= r:
        return
    stack = []
    stack.append(l)
    stack.append(r)
    while stack:
        low = stack.pop(0)
        high = stack.pop(0)
        if high - low <= 0:
            continue
        x = array[high]
        i = low - 1
        for j in range(low, high):
            if array[j] <= x:
                i += 1
                array[i], array[j] = array[j], array[i]
        array[i + 1], array[high] = array[high], array[i + 1]
        stack.extend([low, i, i + 2, high])

#导论
def quick_sortL(array, l, r):
    if l < r:
        q = partition(array, l, r)
        #print(q)
        quick_sortL(array, l, q - 1)
        quick_sortL(array, q + 1, r)
#导论划分
def partition(array, l, r):
    x = array[r]
    i = l - 1
    for j in range(l, r):
        if array[j] <= x:
            i += 1
            array[i], array[j] = array[j], array[i]
    array[i + 1], array[r] = array[r], array[i+1]
    #print(array)
    return i + 1

quick_sort = lambda array: array if len(array) <= 1 else quick_sort([item for item in array[1:] if item <= array[0]]) + [array[0]] + quick_sort([item for item in array[1:] if item > array[0]])
    


#myList = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]

#myList = [49,38,65,1,5,2,3,85,86,87,100,105,102,8,7,6,108,103,97,76,13,27,49,81,82,83]


‘‘‘
myList =5[n for n in range(0,int(1e3))]
for m in range(int(1e4)):
    myList[m]=random.randint(1,int(1e6))
‘‘‘
# myList0 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList1 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList2 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList3 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList4 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList5 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList6 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList7 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList8 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myList9 = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListA = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListB = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListC = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListD = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListE = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListF = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListG = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListH = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListI = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListJ = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListK = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListL = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListM = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListN = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListO = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListP = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListQ = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListR = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListS = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListT = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListU = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListV = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListW = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListX = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListY = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]
# myListZ = [random.randint(1,int(1e5)) for n in range(0,int(1e6))]






‘‘‘
myList2 = myList1.copy
myList3 = myList1.copy
myList4 = myList1.copy
myList5 = myList1.copy
‘‘‘

# nums1=[i+1 for i in range(int(1e4))]
# nums2=[random.randint(0,int(1e4)) for i in range(int(1e3))]

#myList2 = "".join(str(myList))
#print(‘排序前:{:<5} ‘.format(myList2))
‘‘‘
print(‘排序前:‘)
i=0
for s1 in myList:
    i=i+1
    print(‘{:*>10} ‘.format(str(s1)),end=‘‘)
    if(i%20==0):
        print()
print()

print()
‘‘‘
#print("Quick 0 Sort:")
#QuickSort0(myList,0,len(myList)-1)

#print("Quick Middle Sort:")
#QuickSort0(myList,0,len(myList)-1)

#print("Select Sort:")
#SelectedSort(myList)
#
#print("Insert Sort:")
#InsertSort(myList)
#
#print("Bubble Sort:")
#BubbleSort(myList)
#



myList=[]
myList=loadDatadet(infile)

print({:>48}\t{:>10}\t{:>10}.format(原始首尾数:,myList[0],myList[-1]))

stime=time.time()
myList.sort(reverse = False)
etime=time.time()

tishi=内置排序 sort 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(  首尾数:,myList[0],myList[-1]))



myList=[]
myList=loadDatadet(infile)
stime=time.time()
myList1=sorted(myList)
etime=time.time()

tishi=内置排序 sorted 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(1 首尾数:,myList1[0],myList1[-1]))




myList=[]
myList=loadDatadet(infile)
r=len(myList)-1
stime=time.time()
#k = random.randint(int(len(myList)*0.3),int(len(myList)*0.6))
#k = 0
random_quicksort(myList,0,r)
etime=time.time()

tishi=快速排序 随机 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(  首尾数:,myList[0],myList[-1]))
#print(myList2)


#random_quicksort(a,0,len(a)-1)


myList=[]
myList=loadDatadet(infile)
stime=time.time()
random.shuffle(myList)
QuickSort0(myList,0,len(myList)-1)
etime=time.time()

tishi=快速排序 首位 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(  首尾数:,myList[0],myList[-1]))



myList=[]
myList=loadDatadet(infile)
stime=time.time()
random.shuffle(myList)
QuickSort5(myList,0,len(myList)-1)
etime=time.time()
#

tishi=快速排序 中位 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(  首尾数:,myList[0],myList[-1]))


myList=[]
myList=loadDatadet(infile)

stime=time.time()
#k = random.randint(int(len(myList)*0.3),int(len(myList)*0.6))
#k = 0
random.shuffle(myList)
myList2=quick_sort1(myList)
etime=time.time()

tishi=快速排序 简1 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(2 首尾数:,myList2[0],myList2[-1]))
#print(myList2)



myList=[]
myList=loadDatadet(infile)
stime=time.time()
random.shuffle(myList)
myList3=quick_sort2(myList)
etime=time.time()

tishi=快速排序 简2 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(3 首尾数:,myList3[0],myList3[-1]))



myList=[]
myList=loadDatadet(infile)
stime=time.time()
random.shuffle(myList)
myList4=qsort(myList)
etime=time.time()

tishi=快速排序 3行 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(4 首尾数:,myList4[0],myList4[-1]))



myList=[]
myList=loadDatadet(infile)
r=len(myList)-1
stime=time.time()
#k = random.randint(int(len(myList)*0.3),int(len(myList)*0.6))
#k = 0
random.shuffle(myList)
#print(myList)
quick_sortL(myList,0,r)
etime=time.time()

tishi=快速排序 导论 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(  首尾数:,myList[0],myList[-1]))
#print(myList2)




myList=[]
myList=loadDatadet(infile)
stime=time.time()
myList5=RadixSort(myList,10)
etime=time.time()

tishi=基数排序 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(5 首尾数:,myList5[0],myList5[-1]))



myList=[]
myList=loadDatadet(infile)
stime=time.time()
myList6=merge_sort(myList)
etime=time.time()

tishi=归并排序 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(6 首尾数:,myList6[0],myList6[-1]))



myList=[]
myList=loadDatadet(infile)
stime=time.time()
heap_sort(myList)
etime=time.time()

tishi=堆排序 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(C 首尾数:,myList[0],myList[-1]))






myList=[]
myList=loadDatadet(infile)
stime=time.time()
InsertSort(myList)
etime=time.time()

tishi=插入排序 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(7 首尾数:,myList[0],myList[-1]))



myList=[]
myList=loadDatadet(infile)
stime=time.time()
SelectedSort(myList)
etime=time.time()

tishi=选择排序 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(8 首尾数:,myList[0],myList[-1]))



myList=[]
myList=loadDatadet(infile)
stime=time.time()
BubbleSort(myList)
etime=time.time()

tishi=冒泡排序 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(9 首尾数:,myList[0],myList[-1]))



myList=[]
myList=loadDatadet(infile)
stime=time.time()
shell_sort(myList)
etime=time.time()

tishi=希尔排序 用时:
l_tishi = chinese(tishi)
print({0:>{number}}{1:>15.6f}.format(tishi,etime-stime,number=25-l_tishi),end=‘‘)
print({:>10}\t{:>10}\t{:>10}.format(B 首尾数:,myList[0],myList[-1]))




‘‘‘
start=time.time()
quick_sort2(nums1)
end=time.time()
print ‘quick_sort2 time.time: %fs‘ % (end-start)

QuickSort0(myList,0,len(myList)-1)

print()

#myList2 = ‘,‘.join([str(s) for s in myList] )
#myList2 = "".join(str(myList))


print(‘排序后:‘)
i=0
for s1 in myList:
    i=i+1
    print(‘{:>10} ‘.format(str(s1)),end=‘‘)
    if(i%20==0):
        print()
print()
print()


#print(‘排序后:{:<5} ‘.format(myList2))

‘‘‘
# print(myList7[0])
# print(myList7[-1])
# print(myList7[int(len(myList7)/2)])
# lst=[myList7[0],myList7[-1],myList7[int(len(myList7)/2)]]
# div=sorted(lst)[1]
# index=myList7.index(div)
# print(div)
# print(index)
# print(myList7[int(len(myList7)-1)])

 

排序算法比较

标签:指定   style   rand   nts   selected   数字   res   二分   常用   

原文地址:https://www.cnblogs.com/pscc/p/9815530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!