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日
最近浏览更多
阿猫喵喵喵996  LV12 1月25日
1WQAQW1  LV2 2023年6月12日
crosa_Don  LV18 2022年7月6日
最代码-宋家辉  LV61 2021年6月14日
itcaizhe  LV9 2021年6月10日
Killah  LV9 2021年4月16日
2469095052  LV8 2021年3月3日
1771658781  LV5 2021年1月3日
dongzhan  LV12 2020年12月22日
186啊哈  LV2 2020年12月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友