/*
 * 简单选择排序
 */
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;
			}
		}
	}
}

  

相关文章:

  • 2021-06-20
  • 2021-06-16
  • 2021-11-06
  • 2022-03-04
  • 2022-12-23
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-07
  • 2022-01-04
  • 2022-12-23
  • 2021-10-30
  • 2021-05-29
  • 2021-09-02
  • 2021-12-09
相关资源
相似解决方案