排序

参考:https://github.com/wangzheng0822/algo/tree/master/python

归并排序

def merge_sort(a):_merge_sort_between(a, 0, len(a) - 1)def _merge_sort_between(a, low, high):# The indices are inclusive for both low and high.if low < high:mid = low + (high - low) // 2_merge_sort_between(a, low, mid)_merge_sort_between(a, mid + 1, high)_merge(a, low, mid, high)def _merge(a, low, mid, high):# a[low:mid], a[mid+1, high] are sorted.i, j = low, mid + 1tmp = []while i <= mid and j <= high:if a[i] <= a[j]:tmp.append(a[i])i += 1else:tmp.append(a[j])j += 1start = i if i <= mid else jend = mid if i <= mid else hightmp.extend(a[start:end + 1])a[low:high + 1] = tmpif __name__ == "__main__":a1 = [3, 5, 6, 7, 8]a2 = [2, 2, 2, 2]a3 = [4, 3, 2, 1]a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]merge_sort(a1)print(a1)merge_sort(a2)print(a2)merge_sort(a3)print(a3)merge_sort(a4)print(a4)

快速排序

import randomdef quick_sort(a):_quick_sort_between(a, 0, len(a) - 1)def _quick_sort_between(a, low, high):if low < high:# get a random position as the pivotk = random.randint(low, high)a[low], a[k] = a[k], a[low]m = _partition(a, low, high)  # a[m] is in final position_quick_sort_between(a, low, m - 1)_quick_sort_between(a, m + 1, high)def _partition(a, low, high):pivot, j = a[low], lowfor i in range(low + 1, high + 1):if a[i] <= pivot:j += 1a[j], a[i] = a[i], a[j]  # swapa[low], a[j] = a[j], a[low]return jif __name__ == "__main__":a1 = [3, 5, 6, 7, 8]a2 = [2, 2, 2, 2]a3 = [4, 3, 2, 1]a4 = [5, -1, 9, 3, 7, 8, 3, -2, 9]quick_sort(a1)print(a1)quick_sort(a2)print(a2)quick_sort(a3)print(a3)quick_sort(a4)print(a4)

插入排序

def insertion_sort(a: List[int]):length = len(a)if length <= 1:returnfor i in range(1, length):value = a[i]j = i - 1while j >= 0 and a[j] > value:a[j + 1] = a[j]j -= 1a[j + 1] = valueif __name__ == "__main__":array = [5, 6, -1, 4, 2, 8, 10, 7, 6]insertion_sort(array)print(array)

冒泡排序

def bubble_sort(a):length = len(a)if length <= 1:returnfor i in range(length):made_swap = Falsefor j in range(length - i - 1):if a[j] > a[j + 1]:a[j], a[j + 1] = a[j + 1], a[j]made_swap = Trueif not made_swap:breakif __name__ == "__main__":array = [5, 6, -1, 4, 2, 8, 10, 7, 6]bubble_sort(array)print(array)

选择排序

def selection_sort(a):length = len(a)if length <= 1:returnfor i in range(length):min_index = imin_val = a[i]for j in range(i, length):if a[j] < min_val:min_val = a[j]min_index = ja[i], a[min_index] = a[min_index], a[i]if __name__ == "__main__":array = [5, 6, -1, 4, 2, 8, 10, 7, 6]selection_sort(array)print(array)

堆排序

参考:https://www.baidu.com/link?url=swWPSFhb2Tcv40_9d2r024RXj9A9uyP56vjX6Wf05qjMNbPKQGC6kb51K8SZhdWIOLapT_m0Ht8m9OanVH2-h3WoE4ypbVOapf7ZRpSrUh_&wd=&eqid=b7e1f53b0013fbe1000000065cdf9f99

https://www.jianshu.com/p/d174f1862601

https://www.cnblogs.com/bingabcd/p/7425039.html

