⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bubblesort.java

📁 JAVA培训用实用代码
💻 JAVA
字号:
public class BubbleSort{
	public static void main(String args[]){
		int array[] = {55,2,6,4,32,12,-9,73,26,37};
		System.out.println("数据原始顺序:");
		for(int i=0; i<array.length; i++) {     	
			System.out.print(array[i] + "\t");
		}
		
		BubbleSort b = new BubbleSort();
		int[] result =  b.bubble(array); 			
		System.out.println("\n\n排序后:");
		for(int i=0; i<result.length; i++) {        
			System.out.print(result[i] + "\t");
		}
	}

	public int[] bubble(int a[]) {
	    int temp,size = a.length;
		// 外层循环,控制“冒泡”的最终位置
		for(int i=size-1; i>=1; i--){
	    	boolean end = true;
			// 内层循环,用于相临元素的比较
			for(int j=0; j < i; j++) {     
		       	if(a[j] > a[j+1]) {
		        	temp = a[j];
			    	a[j] = a[j+1];
			    	a[j+1] = temp;
					end = false;    	
			   	}
			}
			if(end == true) {
				break;	
			}			
		} 
		return a;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -