找回密码
 立即注册
  • QQ空间
  • 回复
  • 收藏

算法基础

admin 2019-3-11 05:54 91人围观 C++相关

来源于微信公众号:取名无力症

算法基础 Get Started


从今天起,开始看算法导论,不出意外的话,我将会用英文和中文同时记录下我的笔记和心得。如果有人在看的话,如果对我写的有问题,请直接留言指出,不胜感激。
From today, I begin to read the book named Introduction to Algorithms. Without accident, I will record my notes and my thoughts in both English and mandarin. If someone is watching, if there is a problem with me or the blog, please leave a message and point out, I would be grateful.

插入排序 Insertion sort


相信大家都玩过斗地主,每个人在抓牌后,拿到手里的牌是无序的,我们常用的理牌手段就是插入排序。
I trust everyone has played poker before. After draw cards, the cards we hold is disordered, the way we always used is Insertion sort.
我会用伪代码和不同的计算机语言展示插入排序
I will use pseudocode and a lot of computer language show Insertion sort.

伪代码如下
pseudocode:

for j=2 to A.length
key=A[j]
//Insert A[j] into the sorted sequenceA[1..j-1]
i=j-1
while i>0 and A[i]>key
A[i+1]=A[i]
i=i-1
A[i+1]=key

python:

A=[9,7,6,4,3,1]
for j in range(len(A)-1):
key=A[j+1]
i=j
while(i>=0 and A[i]>key):
A[i+1]=A[i]
i=i-1
A[i+1]=key
for num in A:
print(num)

----------------------------------------------------------------------------------------------------------------------
我们尊重原创,也注重分享,文章来源于微信公众号:取名无力症,建议关注公众号查看原文。如若侵权请联系qter@qter.org。
----------------------------------------------------------------------------------------------------------------------

鲜花

握手

雷人

路过

鸡蛋

yafeilinux和他的朋友们微信公众号二维码

微信公众号

专注于Qt嵌入式Linux开发等。扫一扫立即关注。

Qt开源社区官方QQ群二维码

QQ交流群

欢迎加入QQ群大家庭,一起讨论学习!

我有话说......