-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path히프 정렬 프로그램.py
44 lines (39 loc) · 949 Bytes
/
히프 정렬 프로그램.py
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
def heapify(a, h, m):
v, j = a[h], 2 * h
while j <= m:
if j < m and a[j] < a[j+1]:
j += 1
if v >= a[j]:
break
else:
a[int(j/2)] = a[j]
j *= 2
a[int(j/2)] = v
def heapSort(a, n):
for i in range(int(n/2), 0, -1):
heapify(a, i, n)
for i in range(n-1, 0, -1):
a[1], a[i+1], = a[i+1], a[1]
heapify(a, 1, i)
def checkSort(a, n):
isSorted = True
for i in range(1, n):
if a[i] > a[i+1]:
isSorted = False
if (not isSorted):
break
if isSorted:
print("정렬 완료")
else:
print("정렬 오류 발생")
import random, time
N = 10000
a = []
a.append(None)
for i in range(N):
a.append(random.randint(1, N))
start_time = time.time()
heapSort(a, N)
end_time = time.time() - start_time
print("선택 정렬의 실행 시간 (N=%d) : %0.3f"%(N, end_time))
checkSort(a, N)