zoukankan      html  css  js  c++  java
  • 05_2_冒泡与选择排序算法

    05_2_冒泡与选择排序算法

    1. 例子

    package Test;
    
    public class NumSort {
    
    	public static void main(String[] args) {
    		int[] a = {5, 4, 712312, 1, 1119, 2, 1118};
    		print(a);
    		//selectSort(a);
    		bubbleSort(a);
    		print(a);
    	}
    	
    	public static void print(int[] a) {
    		for (int i = 0; i < a.length; i++) {
    			System.out.print(a[i] + " ");
    		}
    		System.out.println("");
    	}
    	
    	public static void selectSort(int[] a) {
    		int k, temp;
    		for (int i = 0; i < a.length; i++) {
    			k = i;
    			for (int j = k+1; j < a.length; j++) {
    				if (a[j] < a[k]) {
    					k = j;
    				}
    			}
    			if (k != i) {
    				temp = a[i];
    				a[i] = a[k];
    				a[k] = temp;
    			} 
    		}
    	}
    	
    	public static void bubbleSort(int[] a) {
    		
    		for (int i = 0; i < a.length; i++) {
    			for (int j = 0; j < a.length-i-1; j++) {
    				int temp;
    				if(a[j+1] > a[j]) {
    					temp = a[j];
    					a[j] = a[j+1];
    					a[j+1] = temp;
    				}
    			}
    		}
    	}
    }
    
  • 相关阅读:
    相关术语随笔
    JDK简介和mac下安装和查看版本命令
    英语
    英语学习2
    英语学习-19.1
    为什么java是只有值传递而没有引用传递
    线程
    关于同步异步

    jdk动态代理实现原理总结
  • 原文地址:https://www.cnblogs.com/flyback/p/8898063.html
Copyright © 2011-2022 走看看