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;
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    DIV 设置垂直居中
    JavaScript--什么是函数
    JavaScript--引用JS外部文件
    JavaScript--如何插入JS
    CSS-类和ID选择器的区别
    CSS-ID选择器
    CSS类选择器
    CSS样式介绍
    HTML--使用mailto在网页中链接Email地址
    HTML--form表单中的label标签
  • 原文地址:https://www.cnblogs.com/hark0623/p/4341684.html
Copyright © 2011-2022 走看看