zoukankan      html  css  js  c++  java
  • Hark的数据结构与算法练习之简单选择排序

    /*
     * 简单选择排序
     */
    public class SimpleSort {
    	public static void main(String[] args) {
    		int[] arrayData = { 5, 9, 6, 7, 4, 1, 2, 3, 8 };
    		SimpleSortMethod(arrayData);
    		for (int integer : arrayData) {
    			System.out.print(integer);
    			System.out.print(" ");
    		}
    	}
    
    	/*
    	 * 时间复杂度 :因为是双循环求解,所以是o(n^2)  
    	 * 空间复杂度:使用的临时空间大小是一个常量,而不是与n有关系,所以空间复杂度是O(1)
    	 * 说明:
    	 * 其实与冒泡的排序大体是相似的,不同之处是冒泡判断出两个数大小后,直接进行交换;而简单选择排序是找出最大/最小的数后,再进行排序
    	 */
    	public static void SimpleSortMethod(int[] arrayData) {
    		int maxIndex, i, j,valueTemp;
    		for (i = 0; i < arrayData.length; i++) {
    			maxIndex = i;
    			for (j = i; j < arrayData.length; j++) {
    				if(arrayData[j] > arrayData[maxIndex])
    				{
    					//在这里,只记录最大值的索引,在后边那个if中进行数值的交换
    					maxIndex = j;
    				}
    			}
    			if(i!=maxIndex)
    			{
    				valueTemp= arrayData[i];
    				arrayData[i]= arrayData[maxIndex];
    				arrayData[maxIndex] = valueTemp;
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    《.Net之美》样章 1.1 理解泛型(转载)
    jQuery&JSON~~
    TreeView绑定XML
    TreeView 的 CheckBox 被点击时的引发页面回发事件
    开发中巧用Enum枚举类型
    今天开始学习Python
    获取天气预报
    ORACLE数据库备份
    Eclipse配置Tomcat
    ORACLE常见错误及解决办法
  • 原文地址:https://www.cnblogs.com/hark0623/p/4341684.html
Copyright © 2011-2022 走看看