# -*- coding: utf-8 -*-
# @Date : 2017-08-19 20:19:56
# @Author : lileilei
'''那么算法和数据结构是什么呢,答曰兵法'''
'''a+b+c=1000 and a*a+b*b=c*c 求a,b,c'''
# import time
# start_time=time.time()
# for a in range(1000):#使用枚举法
# for b in range(1000):
# for c in range(1000):
# if a+b+c==1000 and a*a+b*b==c*c:
# print(a,b,c)
# print(time.time()-start_time)
# import time #方法2
# start_time=time.time()
# for a in range(1000):
# for b in range(1000):
# c=1000-a-b
# if a+b+c==1000 and a*a+b*b==c*c:
# print(a,b,c)
# print(time.time()-start_time)
class Stack(object):
"""栈"""
def __init__(self):
self.__items = []
def is_empty(self):
"""判断是否为空"""
return self.__items == []
def push(self, item):
"""加入元素"""
self.__items.append(item)
def pop(self):
"""弹出元素"""
return self.__items.pop()
def peek(self):
"""返回栈顶元素"""
return self.__items[len(self.__items)-1]
def size(self):
"""返回栈的大小"""
return len(self.__items)
# if __name__ == "__main__":
# stack = Stack()
# stack.push("hello")
# stack.push("world")
# stack.push("itcast")
# print (stack.size())
# print (stack.peek())
# print (stack.pop())
# print (stack.pop())
# print (stack.pop())
class Queue(object):
'''队列'''
def __init__(self):
self.__list=[]
def addqueue(slef,item):
#self.__list.append(item)
self.__list.insert(0,item)
def dequeue(self):
return self.__list.pop()
def is_empty(self):
return self.__list==[]
def size(self):
return len(self.__list)
class Dqueue(object):
'''双端队'''
def __init__(self):
self.__list=[]
def add_front(slef,item):
self.__list.insert(0,item)
def add_re(self,item):
self.__list.insert(item)
def dequeue(self):
return self.__list.pop()
def requeue(self):
return self.__list.pop(0)
def is_empty(self):
return self.__list==[]
def size(self):
return len(self.__list)
def buule_sor(alist):#冒泡
n=len(alist)
for i in range(n-1):
for j in range(n-1-i):
count=0
if alist[j]>alist[j+1]:
alist[j],alist[j+1]=alist[j+1],alist[j]
count+=1
if 0==count:
return
def select_sort(alist):#选择排序
n=len(alist)
for j in range(n-1):
min=j
for i in range(j+1,n):
if alist[min] > alist[i]:
min=i
alist[j],alist[min]=alist[min],alist[j]
def insert_sort(alist):'''插入排序'''
n=len(alist)
for j in range(1,n):
i=j
while i>0:
if alist[i]<alist[i-1]:
alist[i],alist[i-1]=alist[i-1],alist[i]
i-=1
def shell_sort(alist):'''希尔排序'''
n=len(alist)
gap=n//2
while gap>0:
for j in range(gap,n):
i=j
while i>0:
if alist[i]<alist[i-gap]:
alist[i],alist[i-gap]=alist[i-gap],alist[i]
i-=gap
else:
break
gap//=2
def quick_sort(alist,first,last):'''快速排序'''
if first>=last:
return
mid_value=alist[first]
low=first
high=last
while low<high:
while low <high and alist[high]>=mid_value:
high-=1
alist[low]=alist[high]
while low <high and alist[low]<mid_value:
low+=1
alist[high]=alist[low]
alist[low]=mid_value
quick_sort(alist,first,low-1)
quick_sort(alist,low+1,last)
def me_sort(alist):'''归并排序'''
n=len(alist)
if n<=1:
return alist
mid=n//2
left=me_sort(alist[:mid])
right=me_sort(alist[mid:])
left_point,right_porint=0,0
result=[]
while left_point<len(left) and right_porint<len(right):
if left[left_point] <right[right_porint]:
result.append(left[left_point])
left_point+=1
else:
result.append(right[right_porint])
right_porint+=1
result+=left[left_point:]
result+=right[right_porint:]
return result
def binary_search(alist,item):#二分查找 递归
n=len(alist)
if n>0:
mid=n//2
if alist[mid]==item:
return True
elif item<alist[mid]:
return binary_search(alist[:mid],item)
else:
return binary_search(alist[mid+1:],item)
return False
def brin_serce2(alist,item):#二分非递归
n=len(alist)
first=0
lasr=n-1
while first <=lasr:
mid = (first + lasr) // 2
if alist[mid]==item:
return True
elif item<alist[mid]:
lasr=mid-1
else:
first=mid+1
return False
if __name__ == '__main__':
listy=[54,67,76,23,34]
print(brin_serce2(listy,55))

