[从头学数学] 第245节 Python实现数据结构:排序

本文介绍了一本关于计算几何的书籍,并深入探讨了其中几种排序算法的实现,包括选择排序、归并排序和快速排序。通过具体代码示例,展示了这些算法的实际应用。
剧情提要:
阿伟看到了一本比较有趣的书,是关于《计算几何》的,2008年由北清派出版。很好奇
它里面讲了些什么,就来看看啦。


正剧开始:
星历2016年07月24日 11:03:16, 银河系厄尔斯星球中华帝国江南行省。

[工程师阿伟]正在和[机器小伟]一起研究[计算几何]]。






<span style="font-size:18px;">###
# @usage   排序类
# @author  mw
# @date    2016年07月24日  星期日  09:45:01 
# @param
# @return
#
###
class Sort():
    #选择排序法
    def select(self, seq, start):
        minIndex = start

        for j in range(start+1, len(seq)):
            if seq[minIndex] > seq[j]:
                minIndex = j

        return minIndex

    def selSort(self, seq):
        for i in range(len(seq)-1):
            minIndex = self.select(seq, i)
            tmp = seq[i]
            seq[i] = seq[minIndex]
            seq[minIndex] = tmp

        return seq;

    #归并排序
    def merge(self, seq, start, mid, stop):
        lst = []
        i = start
        j = mid

        # Merge the two lists while each has more elements
        while i < mid and j < stop:
            if seq[i] < seq[j]:
                lst.append(seq[i])
                i+=1
            else:
                lst.append(seq[j])
                j+=1

        # Copy in the rest of the start to mid sequence
        while i < mid:
            lst.append(seq[i])
            i+=1
            
        # Many merge sort implementations copy the rest
        # of the sequence from j to stop at this point.
        # This is not necessary since in the next part
        # of the code the same part of the sequence would
        # be copied right back to the same place.
        # while j < stop:
        # lst.append(seq[j])
        # j+=1
        # Copy the elements back to the original sequence
        for i in range(len(lst)):
            seq[start+i]=lst[i]

    def mergeSortRecursively(self, seq, start, stop):
        # We must use >= here only when the sequence we are sorting
        # is empty. Otherwise start == stop-1 in the base case.
        if start >= stop-1:
            return

        mid = (start + stop) // 2

        self.mergeSortRecursively(seq, start, mid)
        self.mergeSortRecursively(seq, mid, stop)
        self.merge(seq, start, mid, stop)

    def mergeSort(self, seq):
        self.mergeSortRecursively(seq, 0, len(seq))

        return seq;

    #快速排序法
    def partition(self, seq, start, stop):
        # pivotIndex comes from the start location in the list.
        pivotIndex = start
        pivot = seq[pivotIndex]
        i = start+1
        j = stop-1

        while i <= j:
            #while i <= j and seq[i] <= pivot:
            while i <= j and not pivot < seq[i]:
                i+=1
                
            #while i <= j and seq[j] > pivot:
            while i <= j and pivot < seq[j]:
                j-=1

            if i < j:
                tmp = seq[i]
                seq[i] = seq[j]
                seq[j] = tmp
                i+=1
                j-=1

        seq[pivotIndex] = seq[j]
        seq[j] = pivot

        return j

    def quicksortRecursively(self, seq, start, stop):
        if start >= stop-1:
            return

        # pivotIndex ends up in between the two halves
        # where the pivot value is in its final location.
        pivotIndex = self.partition(seq, start, stop)

        self.quicksortRecursively(seq, start, pivotIndex)
        self.quicksortRecursively(seq, pivotIndex+1, stop)

    def quickSort(self, seq):
        # randomize the sequence first
        # 为了选取种子更方便
        for i in range(len(seq)):
            j = random.randint(0,len(seq)-1)
            tmp = seq[i]
            seq[i] = seq[j]
            seq[j] = tmp

        self.quicksortRecursively(seq, 0, len(seq))

        return seq;
</span>


<span style="font-size:18px;">def main():
    sort = Sort();

    a = list(range(18));
    pyList = PyList(a);

    b = list(range(-10, 30, 3));
    pyList_2 = PyList(b);
    c = pyList+pyList_2;

    c.insert(5, 100);
    c.removeEmpty();
    c.info();

    #计时开始
    startTime = time.clock();
    c = sort.selSort(c);
    
    #计时结束
    endTime = time.clock();

    c.info();

    #打印结果
    print('操作用时:{0:.3e} s'.format(endTime-startTime));</span>


本节到此结束,欲知后事如何,请看下回分解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值