zoukankan      html  css  js  c++  java
  • list 冒泡排序 --for in --变量交换

    #   查找  排序
    
    k1 = [11,22,1, 2, 3, 4, 5, 6, 7, 8, 9, ]
    k1.sorted()      # 排序改变
    k1[1] , k1[2] = k1[2] ,k1[1]
    print(k1)  
    #    [11, 1, 22, 2, 3, 4, 5, 6, 7, 8, 9]
    #运用变量交换 ---- k1[i] , k1[i+1] = k1[i+1] ,k1[i]    相邻互换位置
    
    #   for  in 循环比较
    k1 = [11,22,1, 2, 3, 4, 5, 6, 9, 8, 9, ] for j in range(len(k1)-1): for i in range(len(k1)-1-j): if k1[i] > k1[i+1]: k1[i], k1[i + 1] = k1[i + 1], k1[i] print(k1) # [1, 2, 3, 4, 5, 6, 8, 9, 9, 11, 22] k1 = [11,22,1, 2, 3, 4, 5, 6, 7, 8, 9, ] for i in range(len(k1)-1): for i in range(len(k1) - 1): if k1[i] > k1[i+1]: k1[i] , k1[i+1] = k1[i+1] ,k1[i] print(k1) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22] for i in range(len(k1)-1): # for in 陷入循环 列表 if k1[i] > k1[i+1]: k1[i], k1[i + 1] = k1[i + 1], k1[i] print(k1) # [11, 1, 2, 3, 4, 5, 6, 7, 8, 9, 22]
  • 相关阅读:
    html中的浮动
    Html中元素的分类
    前端标签命名规范
    meta详解
    CSS的嵌套方法
    html标签
    W3C标准
    AE待整理
    AE小知识点备忘录
    Maximum Subarray
  • 原文地址:https://www.cnblogs.com/lxcai213/p/13276054.html
Copyright © 2011-2022 走看看