Algorithmic Implementation series(6) Implementation of Quick_Sort

本文介绍了一种使用C++实现的快速排序算法,包括元素交换、分区操作等关键步骤,并展示了如何通过递归调用完成整个数组的排序过程。

Compiler: gcc 4.7.3

C++ Standard: C++0x

OS:CentOS 6.3 x86

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 //swap the ith element and the jth element in an array
  6 void swap_elements(int ia[], const size_t i, const size_t j) {
  7     if(i == j) { return; }
  8     const int tmp = ia[i - 1];
  9     ia[i - 1] = ia[j - 1];
 10     ia[j - 1] = tmp;
 11 }
 12 
 13 const size_t partition(int ia[], const size_t p,const size_t r) {
 14     //The value of variable x is the last element in array ia.
 15     const int x = ia[r - 1];
 16 
 17     //Variable i marks the position before the first element in
 18     //array ia.
 19     int i = p - 2;
 20 
 21     //The for loop goes from the first element inclusive to one
 22     //before the last element in array ia.
 23     for(size_t j = p; j != r; ++j) {
 24         if(ia[j - 1] <= x) {
 25             ++i;
 26             swap_elements(ia, i + 1, j);
 27         }
 28     }
 29     swap_elements(ia, i + 2, r);
 30     return i + 2;
 31 }
 32 
 33 void Quick_Sort(int ia[], const size_t p,const size_t r) {
 34     if(p < r) {
 35         const size_t q = partition(ia, p, r);
 36         Quick_Sort(ia, p, q - 1);
 37         Quick_Sort(ia, q + 1, r);
 38     }
 39 }
 40 
 41 int main() {
 42     int ia[] = {2, 32, 4, 6, -2, 4, 5, -55, -3, 55, 88};
 43     const size_t size = sizeof(ia)/sizeof(int);
 44 
 45     Quick_Sort(ia, 1, size);
 46 
 47     for(size_t i = 0; i != size; ++i) {
 48         cout << ia[i] << " ";
 49     }
 50     cout << endl;                                                                                   
 51 
 52     return EXIT_SUCCESS;
 53 }
 54 

在 LaTeX 中,`algorithmic` 环境是用于编写算法伪代码的一种常见方式,尤其适合需要排版清晰、结构严谨的算法描述。该环境通常与 `algorithm` 宏包结合使用,以实现算法的浮动体管理,同时利用 `algpseudocode` 定义伪代码的格式。 ### 基本使用方法 要在 LaTeX 中使用 `algorithmic` 环境编写算法,通常需要在文档前导部分引入必要的宏包,如 `algorithm` 和 `algpseudocode`。以下是一个典型的示例: ```latex \documentclass{article} \usepackage{algorithm} \usepackage{algpseudocode} \begin{document} \begin{algorithm} \caption{An algorithm with caption}\label{alg:cap} \begin{algorithmic} \Require $n \geq 0$ \Ensure $y = x^n$ \State $y \gets 1$ \State $X \gets x$ \State $N \gets n$ \While{$N \neq 0$} \If{$N$ is even} \State $X \gets X \times X$ \State $N \gets \frac{N}{2}$ \Comment{This is a comment} \ElsIf{$N$ is odd} \State $y \gets y \times X$ \State $N \gets N - 1$ \EndIf \EndWhile \end{algorithmic} \end{algorithm} \end{document} ``` 上述代码中,`algorithm` 环境用于定义算法的浮动体,而 `algorithmic` 环境则用于描述算法的具体步骤。通过 `\Require` 和 `\Ensure` 可以定义算法的输入和输出条件,`\State` 用于描述具体的算法操作,`\If`、`\ElsIf`、`\While` 等控制结构则用于实现条件判断和循环[^2]。 ### 格式化与注释 在 `algorithmic` 环境中,可以使用 `\Comment{}` 来添加注释,以便于解释特定步骤的功能。此外,还可以通过自定义命令来增强算法的可读性,例如定义变量更新操作或特定的数学表达式。 例如,可以定义一个自增操作: ```latex \algrenewcommand{\algorithmicrequire}{\textbf{Input:}} \algrenewcommand{\algorithmicensure}{\textbf{Output:}} ``` 这些命令可以重新定义 `\Require` 和 `\Ensure` 的显示文本,使其更具描述性。 ### 常见问题与解决方案 在使用 `algorithmic` 环境时,可能会遇到一些常见的问题,如浮动体的位置控制、算法编号的设置等。可以通过调整 `algorithm` 环境的参数来解决这些问题,例如使用 `[htbp]` 来指定算法的放置位置: ```latex \begin{algorithm}[htbp] ``` 此外,还可以通过 `caption` 和 `label` 命令为算法添加标题和标签,以便于引用和定位[^2]。 ### 总结 使用 `algorithmic` 环境编写算法是 LaTeX 中一种非常有效的方式,能够帮助用户创建结构清晰、格式规范的伪代码。通过合理使用宏包和命令,可以进一步提升算法的可读性和美观性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值