# -*- 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. 边框(Border) 和 轮廓(Outline) 属性

    border 复合属性.设置对象边框的特性. 标签定义及使用说明 如果上述值缺少一个没有关系,例如border:#FF0000;是允许的. 默认值: not specified 继承: no Java ...

  2. 在centOS上搭建wordpress博客系统

    一.主要内容 1.安装LAMP服务器系统(Linux.Apache.MySQL.PHP ); 2.安装wordpress: 二.具体步骤 一.LAMP环境设置 1.安装LAMP系统,在centOS上可 ...

  3. spring boot + mybatis + hikaricp + swagger2 + jasypt

    好久没写博客了记录下写过的东西,别到时候又忘了 文章前提:前面开发项目的时候数据池一直用的阿里的druid,这个数据池吧也不能说它不好,为什么现在想改成hikaricp数据池呢,完全是实用项目需要.d ...

  4. Okio 之初探黄龙

    Okio 是一个包装了 java.io 和 java.nio api 的库,以便可以更容易的访问.存储以及处理数据. ByteStrings 和 Buffers Okio 是围绕着两个容器类构建起来的 ...

  5. Cesium几个案例介绍

    前言 本文为大家介绍几个Cesium的Demo,通过这几个Demo能够对如何使用Cesium有进一步的了解,并能充分理解Cesium的强大之处和新功能.其他的无需多言,如果还不太了解什么是Cesium ...

  6. Best time to buy and sell stocks IV

    题目 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ Say you have an array for which ...

  7. uva--10700

    题意: 输入一串仅仅含有+和*号的表达式,能够通过加入括号来改变表达式的值,求表达式的最大最小值. 思路: 表达式中的数都是不大于20的正整数,由a*b+c<=a*(b+c)能够知道.先算乘法后 ...

  8. 25个增强iOS应用程序性能的提示和技巧(0基础篇)

    在开发iOS应用程序时,让程序具有良好的性能是非常关键的. 这也是用户所期望的,假设你的程序执行迟钝或缓慢,会招致用户的差评.然而因为iOS设备的局限性,有时候要想获得良好的性能,是非常困难的. 在开 ...

  9. UVA 11324 The Largest Clique(强连通分量+缩点DAG的DP)

    题意:给定一个有向图,求出一个最大的结点集,这个节点集中的随意两个点之间至少一个能到达还有一个点. 思路:假设一个点在这个节点集中,那么它所在的强连通分量中的点一定所有在这个节点集中,反之亦然, 求出 ...

  10. 新ITC提交APP常见问题与解决方法(Icon Alpha,Build version,AppIcon120x120)(2014-11-17)

    1)ICON无法上传.提示图片透明(有Alpha通道) 苹果如今不接受png里的Alpha了.提交的图标带有Alpha通道就提示: watermark/2/text/aHR0cDovL2Jsb2cuY ...