常用排序算法之堆排序与快速排序
来源:互联网 发布:淘宝店铺免费代码 编辑:程序博客网 时间:2024/04/30 03:23
'堆排序
Option Explicit
Dim Result, I
Dim TestData(100)
const N = 100
Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next
'堆排序
Sub HSort(byRef Array, low, hi)
Dim i, t, j, p, l, r
For i = hi To low + 1 Step -1
j = i
p = Int((j-low+1)/2)+low-1
t = Array(j)
Do
If p = low-1 Then
Exit Do
End If
If t > Array(p) Then
Array(j) = Array(p)
j = p
p = Int((j-low+1)/2)+low-1
Else
Exit Do
End If
Loop
Array(j) = t
Next
For i = hi To low + 1 Step -1
t = Array(i)
Array(i) = Array(low)
j = low
Do
l = (j-low+1)*2+low-1
If l < i Then
r = (j-low+1)*2+low
If r < i Then
If Array(l) < Array(r) Then
l = r
End If
End If
If t < Array(l) Then
Array(j) = Array(l)
j = l
Else
Exit Do
End If
Else
Exit Do
End If
Loop
Array(j) = t
Next
End Sub
HSort TestData, 0, N - 1
For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next
MsgBox(Result)
Option Explicit
Dim Result, I
Dim TestData(100)
const N = 100
Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next
'堆排序
Sub HSort(byRef Array, low, hi)
Dim i, t, j, p, l, r
For i = hi To low + 1 Step -1
j = i
p = Int((j-low+1)/2)+low-1
t = Array(j)
Do
If p = low-1 Then
Exit Do
End If
If t > Array(p) Then
Array(j) = Array(p)
j = p
p = Int((j-low+1)/2)+low-1
Else
Exit Do
End If
Loop
Array(j) = t
Next
For i = hi To low + 1 Step -1
t = Array(i)
Array(i) = Array(low)
j = low
Do
l = (j-low+1)*2+low-1
If l < i Then
r = (j-low+1)*2+low
If r < i Then
If Array(l) < Array(r) Then
l = r
End If
End If
If t < Array(l) Then
Array(j) = Array(l)
j = l
Else
Exit Do
End If
Else
Exit Do
End If
Loop
Array(j) = t
Next
End Sub
HSort TestData, 0, N - 1
For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next
MsgBox(Result)
'快速排序
Option Explicit
Dim Result, I
Dim TestData(100)
const N = 100
Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next
Sub Swap(byRef Array, first, second)
Dim t
t = Array(first)
Array(first) = Array(second)
Array(second) = t
End Sub
'快速排序
Sub QSort(byRef Array, low, hi)
Dim i, j, p
While low < hi
p = Array(hi)
i = low - 1
For j = low To hi-1
If Array(j) <= p Then
i = i + 1
Swap Array, i, j
End If
Next
Swap Array, i+1, j
QSort Array, low, i
low = i + 2
Wend
End Sub
QSort TestData, 0, N - 1
For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next
MsgBox(Result)
Option Explicit
Dim Result, I
Dim TestData(100)
const N = 100
Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next
Sub Swap(byRef Array, first, second)
Dim t
t = Array(first)
Array(first) = Array(second)
Array(second) = t
End Sub
'快速排序
Sub QSort(byRef Array, low, hi)
Dim i, j, p
While low < hi
p = Array(hi)
i = low - 1
For j = low To hi-1
If Array(j) <= p Then
i = i + 1
Swap Array, i, j
End If
Next
Swap Array, i+1, j
QSort Array, low, i
low = i + 2
Wend
End Sub
QSort TestData, 0, N - 1
For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next
MsgBox(Result)
- 常用排序算法之堆排序与快速排序
- 数据结构与算法之排序:堆排序、归并排序及快速排序
- 常用排序算法之堆排序
- Java常用排序算法之堆排序
- 常用排序算法之堆排序
- 常用排序算法之堆排序
- 常用排序算法之堆排序
- 常用排序算法之快速排序算法
- 常用排序算法——插入排序,快速排序,归并排序,堆排序,计数排序
- 【朝花夕拾之排序算法总结】 快速排序,堆排序总结
- 常用排序算法之快速排序
- 常用排序算法之快速排序
- Java常用排序算法之快速排序
- 常用排序算法之快速排序
- 常用排序算法之快速排序
- 常用排序算法之快速排序
- JAVA常用排序算法之快速排序
- 常用排序算法之快速排序
- 有用链接
- 盖茨巨额家产构建慈善帝国
- 近来一段时间的经典语录
- 考查嵌入式C开发人员的最好的0x10道题
- 固定的左侧移动导航菜单
- 常用排序算法之堆排序与快速排序
- 九九乘法表
- Oracle数据库的物理结构
- 常用排序算法之插入排序与希尔排序
- C语言中类型转换和字节对齐
- 常用排序算法之归并排序
- 通过log4j日志异步化大幅提升系统性能
- 常用排序算法之计数排序,基数排序与桶排序
- 让你帖子不沉的十大方法