• 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();
    	}
    }
    

      

  • 相关阅读:
    RHEL7.2安装及配置实验环境
    VMwareworkstation 12安装
    Iterator主要有三个方法:hasNext()、next()、remove()详解
    httpclient
    http接口测试——Jmeter接口测试实例讲解
    java获取Excel的导出
    java获取Excel的导入
    java的post请求
    java的get请求
    Python3 列表(List)基础
  • 原文地址:https://www.cnblogs.com/zhaofeng555/p/3594868.html
Copyright © 2020-2023  润新知