algorithm-base/animation-simulation/数据结构和算法/简单选择排序.md

73 lines
4.0 KiB
Java
Raw Normal View History

2021-03-20 08:30:29 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2021-03-20 07:58:25 +00:00
### ****
n-i+1 i
![](https://cdn.jsdelivr.net/gh/tan45du/github.io.phonto2@master/myphoto/微信截图_20210120150816.4za4u7331sw0.png)
绿 4 4 1
min
2021-03-21 05:04:38 +00:00
![](https://img-blog.csdnimg.cn/2021032113041462.gif)
2021-03-20 07:58:25 +00:00
****
```java
class Solution {
public int[] sortArray(int[] nums) {
int len = nums.length;
int min = 0;
for (int i = 0; i < len; ++i) {
min = i;
//遍历到最小值
for (int j = i + 1; j < len; ++j) {
if (nums[min] > nums[j]) min = j;
}
if (min != i) swap(nums,i,min);
}
return nums;
}
public void swap (int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
```
****
i n - i ,n (n-1) + (n-2) +.... + 2 + 1= n*(n-1)/2 0 () n - 1 O(n^2)
****
O(1)
****
![](https://cdn.jsdelivr.net/gh/tan45du/github.io.phonto2@master/myphoto/微信截图_20210120153751.7dygihb0ko80.png)
2 3
| | | | | | |
| ------------ | -------------- | -------------- | -------------- | ---------- | -------- |
| | O(n^2) | O(n^2) | O(n^2) | O(1) | |