java算法之简单选择排序

xiaoxiao2021-02-28  115

基本思想 每一趟从待排序的数据元素中选出最小的的一个元素,顺序放在已经排好序的数组的最后,直到全部待排的数据排完序。 代码 public class SelectSort {

public static void selectSort(int[] arr) { int temp; int index; for (int i = 0; i < arr.length; i++) { index = i; for (int j = i + 1; j < arr.length; j++) { if (arr[index] > arr[j]) { index = j; } } temp = arr[index]; arr[index] = arr[i]; arr[i] = temp; } } public static void main(String[] args) { int[] a = new int[] { 49, 38, 65, 97, 76, 13, 27, 50 }; selectSort(a); for (int i : a) System.out.print(i + " "); }

} 时间复杂度 O(n^2)

转载请注明原文地址: https://www.6miu.com/read-39097.html

最新回复(0)