Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
4
Добавлен:
26.03.2015
Размер:
22.02 Кб
Скачать

Public n As Byte

Public Sub heap(i As Integer, a() As Integer, n As Integer)

j = i

Do While (2 * j + 1 <= n) And ((a(j) < a(2 * j)) Or (a(j) < a(2 * j + 1)))

If a(2 * j) > a(2 * j + 1) Then

s = a(j)

a(j) = a(2 * j)

a(2 * j) = s

j = 2 * j

Else

s = a(j)

a(j) = a(2 * j + 1)

a(2 * j + 1) = s

j = 2 * j + 1

End If

Loop

If (2 * j <> n) And (a(j) < a(2 * j)) Then

s = a(j)

a(j) = a(2 * j)

a(2 * j) = s

End If

End Sub

Dim a(20) As Integer

Private Sub пирамида_Click()

Dim i As Integer

Dim m As Integer

Dim s As Integer

Dim l As Integer

i = n \ 2

Print i

l = i

m = n

For k = 1 To l

Call heap(i, a, m)

i = i - 1

Next k

For k = 2 To n

s = a(1)

a(1) = a(m)

a(m) = s

m = m - 1

Call heap(1, a, m)

Next k

End Sub