zoukankan      html  css  js  c++  java
  • 冒泡排序

    package sort;
    
    public class BubbleSort {
    
    	private int[] array;
    	private int maxSize;
    
    	public BubbleSort(int maxSize) {
    		this.array = new int[maxSize];
    		this.maxSize = maxSize;
    	}
    
    	private int[] getRandomNum() {
    		for (int i = 0; i < maxSize; i++) {
    			array[i] = (int) Math.round(Math.random() * 1000);
    		}
    		return array;
    	}
    
    	private void getSortedNum() {
    		int i, j;
    		for (i = array.length - 1; i > 0; i--) {
    			for (j = 0; j < i; j++) {
    				if (array[j] > array[j + 1]) {
    					swapNum(j, j + 1);
    				}
    			}
    		}
    	}
    
    	// private void getSortedNum() {
    	// int i, j;
    	// for (i = 1; i < array.length; i++) {
    	// for (j = 0; j < array.length - i; j++) {
    	// if (array[j] > array[j + 1]) {
    	// swapNum(j, j + 1);
    	// }
    	// }
    	// }
    	// }
    
    	private void swapNum(int subScriptA, int subScriptB) {
    		int tempNum;
    		tempNum = array[subScriptA];
    		array[subScriptA] = array[subScriptB];
    		array[subScriptB] = tempNum;
    	}
    
    	public static class BubbleSortApp {
    		public static void main(String[] args) {
    			int maxSize = 1000;
    			BubbleSort bs = new BubbleSort(maxSize);
    			String strs = "";
    
    			bs.getRandomNum();
    			for (int i = 0; i < bs.array.length - 1; i++) {
    				strs += bs.array[i] + ",";
    			}
    			strs += bs.array[bs.array.length - 1];
    			System.out.println("sort :      " + strs);
    
    			strs = "";
    			bs.getSortedNum();
    
    			for (int i = 0; i < bs.array.length - 1; i++) {
    				strs += bs.array[i] + ",";
    			}
    			strs += bs.array[bs.array.length - 1];
    			System.out.println("sorted : " + strs + "\n");
    		}
    	}
    }
    

      

  • 相关阅读:
    解决jar包冲突
    postman使用记录
    get请求直接通过浏览器发请求传数组或者list到后台
    excel中ppmt/pmt/ipmt的计算方式
    unicode编码与解码
    spring参数拼装
    java内存模型(jmm)
    Mysql事务,并发问题,锁机制-- 幻读、不可重复读(转)
    星空雅梦
    星空雅梦
  • 原文地址:https://www.cnblogs.com/cloudwind/p/2608589.html
Copyright © 2011-2022 走看看