j将测试进行到底~~
排序算法之一(冒泡排序)
上一篇 /
下一篇 2008-10-22 13:23:58 / 天气: 阴雨
/ 心情: 郁闷
/ 个人分类:数据结构算法
终于决定靠实力找工作,不再把自己的活动范围仅仅限制为现在的实习单位。复习的范围先从最基础的数据结构算法开始吧,今天先用JAVA小写了下冒泡排序,时间复杂度和空间复杂度应该不是最优的,仅供大家参考啦。
public class BubbleSort {
public void bubbleSort(int[] array){
int n = array.length;
for(int i=n;i>1;i--){
for(int j=0;j<i-1;++j){
if(array[j+1]<array[j]){
int temp = array[j+1];
array[j+1] = array[j];
array[j]=temp;
}
}
}
}
public static void main(String args[]){
int[] array = {10,9,8,7,6,5,4,3,2,1};
for(int i=0;i<array.length;i++){
if(i<array.length-1){
System.out.print(array[i]+",");
}
else
System.out.print(array[i]);
}
System.out.println();
new BubbleSort().bubbleSort(array);
for(int i=0;i<array.length;i++){
if(i<array.length-1){
System.out.print(array[i]+",");
}
else
System.out.print(array[i]);
}
System.out.println();
}
}
收藏
举报
TAG:
数据结构算法