有一个无序序列[37,99,73,48,47,40,40,25,99,51],先进行排序打印输出,分别尝试插入20/40/41 数值到序列中合适的位置,保证其有序。

1、for 循环实现

第一种实现,利用嵌套for循环,每次迭代出来的数值进行比较。如果比原表中的数值小,则插入到这个数左面。

lst1 = [37,99,73,48,47,40,40,25,99,51]

lst = sorted(lst1)



for x in (20,40,41,100):

    i = 0

    for i,v in enumerate(lst):

        if v > x:

            break

    lst.insert(i,x)

print(lst)

2、利用二分法实现

排序后二分查找到适当位置插入数值。

排序使用sorted解决,假设升序输出。

查找插入点,使用二分查找完成。

假设全长为n,首先在大致的中点元素开始和待插入的数进行比较,如果大则和右边的区域的中点元素进行比较,如果小则和左边的区域的中点进行比较,以此类推。

lst = [37, 99, 73, 48, 47, 40, 40, 25, 99, 51]

lst1 = sorted(lst)

def lst_insert(lst1,i):

    ret = lst1

    # print(ret)

    length = len(lst1)

    low = 0

    while low < length:

        mid = length // 2

        if ret[mid] < i:

            low = mid + 1       #说明i大,右边,限制下限。

        else:

            length = mid      #说明i不大于,左边,限制上限。

    ret.insert(low,i)

    print(ret)

for x in (40,20,21):

    lst_insert(lst1,x)

算法的核心就是折半至重合为止。

3、二分

1)二分的前提是有序,否则不允许。

2)二分的查找算法的时间复杂度是O(log n)

4、Bisect模块

提供的函数有:

bisect.bisect_left(a,x,lo=0,hi=len(a)):

查找在有序列表a中插入x的index。Lo和hi用于指定列表的区间,默认是使用整个列表,如果x已经存在,在其左边插入,返回值为index。

bisect.bisect_right(a,x,lo= 0,hi=len(a))或者bisect.bisect(a,x,lo= 0,hi=len(a))

和bisect_left类似,但如果x已经存在,则在其右边插入。

bisect.insort_left(a,x,lo= 0,hi=len(a))

在有序列表a中插入x,等同于a.insert(bisect.bisect_left(a,x,lo,hi),x).

bisect.insort_right(a,x,lo= 0,hi=len(a))或者bisect.insort(a,x,lo= 0,hi=len(a))和insort_left函数类似,但是如果x如果已经存在,在其右边插入。

函数可以分为两类:

Bisect系,用于查找index。

Insort系,用于实际插入。

默认重复时候从右边插入。

import bisect





lst = [37, 99, 73, 48, 47, 40, 40, 25, 99, 51]

lst1 = sorted(lst)

print(lst1)  #[25, 37, 40, 40, 47, 48, 51, 73, 99, 99]

print(20,bisect.bisect(lst1,20))    #20 0

print(30,bisect.bisect(lst1,30))    #30 1

print(40,bisect.bisect(lst1,40))    # 40 4

print(100,bisect.bisect(lst1,100))    #  100 10



print(20,bisect.bisect_left(lst1,20))   #20 0

print(30,bisect.bisect_left(lst1,30))   #30 1

print(40,bisect.bisect_left(lst1,40))   #40 2

print(100,bisect.bisect_left(lst1,100))   # 100 10



for x in (20,30,40,100):

    bisect.insort_left(lst1,x)

    print(lst1)

    # [20, 25, 37, 40, 40, 47, 48, 51, 73, 99, 99]

    # [20, 25, 30, 37, 40, 40, 47, 48, 51, 73, 99, 99]

    # [20, 25, 30, 37, 40, 40, 40, 47, 48, 51, 73, 99, 99]

    # [20, 25, 30, 37, 40, 40, 40, 47, 48, 51, 73, 99, 99, 100]

5、二分应用

