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

  

原文地址:https://www.cnblogs.com/hark0623/p/4341684.html