算法图解——全书总结——1,2章

目录:1算法简介

2选择排序

3递归

4快速排序

5散列表

6广度优先搜索

7狄克斯特拉算法

8贪婪算法

9动态规划

10K临近算法

1算法简介

算法是计算方法,解决问题的过程。不同的算法会有不同的代价,结果。优秀的算法是在较小的代价下去实现较好的计算结果。

以查找为例:顺序查找,二分查找。

提出问题:随机给定一个1~100的数字。你对这个数字进行猜测,对方会告诉你猜大了,还是小了,还是对了。

顺序查找:

 

 显然这种算法是糟糕的,时间代价为O(n)。但这种查找代码非常简单,在问题规模不大时,也是非常有用的。

 二分查找:

在顺序表内查找一个值可以使用更快的二分查找:时间代价仅为O(log(n))。原理如下

 

 

每次猜中间的数字,如果猜中了就停止,如果没有就,根据反馈结果,大了,小了抛弃一半的数据。这样最多进行log2(n)次查找就能得出结果。

代码:

class BinarySearch():

  def search_iterative(self, list, item):
    # low and high keep track of which part of the list you'll search in.
    low = 0
    high = len(list) - 1

    # While you haven't narrowed it down to one element ...
    while low <= high:
      # ... check the middle element
      mid = (low + high) // 2
      guess = list[mid]
      # Found the item.
      if guess == item:
        return mid
      # The guess was too high.
      if guess > item:
        high = mid - 1
      # The guess was too low.
      else:
        low = mid + 1

    # Item doesn't exist
    return None

  def search_recursive(self, list, low, high, item):
    # Check base case 
    if high >= low: 
  
        mid = (high + low) // 2
        guess = list[mid]
  
        # If element is present at the middle itself 
        if guess == item:
            return mid 
  
        # If element is smaller than mid, then it can only 
        # be present in left subarray 
        elif guess > item: 
            return self.search_recursive(list, low, mid - 1, item) 
  
        # Else the element can only be present in right subarray 
        else: 
            return self.search_recursive(list, mid + 1, high, item) 
  
    else: 
        # Element is not present in the array 
        return None

if __name__ == "__main__":
  # We must initialize the class to use the methods of this class
  bs = BinarySearch()
  my_list = [1, 3, 5, 7, 9]
  
  print(bs.search_iterative(my_list, 3)) # => 1

  # 'None' means nil in Python. We use to indicate that the item wasn't found.
  print(bs.search_iterative(my_list, -1)) # => None

 

 

2选择排序。

当你需要对一堆无序的数据进行排序时,选择排序是一种简单的算法,它只需要O(n)额外储存空间,但运行时间为O(n * n)。

例如对下面的钢琴曲按播放量降序排列。

 每次选择未排序中最大的数字,将它与未排序的第一个数字交换。有

 

 经过N次后:可以完成排序。

 

这里有python代码

# Finds the smallest value in an array
def findSmallest(arr):
  # Stores the smallest value
  smallest = arr[0]
  # Stores the index of the smallest value
  smallest_index = 0
  for i in range(1, len(arr)):
    if arr[i] < smallest:
      smallest_index = i
      smallest = arr[i]      
  return smallest_index

# Sort array
def selectionSort(arr):
  newArr = []
  for i in range(len(arr)):
      # Finds the smallest element in the array and adds it to the new array
      smallest = findSmallest(arr)
      newArr.append(arr.pop(smallest))
  return newArr

print(selectionSort([5, 3, 6, 2, 10]))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值