import java.util.Arrays
/**
* 选择排序
* 个人理解:1.下标从0到i,每次循环,依次将i位置的数和j(下标从i+1到arr.length)位置数比较,大于的替换位置
* 2.每一次排序后i及i之前位置的数都已经排好
* 3.时间复杂度,因为需要两层循环,且每层循环都有循环完,所以最好情况和最坏情况都是O(n²)
* 3.空间复杂度和插入排序一样,因为没有用的额外空间,所以为0(1)
* 4.稳定性,因为发生跳跃式变换(跳跃式指非相邻元素交换位置),所以不稳定
*/
public class SortTest {
public static void main(String[] args) {
int[] arr = {70, 12, 82, 43, 99, 23, 38, 53, 22}
selectSort(arr)
Arrays.stream(arr).forEach(a->System.out.print(a+" "))
}
public static void selectSort(int[] arr) {
for (int i=0
int temp = arr[i]
for (int j=i+1
if (arr[j] < temp) {
arr[i] = arr[j]
arr[j] = temp
}
temp = arr[i]
}
}
}
}