zoukankan      html  css  js  c++  java
  • 排序算法总结

    插入排序:

     1 def insert_sort(chaosList): #自己写的
     2     tempList = []
     3     for i in chaosList:
     4         if len(tempList) == 0:
     5             tempList.append(i)
     6         else:
     7             flag = True
     8             for j in range(len(tempList)-1,-1,-1):
     9                 if tempList[j] > i:
    10                     pass
    11                 else:
    12                     tempList.insert(j+1, i)
    13                     flag = False
    14                     break
    15             if flag:
    16                 tempList.insert(0, i)
    17     return tempList
    18 
    19 def insert_sort1(chaosList): # 参考别人的
    20     for i in range(1,len(chaosList)):
    21         for j in range(i, 0, -1):
    22             if chaosList[j] < chaosList[j-1]:
    23                 chaosList[j], chaosList[j-1] = chaosList[j-1], chaosList[j]
    24     return chaosList
    插入排序

    快速排序:

     1 def fast_sort(chaosList):
     2     if len(chaosList) >1:
     3         i, j = 0, len(chaosList)-1
     4         tempcount = chaosList[i]
     5         while j > i:
     6             # 遍历右边的数值直到小于基准数值,进行赋值
     7             while chaosList[j] >= tempcount and j > i:
     8                 j -= 1
     9             chaosList[i] = chaosList[j]
    10             # 遍历左边的数值直到大于基准数值,进行赋值
    11             while chaosList[i] <= tempcount and j > i:
    12                 i += 1
    13             chaosList[j] = chaosList[i]
    14         # 上两次的赋值,接下来的赋值,以减少过程中的交换
    15         chaosList[i] = tempcount
    16         return fast_sort(chaosList[:i]) + [chaosList[i]] + fast_sort(chaosList[i+1:len(chaosList)])
    17     else:
    18         return chaosList
    快速排序

    算法问题(和上面的排序算法并没有关系):

    题目描述:有一头母牛,它每年年初生一头小母牛。
    每头小母牛从第 4 个年头开始,每年年初也生一头小母牛。(人工授精,不要公牛,谢谢)
    请编程实现计算在第 n 年的时候,共有多少头母牛?

     1 def cowNumbers(year):    # 自己写的
     2     if year > 4:
     3         numbers = 4
     4         for i in range(5, year+1):
     5             # 今年新出生牛的数量 = 三年前牛群总数量
     6             numbers += cowNumbers(i-3)
     7     else:
     8         numbers = year
     9     return numbers
    10 
    11 def cowN(year):    # 别人写的
    12     return cowN(year - 1) + cowN(year - 3) if year > 1 else 1
    母牛问题
    1 第 1 年后共有  1 头牛!
    2 第 2 年后共有  2 头牛!
    3 第 3 年后共有  3 头牛!
    4 第 4 年后共有  4 头牛!
    5 第 5 年后共有  6 头牛!
    6 第 6 年后共有  9 头牛!
    7 第 7 年后共有 13 头牛!
    8 第 8 年后共有 19 头牛!
    9 第 9 年后共有 28 头牛!
    答案

       题目描述:学校有 n 个宿舍楼,第 i 个楼有 ai 个房间。邮递员投递信件的时候发现有些信件并没有指定宿舍楼和房间号,只有房间全局编号(先对第一个宿舍楼的房间编号,第二个宿舍楼在其基础上继续编号,全局编号范围 1- a1+ a2+···+ an)

      例如: n = 2,a1 = 3,a2 = 5,则房间编号范围 1-8,信封上全局编号为 7, 则代表要投递到第二个宿舍楼的第 4 个房间。

      编写程序,输入代表各宿舍楼房间号的长度为 n 的数组,和代表各信封上的全局房间号的长度为 m 的数组。输出每封信件应该投递到哪个宿舍楼的哪个房间。

     1 def dis_room(dormitoryList, letterIndex):
     2     for i in range(len(letterIndex)):
     3         temp_sum = 0
     4         for j in range(len(dormitoryList)):
     5             temp_sum += int(dormitoryList[j])
     6             if temp_sum < int(letterIndex[i]):
     7                 continue
     8             else:
     9                 pre_sum = temp_sum - int(dormitoryList[j])
    10                 print(j + 1, int(letterIndex[i]) - pre_sum)
    11                 break
    12 domList = str(input()).split(',')
    13 letList = str(input()).split(',')
    14 dis_room(domList, letList)
    信封房间号问题
     1 输入:
     2 3,5
     3 7
     4 输出:
     5 2 4
     6 
     7 输入:
     8 10,15,12
     9 1,9,12,23,26,37
    10 输出:
    11 1 1
    12 1 9
    13 2 2
    14 2 13
    15 3 1
    16 3 12
    答案

      题目描述:给定一个数组,使数组中的数字左移 n 位

     1 def move_num(li, move_pos):
     2     move_index = move_pos
     3     index = 0
     4     temp = li[index]
     5     while True:
     6         move_index %= len(li)
     7         if move_index == 0:
     8             li[index] = temp
     9             break
    10         li[index] = li[move_index]
    11         index = move_index
    12         move_index += move_pos
    13     return li
    14 
    15 li = [1, 2, 3, 4, 5, 6, 7]
    16 move_pos = int(input('input integer: '))
    17 res = move_num(li, move_pos)
    18 print res
    左移数组
    1 input integer: 5
    2 [6, 7, 1, 2, 3, 4, 5]
    3 
    4 input integer: 2
    5 [3, 4, 5, 6, 7, 1, 2]
    6 
    7 input integer: 8
    8 [2, 3, 4, 5, 6, 7, 1]
    答案
  • 相关阅读:
    Socket 之 同步以及异步通信
    Socket 之 c#实现Socket网络编程
    Socket 之 API函数介绍
    Socket 之 原理与编程基础
    C# 之 user32函数库
    WinServer 之 访问同网段服务器 或 同一服务器多虚拟机间的访问
    annex-b格式
    FLV文件格式解析
    PHP5中的stdClass
    web服务器【apache/nginx] 关闭目录的浏览权限
  • 原文地址:https://www.cnblogs.com/xsmile/p/10606290.html
Copyright © 2011-2022 走看看