二分法、选择排序、快速排序

最近看了《算法图解》,觉得很棒,做一些总结记录。

# -*- coding: utf-8 -*-
import random
__author__ = "chenk"

class Algorithme:
    """"""
    def __init__(self):
        self.low = 0
        self.high = 10000

    def get_random_num(self):
        """Return a random num. The num is between 0 and 10000."""
        return random.randint(self.low, self.high)

    def get_list(self, num):
        """Returns an array of the number of executions."""
        num_list = list()
        while num:
            num_list.append(self.get_random_num())
            num -= 1
        return num_list

    def dichotomy(self):
        """二分法猜数字: A给出一个整数num(0<num<10000), 让用户B猜, 问B至少要几次才能猜到?
        二分法程序执行时间:O(log n), log以2为底。"""
        low = self.low
        high = self.high
        # 随机生成一个数
        num = self.get_random_num()
        print("The random num is %d." % num)
        # 每次猜中间数
        mid = (low + high) // 2
        count = 0
        while True:
            count += 1
            if mid > num:
                high = mid
            elif mid < num:
                low = mid
            else:
                print("You got it. It takes %d times." % count)
                break
            mid = (low + high) // 2

    def get_max_num(self, array):
        """From the array get the max num."""
        max_num = 0
        for num in array:
            if num > max_num:
                max_num = num
        return max_num

    def selection_sort(self):
        """选择排序(从大到小):每次从数组中取最大的值,循环 n/2 次。
        选择排序程序执行时间:O(n^2)"""
        num_list = self.get_list(20)
        sorted_list = list()
        print("选择排序目标数组:\n", num_list)
        for i in range(len(num_list)):
            max_num = self.get_max_num(num_list)
            sorted_list.append(max_num)
            num_list.remove(max_num)
        return sorted_list

    def quicksort(self, array):
        """快速排序(从大到小).
         快速排序程序执行时间:n*O(log n)"""
        if len(array) < 2:
            return array
        else:
            pivot = array[0]
            left = [i for i in array[1:] if i >= pivot]
            right = [i for i in array[1:] if i < pivot]
            return self.quicksort(left) + [pivot] + self.quicksort(right)

if __name__ == "__main__":
    algorithme = Algorithme()
    for i in range(10):
        algorithme.dichotomy()

    sorted_list = algorithme.selection_sort()
    print("排序后的值:\n", sorted_list)
    quicksort_list = algorithme.get_list(10)
    print("快速排序目标数组:\n", quicksort_list)
    quicksort = algorithme.quicksort(quicksort_list)
    print("排序后的值:\n", quicksort)

上述执行代码的结果为:

The random num is 3432.
You got it. It takes 11 times.
The random num is 9185.
You got it. It takes 12 times.
The random num is 8764.
You got it. It takes 11 times.
The random num is 6344.
You got it. It takes 12 times.
The random num is 667.
You got it. It takes 13 times.
The random num is 5982.
You got it. It takes 12 times.
The random num is 7408.
You got it. It takes 12 times.
The random num is 450.
You got it. It takes 12 times.
The random num is 2013.
You got it. It takes 12 times.
The random num is 5428.
You got it. It takes 14 times.
选择排序目标数组:
 [42, 607, 2544, 3854, 4845, 9129, 1208, 1286, 7588, 9414, 6669, 3927, 8941, 6775, 9866, 2526, 9410, 9463, 1048, 2010]
排序后的值:
 [9866, 9463, 9414, 9410, 9129, 8941, 7588, 6775, 6669, 4845, 3927, 3854, 2544, 2526, 2010, 1286, 1208, 1048, 607, 42]
快速排序目标数组:
 [4563, 6788, 628, 4224, 4286, 4563, 4503, 9094, 5353, 8843]
排序后的值:
 [9094, 8843, 6788, 5353, 4563, 4563, 4503, 4286, 4224, 628]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值