Algorithm: Find the maximum
2015-05-29 08:47
387 查看
Algorithm M
Algorithm M (Find the maximum). Given n elements X[1], X[2],…, X, we
will find m and j such that m = X[j] = max1<=i<=nX[i], where j is the largest
index that satisfies this relation.
M1. [Initialize.] Set j <– n, k <– n-1, m <– X
. (During this algorithm we
will have m = X[j] = maxk< i<=nX[i].)
M2. [All tested?] If k = 0, the algorithm terminates.
M3. [Compare.] If X[k] <= m, go to M5.
M4. [Change m.] Set j <– k, m <– X[k]. (This value of m is a new current
maximum.)
M5. [Decrease k.] Decrease k by one and return to M2. |
Flow diagram
Java program
/** * Created with IntelliJ IDEA. * User: 1O1O * Date: 12/17/13 * Time: 6:52 PM * :)~ * Find the maximum:ALGORITHMS */ public class Main { public static void main(String[] args) { int N = 16; int[] X = new int[17]; X[1] = 503; X[2] = 87; X[3] = 512; X[4] = 61; X[5] = 908; X[6] = 170; X[7] = 897; X[8] = 275; X[9] = 653; X[10] = 426; X[11] = 154; X[12] = 509; X[13] = 612; X[14] = 677; X[15] = 765; X[16] = 703; int j=N; int k=N-1; int m=X ; while (k > 0){ if(X[k] > m){ j=k; m=X[k]; } k--; } System.out.println("Max: m=X[j]="+"X["+j+"]="+m); } }
Outputs
Max: m=X[j]=X[5]=908
Reference
<< The Art of Computer Programming: Fundamental Algorithms>> VOLUME 1, DONALD E. KNUTH相关文章推荐
- 字符串查找 cmd find命令
- Javascript SHA-1:Secure Hash Algorithm
- Jquery中find与each方法用法实例
- jQuery初学:find()方法及children方法的区别分析
- rsync结合find技巧分享
- MySQL的FIND_IN_SET函数使用方法分享
- Jquery find与filter函数区别 说明
- Mysql中的find_in_set的使用方法介绍
- Yii框架中 find findAll 查找出制定的字段的方法对比
- error at ::0 can't find referenced pointcut的问题 3ff8
- wc递归统计代码行数 -- Linux/Unix 哲学魅力
- Linux find命令中-path -prune参数作用详细介绍
- linux中查找命令:find,locate,whereis,which,type的比较
- 常用批处理命令总结3之Find和FindStr
- find命令 — 之查找指定时间内修改过的文件
- linux面试题:删除一个目录下的所有文件,但保留一个指定文件
- 查找技巧,find及xargs的使用
- Shell之 find -exec -ok 的用法
- Shell之 find -exec -ok 的用法
- linux中强大且常用命令:find、grep