首页>代码>总结分享下日常生活中用到的一些java功能,很好很强大,功能很多有20个,希望大家喜欢>/Demos-JavaSE/DataStructure/src/com/zyg/test/sort/BubbleSort.java
package com.zyg.test.sort;
public class BubbleSort {
public static void main(String[] args) {
int[] array = {10,5,7,12};
System.out.print("排序前;");
printArray(array);
bubbleAscSort(array);
bubbleDescSort(array);
}
/**
* 冒泡升序排序
* @param array
*/
public static void bubbleAscSort(int[] array){
int length = array.length;
int temp=0;
for(int i=0;i<length-1;i++){
for(int j=0;j<length-i-1;j++){
if(array[j+1]<array[j]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println();
System.out.print("升序排序后;");
printArray(array);
}
/**
* 冒泡降序排序
* @param array
*/
public static void bubbleDescSort(int[] array){
int length = array.length;
int temp=0;
for(int i=0;i<length-1;i++){
for(int j=0;j<length-i-1;j++){
if(array[j+1]>array[j]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println();
System.out.print("降序排序后;");
printArray(array);
}
public static void printArray(int[] array){
int length = array.length;
for(int i=0;i<length;i++){
System.out.print(array[i]);
if(i!=length-1){
System.out.print(",");
}
}
}
}
最近下载更多
数据库1 LV12
2020年7月7日
1279894731 LV1
2019年11月13日
yongzheng132 LV17
2019年6月25日
kld113 LV20
2019年3月5日
fengbinzj LV1
2019年1月25日
rangnixiao LV21
2018年10月20日
Sean_yang LV1
2018年4月10日
javalang LV1
2018年3月20日
1755790963 LV1
2017年9月8日
sc936866 LV11
2017年8月16日

最近浏览