-
Notifications
You must be signed in to change notification settings - Fork 2
/
SelectionSort.ts
165 lines (158 loc) · 3.24 KB
/
SelectionSort.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright © https://github.com/jarry All rights reserved.
* @author: [email protected]
* @version: 1.0
*/
class SelectionSort {
arr: Array<number>
constructor() {}
// 标准版
selectionSort1(arr: Array<number>) {
let min: number, minIdx: number, tmp: number
let l = arr.length
for (let i = 0; i < l - 1; i++) {
min = arr[i]
minIdx = i
let j = i + 1
for (; j < l; j++) {
// 从待排序列表找到最小值和位置
if (arr[j] < min) {
min = arr[j]
minIdx = j
}
}
console.log(
'i=' + i,
' j=' + j,
'min=' + min,
'minIdx=' + minIdx,
'arr[]=',
arr
)
// 将待排序里最小值交换到已排序最后面
if (minIdx !== i) {
tmp = arr[i]
arr[i] = min
arr[minIdx] = tmp
}
}
return arr
}
// 新建数组版,无需交换
selectionSort2(arr: Array<number>) {
let min: number,
minIdx: number,
newArr: number[] = []
let l = arr.length
for (let i = 0; i < l; i++) {
min = arr[i]
minIdx = i
let j = i + 1
for (; j < l; j++) {
// 找到并记录下最小值和位置
if (arr[j] < min) {
min = arr[j]
minIdx = j
}
}
console.log(
'i=' + i,
' j=' + j,
'min=' + min,
'minIdx=' + minIdx,
'arr[]=',
arr
)
// 将待排序里最小值添加到新数组中去
newArr.push(min)
// 原数组中删除对应的项
arr.splice(minIdx, 1)
l--
i--
}
return newArr
}
}
;(function () {
const selectionSort = new SelectionSort()
const arr1 = [7, 11, -9, 10, -12, 13, 8]
console.time('sort1')
console.log('origin arr1:', arr1)
console.log('\r\narr1 sorted:', selectionSort.selectionSort1(arr1))
console.timeEnd('sort1')
const arr2 = [7, 11, -9, 10, -12, 13, 8]
console.time('sort2')
console.log('origin arr2:', arr2)
console.log('\r\narr2 sorted:', selectionSort.selectionSort1(arr2))
console.timeEnd('sort2')
})()
/**
jarry@jarrys-MacBook-Pro selectionsort % tsc SelectionSort.ts
jarry@jarrys-MacBook-Pro selectionsort % node SelectionSort.js
origin arr1: [
7, 11, -9, 10,
-12, 13, 8
]
i=0 j=7 min=-12 minIdx=4 arr[]= [
7, 11, -9, 10,
-12, 13, 8
]
i=1 j=7 min=-9 minIdx=2 arr[]= [
-12, 11, -9, 10,
7, 13, 8
]
i=2 j=7 min=7 minIdx=4 arr[]= [
-12, -9, 11, 10,
7, 13, 8
]
i=3 j=7 min=8 minIdx=6 arr[]= [
-12, -9, 7, 10,
11, 13, 8
]
i=4 j=7 min=10 minIdx=6 arr[]= [
-12, -9, 7, 8,
11, 13, 10
]
i=5 j=7 min=11 minIdx=6 arr[]= [
-12, -9, 7, 8,
10, 13, 11
]
arr1 sorted: [
-12, -9, 7, 8,
10, 11, 13
]
sort1: 8.069ms
origin arr2: [
7, 11, -9, 10,
-12, 13, 8
]
i=0 j=7 min=-12 minIdx=4 arr[]= [
7, 11, -9, 10,
-12, 13, 8
]
i=1 j=7 min=-9 minIdx=2 arr[]= [
-12, 11, -9, 10,
7, 13, 8
]
i=2 j=7 min=7 minIdx=4 arr[]= [
-12, -9, 11, 10,
7, 13, 8
]
i=3 j=7 min=8 minIdx=6 arr[]= [
-12, -9, 7, 10,
11, 13, 8
]
i=4 j=7 min=10 minIdx=6 arr[]= [
-12, -9, 7, 8,
11, 13, 10
]
i=5 j=7 min=11 minIdx=6 arr[]= [
-12, -9, 7, 8,
10, 13, 11
]
arr2 sorted: [
-12, -9, 7, 8,
10, 11, 13
]
sort2: 1.083ms
*/