python 算法学习部分代码记录篇章1的更多相关文章

  1. Python模块学习 ---- logging 日志记录

    许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net,c++中,有人们熟悉的log4cp ...

  2. python算法学习总结

    数据结构一维: 基础:数组array(string),链表Linked List 高级:栈stack,队列queue,双端队列deque,集合set,映射map(hash or map), etc二维 ...

  3. python爬虫学习之日志记录模块

    这次的代码就是一个日志记录模块,代码很容易懂,注释很详细,也不需要安装什么库.提供的功能是日志可以显示在屏幕上并且保存在日志文件中.调用的方式也很简单,测试代码里面有. 源代码: #encoding= ...

  4. python算法学习--待续

    几个算法网站 算法可视化网站:https://visualgo.net/en,通过动画展示算法实现过程 程序可视化网站:http://www.pythontutor.com/visualize.htm ...

  5. Python之路,Day21 - 常用算法学习

    Python之路,Day21 - 常用算法学习   本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...

  6. 利用python深度学习算法来绘图

    可以画画啊!可以画画啊!可以画画啊! 对,有趣的事情需要讲三遍. 事情是这样的,通过python的深度学习算法包去训练计算机模仿世界名画的风格,然后应用到另一幅画中,不多说直接上图! 这个是世界名画& ...

  7. 集成学习值Adaboost算法原理和代码小结(转载)

    在集成学习原理小结中,我们讲到了集成学习按照个体学习器之间是否存在依赖关系可以分为两类: 第一个是个体学习器之间存在强依赖关系: 另一类是个体学习器之间不存在强依赖关系. 前者的代表算法就是提升(bo ...

  8. 第四百一十五节,python常用排序算法学习

    第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...

  9. 小姐姐带你一起学:如何用Python实现7种机器学习算法(附代码)

    小姐姐带你一起学:如何用Python实现7种机器学习算法(附代码) Python 被称为是最接近 AI 的语言.最近一位名叫Anna-Lena Popkes的小姐姐在GitHub上分享了自己如何使用P ...

随机推荐

  1. Smarty基础用法

    一.Smarty基础用法: 1.基础用法如下 include './smarty/Smarty.class.php';//引入smarty类 $smarty = new Smarty();//实例化s ...

  2. P1092 虫食算

    题目传送:https://www.luogu.org/problem/show?pid=1092 #include <iostream> #include <cstring> ...

  3. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  4. ThinkPHP 5 中AJAX跨域请求头设置方法

    最近用thinkphp做项目,在测试环境时,存在接口的测试问题.在tp官网也没能找到相关的解决方法.自已看了一下源码,有如下的解决方案. 在项目目录下面,创建common/behavior/CronR ...

  5. 最全最详细:ubuntu16.04下内核编译以及设备驱动程序的编写(针对新手而写)

    写在前面:本博客为本人原创,转载请注明出处!同时,本博客严禁任何下载站随意抓取!!! 本博客唯一合法URL: 总体考虑 要去写设备驱动程序,说白了就三大步骤:下载内核源码构建内核源码树(也就是下载你的 ...

  6. java 之 组合模式(大话设计模式)

    代码是一门艺术,每次看完大话设计模式后都会有新的认识,有时会感叹原来还可以这样玩,相信大家都用过递归,递归的使用一般遍历文件夹等会常用到, 今天讲的设计模式类似于递归,也比较神奇,先看下类图,稍后再帮 ...

  7. Hbase 常用命令

    ################################################################# #author: 陈月白 #_blogs: http://www.c ...

  8. Struts2入门到放弃

    写在前面------------------------------------------------------------------------- 本文章主要从三个方面来学习Struts2框架 ...

  9. android 读取系统文件 wpa_supplicant

    1,须要权限 <uses-permission android:name="android.permission.ACCESS_SUPERUSER" /> 2,下载 R ...

  10. UVa 10170 - The Hotel with Infinite Rooms

    题目:求从s開始的递增序列(每次加1).求出他们加和不小于D的那个最后的加数. 分析:数学题.分治.s + s+1 + ... + n = n*(n+1)/2 - s*(s-1)/2 = (n+s)* ...