- package com.tw.ds.sort;
- /**
- *<p>java数据结构之:冒泡排序方法
- *冒泡排序算法的一般性策略:搜索整个值列,比较相邻元素,如果两者的相对次序不对,则交换它们,
- * 其结果是最大值“想水泡一样”移动到值列的最后一个位置上,
- * 这也是它在最终完成排序的值列中合适的位置。然后再次搜索值列,
- * 将第二大的值移动至倒数第二个位置上,重复该过程,直至将所有元素移动到正确的位置上.
- * http://lavasoft.blog.51cto.com/
- * http://www.blogjava.net/hitlang/archive/2007/09/05/142943.html
- * http://tscjsj.blog.51cto.com/412451/84582
- * http://liushibin06.blog.163.com/blog/static/170382582009930111519971/
- *</p>
- * @author tangw 2010-11-22
- *
- */
- public class BubbleSortMain {
- //主方法
- public static void main(String[] args) {
- //定义数组
- int[] items = {2,5,1,4,6,100,11,4};
- //排序
- sort(items);
- //循环输出
- for(int i=0;i<items.length;i++){
- System.out.println("---i="+i+" value="+items[i]);
- }
- }//end method main
- //排序
- public static void sort(int[] arItems){
- int temp;
- for(int i=0;i<arItems.length;++i){
- for(int j=0;j<arItems.length-i-1;++j){
- if(arItems[j] > arItems[j + 1]){
- temp = arItems[j];
- arItems[j] = arItems[j+1];
- arItems[j+1]=temp;
- }
- }
- }
- }// end method sort
- }