견고한 정렬 유무 그리고 시간복잡도를 정리해두었다. 어떻게 보면 대부분의 독자는 이를 고려
할 필요가 없을 수 있지만 이런 게 존재한다는 것을 알아두면 유용하다.
표
A-3
배열 정렬 메서드
종류속도견고함공간복잡도시간복잡도
'
quicksort
'
1No0
O
(
n
^
2
)
'
mergesort
'
2Yesn
/
2
O
(
n
log
n
)
'
heapsort
'
3No0
O
(
n
log
n
)
A.6.3
배열 일부만 정렬하기
정렬의 목적 중 하나는 배열에서 가장 크거나 작은 값을 찾는 것이다.
NumPy
는
k
번째 작은 원
소를 기준으로 배열을 나누기 위해 최적화된 메서드인
numpy
.
partition
과
np
.
argpartition
을 제공한다.
In
[
194
]:
np
.
random
.
seed
(
12345
)
In
[
195
]:
arr
=
np
.
random
.
randn
(
20
)
In
[
196
]:
arr
Out
[
196
]:
array
([
-
0
.
2047
,
0
.
4789
,
-
0
.
5194
,
-
0
.
5557
,
1
.
9658
,
1
.
3934
,
0
.
0929
,
0
.
2817
,
0
.
769
,
1
.
2464
,
1
.
0072
,
-
1
.
2962
,
0
.
275
,
0
.
2289
,
1
.
3529
,
0
.
8864
,
-
2
.
0016
,
-
0
.
3718
,
1
.
669
,
-
0
.
4386
])
In
[
197
]:
np
.
partition
(
arr
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.