如果写伪代码笔记

本文深入解析了伪代码的概念及其在算法描述中的应用,详细介绍了包括快速排序、选择排序和冒泡排序在内的多种排序算法的伪代码实现,并探讨了二分查找算法的工作原理。

介绍

伪代码(Pseudocode)是一种非正式的,类似于英语结构的,用于描述模块结构图的语言。(百度百科)

要点

  1. 变量:不需要声明变量类型,对于重要变量加以注释。
  2. 变量赋值: a ⬅ 3(把3赋值给a) ⬅ 比较麻烦 可以使用 =
  3. 表达式: 算数运算符 + - * / ^ 关系运算符 = != > < <= >= 逻辑运算符 and or not
  4. 条件语句:
if x = 1
	then *****
	else *****

if x = 1
	then ***
	elseif x = 2    //elseif 要连在一起
		then ***
	else ***
  1. 循环结构
    while
while x < 10
	dp ***
	***
	end

for

for var init to limit by incr do  //var 变量 into 赋值 
    s
end

for i ⬅ 0 to 10
	do ***
	***
	end
  1. 结束语句:exit 循环结束 return 算法结束
  2. 注释://

实例

快速排序

GetPartation(A,l,r)
	x = A[r]
	i = l - 1
	for j = p to r-1
		do if A[j] <= x
			then i = i + 1
			exchange A[i] <->A[j]
	exchange A[i+1] <-> A[r]
	return i+1
end
QuickSort(A,l,r)
	if l < r
		mid = GetPartation(A,l,r)
		QuickSort(A,l,mid-1)
		QuickSort(A,mid+1,r)
end

选择排序

select sort
	list = {}
	for i = 0 to list.length - 2 do
		index = i;
		if list[j] < list[index] then
			index = j
		for j = i+1 to list[index] then
			index = j;
		end
		if index < i end
			list.swap(index,i)
	end

冒泡排序

bubble sort
	list = {}
	for i = list.length-1 donw to 0  do
		for j = i-1 to 0 do
			if list[j] > list[j+1] then
				list.swap(j,j+1)
		end
	end

Binary_Search(first, last)
IF (first > last) THEN
RETURN false
ELSE
set middle to (first + middle) / 2
set result to item.compareto(middle)
IF (result = 0) THEN
RETURN true
ELSE
IF (result < 0) THEN
Binary_Search(first, middle)
ELSE
Binary_Sreach(middle, last)
二分查找

Binary_Search(A,first,last,key) // A是数组 first last查询起点终点 key查询值
	if first > last then
		return false
	else
		mid = (first + last) / 2
		if A[mid] = key then
			return true;
			else if A[mid] < key then
				Binary_Search(A,first,mid,key)
			else
				Binary_Search(A,mid,last,key)
### 如何用 LaTeX 编伪代码 LaTeX 提供了几种不同的宏包来编伪代码,其中最常用的有 `algorithm2e` 和 `algorithmicx` 宏包[^1]。这些宏包允许用户以结构化的方式定义算法并生成美观的伪代码。 #### 使用 `algorithm2e` 宏包编伪代码示例 以下是基于 `algorithm2e` 的简单伪代码模板: ```latex \documentclass{article} \usepackage[ruled,vlined]{algorithm2e} \begin{document} \begin{algorithm}[H] \DontPrintSemicolon \KwData{This algorithm takes no input data.} \KwResult{It prints a greeting message.} \caption{Greeting Algorithm}\label{alg:greet} \Begin{ Print("Hello, world!")\; } \end{algorithm} \end{document} ``` 上述代码展示了如何创建一个简单的问候语算法,并通过 `\caption` 设置标题。 --- #### 使用 `algorithmicx` 宏包编伪代码示例 对于更复杂的场景,可以使用 `algorithmicx` 宏包及其子包 `algpseudocode` 来实现灵活的伪代码设计: ```latex \documentclass{article} \usepackage{algorithm} \usepackage{algpseudocode} \begin{document} \begin{algorithm} \caption{Sorting Algorithm Example}\label{alg:sort} \begin{algorithmic}[1] \Procedure{Sort}{$A$} \For{$i = 0$ to $n-1$} \State Find the minimum element in $A[i..n-1]$ \State Swap it with $A[i]$ \EndFor \EndProcedure \end{algorithmic} \end{algorithm} \end{document} ``` 这段代码展示了一个基本的排序算法框架,利用了 `algorithmicx` 中的循环和条件控制命令。 --- #### 利用 LyX 创建伪代码 如果倾向于图形化的编辑工具,则可以通过 LyX 实现伪代码的设计。具体方法如下: 首先,在 LyX 中以列表形式输入算法逻辑,随后切换到 TeX 源码视图,将 `\item` 替换为相应的状态声明(如 `\State`),并将内容嵌入到合适的伪代码环境中[^2]。 此外,建议将原始笔记保存为 LyX 注释(Comment)而非 Note,以便后续修改更加便捷。 --- ### 下载资源与模板的方法 可以从 CTAN 或 Overleaf 获取官方支持的伪代码模板及相关文档。CTAN 是综合性的 LaTeX 软件库,而 Overleaf 提供在线协作平台以及预配置的项目模板。 例如访问链接 https://ctan.org/pkg/algorithm2e?lang=en 可获得关于 `algorithm2e` 的最新版本说明;同样地,Overleaf 上也有许多公开共享的例子可以直接克隆使用。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值