-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertSort.c
More file actions
63 lines (33 loc) · 895 Bytes
/
Copy pathInsertSort.c
File metadata and controls
63 lines (33 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// InsertSort.c
// AlgorithmForC
//
// Created by gjh on 2021/1/28.
//
#include "InsertSort.h"
#pragma mark - 插入排序
/// 912. 排序数组 插入排序: 稳定排序,在接近有序的情况下,表现优异
int* insertSort(int *nums, int numsSize) {
// 内层 for 循环是倒序循环
for (int i = 0; i < numsSize; i++) {
for (int j = i; j > 0; j--) {
if (nums[j] < nums[j - 1]) {
int tmp = nums[j - 1];
nums[j - 1] = nums[j];
nums[j] = tmp;
}
}
}
return nums;
}
//for (int i = 0; i < numsSize; i++) {
// for (int j = i; j > 0; j--) {
// if (nums[j] < nums[j - 1]) {
// int tmp = nums[j];
// nums[j] = nums[j - 1];
// nums[j - 1] = tmp;
// } else {
// break;
// }
// }
//}