zoukankan      html  css  js  c++  java
  • java排序算法-选择排序

    public class SelectionSort {
    
    	private static void selectSortTest() {
    		int[] sortArray = { 5, 2, 4, 1, 3 };
    		System.out.print("选择排序前: ");
    		Utils.printArray(sortArray);
    		selectSort(sortArray); 
    		System.out.print("选择排序后: ");
    		Utils.printArray(sortArray);
    	}
    
    	public static void selectSort(int[] sort) {
    		int temp;
    		for (int i = 0; i < sort.length - 1; i++) {
    			for (int j = i + 1; j < sort.length; j++) {
    				if (sort[i] > sort[j]) {
    					temp = sort[i];
    					sort[i] = sort[j];
    					sort[j] = temp;
    				}
    			}
    		}
    	}
    
    	public static void heapSortTest(){
    		int[] arr = { 5, 2, 4, 1, 3 };
    		Utils.printArray("堆排序前:",arr);
    		heapSort(arr);
    		Utils.printArray("堆排序后:",arr);
    	}
    	
    	public static void heapSort(int[] arr) {
    		int arrLen = arr.length;
    		int temp = 0;
    		
    		//建立堆
    		for (int i = (arrLen-1) / 2; i >= 0; i--)
    			adjustHeap(arr, i, arrLen);
    		
    		//调整堆
    		for (int i = arrLen - 2; i >= 0; i--) {
    			temp = arr[i + 1];
    			arr[i + 1] = arr[0];
    			arr[0] = temp;
    			adjustHeap(arr, 0, i + 1);
    			
    //			Utils.printArray("第"+(9-i)+"次调整:",arr);
    		}
    	}
    
    	public static void adjustHeap(int[] arr, int ri, int n) {
    		int temp = arr[ri];
    		int ci = 2 * ri + 1;
    		while (ci <= n - 1) {
    			if (ci < n - 1 && arr[ci] < arr[ci + 1])
    				ci++;
    			
    			if (temp >= arr[ci])
    				break;
    			arr[(ci - 1) / 2] = arr[ci];
    			ci = 2 * ci + 1;
    		}
    		arr[(ci - 1) / 2] = temp;
    	}
    	
    	public static void main(String [] args){
    		selectSortTest();
    		heapSortTest();
    	}
    }
    

      

  • 相关阅读:
    装饰器实例
    生成器、迭代器脚本实例
    魔法方法和属性
    随机生成验证码
    认证客户端的链接合法性
    将socket通信实现多进程
    线程锁模拟抢票系统
    ntp时间服务器
    蛇形串---------
    两年内计划
  • 原文地址:https://www.cnblogs.com/zhaofeng555/p/3594868.html
Copyright © 2011-2022 走看看