def HeapSort(input_list):#调整parent结点为大根堆def HeapAdjust(input_list,parent,length):temp = input_list[parent]child = 2*parent+1while child < length:if child+1 <length and input_list[child] < input_list[child+1]:child +=1if temp > input_list[child]:breakinput_list[parent] = input_list[child]parent = childchild = 2*child+1input_list[parent] = tempif input_list == []:return []sorted_list = input_listlength = len(sorted_list)#最后一个结点的下标为length//2-1#建立初始大根堆for i in range(0,length // 2 )[::-1]:HeapAdjust(sorted_list,i,length)for j in range(1,length)[::-1]:#把堆顶元素即第一大的元素与最后一个元素互换位置temp = sorted_list[j]sorted_list[j] = sorted_list[0]sorted_list[0] = temp#换完位置之后将剩余的元素重新调整成大根堆HeapAdjust(sorted_list,0,j)print('%dth' % (length - j))print(sorted_list)return sorted_listif __name__ == '__main__':input_list = [50,123,543,187,49,30,0,2,11,100]print("input_list:")print(input_list)sorted_list = HeapSort(input_list)print("sorted_list:")print(input_list)

leetcode-239

参考:https://blog.csdn.net/weixin_41871126/article/details/85846113

class Solution(object):def maxSlidingWindow(self, nums, k):""":type nums: List[int]:type k: int:rtype: List[int]"""if not nums:return []res,window = [],[]for i,j in enumerate(nums):if i >= k and window[0] <= i - k:window.pop(0)while window and nums[window[-1]] <= j:window.pop()window.append(i)if i >= k - 1:res.append(nums[window[0]])return res

实现 O(n) 时间复杂度内找到一组数据的第 K 大元素

参考:https://blog.csdn.net/u011519550/article/details/89417311

#给定一个无序列表,求出第K大的元素,要求复杂度O(n)
def find_k(test_list,k):flag=test_list[0]test_list.pop(0)l_list=[i for i in test_list if i < flag]r_list=[i for i in test_list if i >= flag]#结果递归的基线条件if len(r_list)==k-1:return flagelif len(r_list)>k-1:return find_k(r_list,k)else:#因为test_list.pop(0)让test_list少了一个元素,所以下面需要+1gap=len(test_list)-len(l_list)+1k=k-gapreturn find_k(l_list,k)if __name__ == '__main__':test_list = [5, 4, 3, 2, 1,10,20,100]res=find_k(test_list,1)print(res)

二分查找

实现一个有序数组的二分查找算法

参考:https://blog.csdn.net/qq_20207459/article/details/79630382

print("有序数组中的二分查找")
key=int(input("请输入您要查找的整数:"))
c=[10,11,12,17,19,21,22,24,32,38,49,51,66,78,90]
def BinarySearch(key,c):lo,hi= 0,len(c)-1while lo<=hi:mid = int(lo+(hi-lo)/2)if key<c[mid]:hi = mid-1elif key>c[mid]:lo = mid+1else:return print("%s在数组中的索引为%s"%(key,mid))return print("%s不在该数组中"%key)
BinarySearch(key,c)

实现模糊二分查找算法(比如大于等于给定值的第一个元素)

参考:https://blog.csdn.net/m0_38019841/article/details/88235655#%E6%A8%A1%E7%B3%8A%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE%3A

#模糊二分[大于等于给定值的第一个元素的位置]
def fuzzyHalfSearch(nums,num):if not nums:return if num>nums[-1]:returnif num <= nums[0]:return 0elif num == nums[-1]:return len(nums)-1low = 0high = len(nums)-2while low <= high:medim = (high+low)/2if num>nums[medim] and num<=nums[medim+1]:return medim+1elif num > nums[medim+1]:low = medim+1elif num <= nums[medim]:high = medimreturn

