【Java基础】--算法与数组
2015-09-15 15:48
567 查看
前言
数组就是一堆数据类型相同的数按照一定的顺序排列的集合。数组就像标号的盒子,一排的就是一维数组,多排的就是二维数组。数组方便的数据的存取和查找。算法
原数组:5,8,7,2,9冒泡法:
相邻两个数比较大小,交换,最终找到最大值。选择排序:
从现有序列里面找最小,放到起始位置。在Java中的具体应用:
<span style="font-size:14px;">public class NumSort{ public static void main(String[] args){ int [] a=new int [args.length]; for(int i =0;i<args.length;i++){ a[i]= Integer.parseInt(args[i]); } print(a); selectionSort(a); print(a); } public static void selectionSort(int [] a){ //冒泡排序 int temp = 0; for (int i = a.length - 1; i > 0; --i) { for (int j = 0; j < i; ++j) { if (a[j + 1] < a[j]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } //选择排序 // int k ,temp; // for(int i=0;i<a.length;i++){ // k=i; // for(int j=k+1;j<a.length;j++){ // if(a[j]<a[k]){ // k=j; // } // } // if(k!=i){ // temp=a[i]; // a[i]=a[k]; // a[k]=temp; // } //} } private static void print(int[] a){ for(int i=0;i<a.length;i++){ System.out.print(a[i] + " "); } System.out.println(); } }</span>
运行结果:
总结
算法要一步一步的在纸上写一遍,然后单步调试完整走一遍,就不会那么晕了。我相信,并持续相信,我的编程会学的特别棒的。相关文章推荐
- spring mvc学习(一)
- JAVA中如何使用log4j作为日志记录工具
- Spring MVC基于注解的Controller
- javax.servlet.jsp.JspException cannot be resolved to a type
- 用 java 写一个计算器
- java.util.concurrent 学习笔记(1)
- JDK环境变量配置
- Maven学习记录5——Eclipse添加maven依赖找不到maven库中的jar
- javac 编译
- 【leetcode】ZigZag Conversion【java】
- How to remove the action suffix extension in Struts 2
- Eclipse上自己生成的keystore,出现错误“Keystore was tampered with, or password was incorrect”的一种解决办法
- springMvc get参数转码
- JAVA设计模式之单例模式
- Struts 2 – Development mode example
- 在java servlet中获得session中保存对象的值。
- 七牛---用JAVA生成各种凭证代码
- Struts 2 Namespace configuration example and explanation
- java.util.ResourceBundle使用
- springAOP的入门例子