zoukankan      html  css  js  c++  java
  • 直接选择排序

    package Sort;
    
    import org.junit.Test;
    
    import Sort.utils.Swap;
    
    /*
     * 	直接选择排序为不稳定排序
     * 	直接选择排序的最好时间复杂度和最差时间复杂度都是O(n^2),
     * 	因为即使数组一开始就是正序的,也需要将两重循环进行完,平均时间复杂度也是O(n^2)。
     * 	空间复杂度为O(1),因为不占用多余的空间。直接选择排序是一种原地排序(In-place sort)
     * 	并且稳定(stable sort)的排序算法,优点是实现简单,占用空间小,缺点是效率低,
     * 	时间复杂度高,对于大规模的数据耗时长
     *
     */
    public class SelectSort {
    	public static <T extends Comparable<T>> void selectSort(T[] arr) {
    		for (int i = 0; i < arr.length - 1; i++) {
    			int minIndex = i;
    			for (int j = i + 1; j < arr.length; j++) {
    				if (arr[j].compareTo(arr[minIndex]) < 0) {
    					minIndex = j;
    				}
    			}
    			if (minIndex != i) {
    				Swap.swap(arr, minIndex, i);
    			}
    		}
    	}
    
    	// public static void swap(Object[] obj, int minIndex, int i) {
    	// Object tep = obj[minIndex];
    	// obj[minIndex] = obj[i];
    	// obj[i] = tep;
    	// }
    
    	@Test
    	public void testSelectSort() {
    		Integer[] arr = { 34, 8, 64, 51, 32, 21 };
    		selectSort(arr);
    		for (Integer i : arr) {
    			System.out.print(i + " ");
    		}
    	}
    }
    

  • 相关阅读:
    我为何需要使用空接口?
    Castle 整合.NET Remoting
    MVC结构简介
    在asp.net页面上得到Castle容器的实例
    Castle.MVC框架介绍
    08.vue-router动态路由匹配
    07. vue-router嵌套路由
    06.路由重定向
    04 Vue Router路由管理器
    ES6新特性之 let 、const
  • 原文地址:https://www.cnblogs.com/wei1/p/9582109.html
Copyright © 2011-2022 走看看