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");
    		}
    	}
    }
    

      

  • 相关阅读:
    使用jekyll和Github搭建个人博客
    numpy的ndarray和matrix的运算
    Beta分布
    卡方分布
    二项式分布
    正态分布
    概率质量函数(PMF)、概率密度函数(PDF)和累积概率密度函数(CDF)
    Jetson AGX Xavier/Ubuntu测试SSD的读写速度
    Jetson AGX Xavier/Ubuntu安装SSD
    datatable 参数详解
  • 原文地址:https://www.cnblogs.com/cloudwind/p/2608589.html
Copyright © 2011-2022 走看看