【数据结构与算法】task3 排序二分查找相关推荐

  1. 常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构)

    常见数据结构和算法实现(排序/查找/数组/链表/栈/队列/树/递归/海量数据处理/图/位图/Java版数据结构) 数据结构和算法作为程序员的基本功,一定得稳扎稳打的学习,我们常见的框架底层就是各类数据 ...

  2. 51Nod-1090 3个数和为0【排序+二分查找】

    1090 3个数和为0 基准时间限制:1秒 空间限制:131072KB 分值:5难度:1级算法题 给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从中找出所有和 = 0的3 ...

  3. 在Object-C中学习数据结构与算法之排序算法

    笔者在学习数据结构与算法时,尝试着将排序算法以动画的形式呈现出来更加方便理解记忆,本文配合Demo 在Object-C中学习数据结构与算法之排序算法阅读更佳. 目录 选择排序 冒泡排序 插入排序 快速 ...

  4. 数据结构排序算法实验报告_[数据结构与算法系列]排序算法(二)

    我的上一篇文章向大家介绍了排序算法中的冒泡排序.插入排序和选择排序.它们都是平均时间复杂度为 O(n^2) 的排序算法,同时还为大家讲解了什么是原地排序和什么是排序的稳定性.下图是这三种算法的比较,不 ...

  5. 数据结构和算法系列13 五大查找之哈希查找

    原文地址 http://www.cnblogs.com/mcgrady/p/3294871.html 数据结构和算法系列13 五大查找之哈希查找 这一篇要总结的是五天查找的最后一篇,哈希查找,也称为散 ...

  6. 数据结构与算法(三) 排序算法(代码示例)

    数据结构与算法三 排序算法 1. 选择排序 2. 插入排序 3. 冒泡排序 4. 归并排序 5. 快速排序 6. 希尔排序 7. 堆排序 总结 1. 选择排序 选择排序的基本原理: 对于未排序的一组记 ...

  7. 数据结构与算法之排序算法

    数据结构与算法之排序算法 排序算法的介绍 ​ 排序也称排序算法(Sort Algorithm),排序是将一组数据,依指定的顺序进行排序的过程. 排序的分类 1)内部排序:指将需要处理的数据都加载到内部 ...

  8. 数据结构/算法笔记(1)-两种排序 二分查找

    快速排序 确定分界点:在数组中选一个元素的值 x x x 作为分界,(某人说)可任取 (重点)调整区间:以 x x x 为准,将数组分为左右两段,通过换位保证左边都≤x,右边都≥x,即可 递归分别处理 ...

  9. [算法]LeetCode 专题 -- 二分查找专题 34. 在排序数组中查找元素的第一个和最后一个位置

    LeetCode 专题 – 二分查找专题 34. 在排序数组中查找元素的第一个和最后一个位置 难度:中等 题目描述 给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值 ...

最新文章

  1. C# 之 用NPOI类库操作Excel
  2. linux+网络根文件系统,认识Linux根文件系统结构
  3. NetBeans IDE中运行当前文件快捷键
  4. 华为交换机不同网段互访_华为S5700系列交换机使用高级ACL限制不同网段的用户互访...
  5. CM3计算板EC20模组拨号上网
  6. LINUX编译Android doubango
  7. 将强化学习应用到量化投资中实战篇(神经网络模块开发)
  8. Activiti7整合SpringBoot
  9. doesn‘t work properly without JavaScript enabled. Please enable it to continue 的原因之一
  10. Deecamp20 项目提交【如何用pcdet(second)跑自己的数据】
  11. h5页面 请在微信客户端打开链接_模拟微信接口时,提示“请在微信客户端打开链接”(转)...
  12. html规范eml文件,eml文件【操作办法】
  13. word教程之word2007和2010版本查找和替换快捷键介绍
  14. artDialog对话框组件使用方法
  15. java 短信_java实现发送手机短信
  16. jmeter 接口测试 签名_Jmeter之API接口签名验证测试
  17. CGAN之条件生成对抗网络(Matlab)
  18. java String工具类/字符串工具类 StringUtil
  19. 解决Linux无法创建新用户和/home目录下无法创建新目录的问题,或者无权限创建用户目录问题mkdir: cannot create directory ‘ ’: Permissi
  20. Redis 位图数据结构介绍

热门文章

  1. 单片机STM32的5个时钟源知识,你不能错过。
  2. 最近发现一个很全面的历史网站,包含世界各国各个时代内容
  3. spyder运行时闪退解决办法:
  4. 留言管理系统的设计与实现
  5. getElementsByName、getElementById的简单用法
  6. ubuntu22.04 安装优化(主题,软件,换源,插件扩展)
  7. 越来越稳!Kubernetes 1.8.0 版本发布
  8. 在mysql中创建和调试存储过程
  9. 网络嗅探 精华版(全)
  10. 计算机科学与物流工程国际学术会议,第一届通信工程与物流管理国际会议