判断学习成绩,成绩等级A-E  。90分以上为A ,80 以上为B  ,70以上为C  60以上为D。60以下为D。



def insert_score(nums):

    lst = [60, 70, 80, 90]

    grades = "EDCBA"

    index = bisect.bisect(lst,nums)

    print(index)

    print(grades[index])

insert_score(70)

二分法排序-Python实现的更多相关文章

  1. [No000087]Linq排序,SortedList排序,二分法排序性能比较

    using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...

  2. [PHP]基本排序(冒泡排序、快速排序、选择排序、插入排序、二分法排序)

    冒泡排序: function bubbleSort($array){ $len=count($array); //该层循环控制 需要冒泡的轮数 for($i=1;$i<$len;$i++){ / ...

  3. java简单的二分法排序

    二分法排序的思路:数据元素要按顺序排列,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功:若 x 小于当前位置值,则在数列的前半段中查找:若 x 大于当前位置值则在数列的后 ...

  4. js 排序:sort()方法、冒泡排序、二分法排序。

    js中的排序,这里介绍三种,sort()方法.冒泡排序.二分法排序. 1.sort方法 写法:  数组.sort(); 返回排好序的数组,如果数组里是数字,则由小到大,如果是字符串,就按照第一个字符的 ...

  5. 选择排序-Python与PHP实现版

    选择排序Python实现 import random # 生成待排序数组 a=[random.randint(1,999) for x in range(0,36)] # 选择排序 def selec ...

  6. javascript数组去重 String字符串去掉两端空格 javascript Array二分法排序 比较 javascript 求和

    通过原形添加方法: ==================数组去重(对象去重法)======================= Array.prototype.unique=function(){ va ...

  7. 计数排序与桶排序python实现

    计数排序与桶排序python实现 计数排序 计数排序原理: 找到给定序列的最小值与最大值 创建一个长度为最大值-最小值+1的数组,初始化都为0 然后遍历原序列,并为数组中索引为当前值-最小值的值+1 ...

  8. python内置数据类型-字典和列表的排序 python BIT sort——dict and list

    python中字典按键或键值排序(我转!)   一.字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序. Py ...

  9. 选择排序——Python实现

    选择排序: 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小( ...

随机推荐

  1. HDU - 4722 Good Numbers 【找规律 or 数位dp模板】

    If we sum up every digit of a number and the result can be exactly divided by 10, we say this number ...

  2. codeforce 849B

    B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. python 编码问题随笔

    原文点击这里 借用原作者的一句话"据说,每个做 Python 开发的都被字符编码的问题搞晕过,最常见的错误就是 UnicodeEncodeError.UnicodeDecodeError,你 ...

  4. HTML5 动效

    HTML5 动效 motion graphics toolbelt for the web https://github.com/xgqfrms/mojs A collection of loadin ...

  5. Docker In Action

    Docker In Action Docker 实战 https://docs.docker.com/get-started/overview/ Docker Engine Docker Archit ...

  6. CSS transition & shorthand property order

    CSS transition & shorthand property order shorthand property https://developer.mozilla.org/en-US ...

  7. TypeScript & global.d.ts

    TypeScript & global.d.ts https://www.typescriptlang.org/docs/handbook/declaration-files/template ...

  8. Flutter 创建dashboard页面

    1 import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends Sta ...

  9. 手把手教你gitlab汉化

    详细教程如下: 一.在Github上 https://gitlab.com/xhang/gitlab/-/tags 下载对应的版本到服务器中 这种-zh结尾的才是汉化包,下载速度可能比较慢,有条件的可 ...

  10. 边缘计算k8s集群之SuperEdge

    什么是边缘计算? 边缘计算,是指在靠近物或数据源头的一侧,采用网络.计算.存储.应用核心能力为一体的开放平台,就近提供最近端服务.其应用程序在边缘侧发起,产生更快的网络服务响应,满足行业在实时业务.应 ...