VB数组快速排序算法

VB数组快速排序算法VB数组排序模块,使用的是快速排序法,支持Variant、Double、Long、String……等多种数据类型数组排序。OptionExplicitPrivateSubQuicksortInt(list()AsInteger,ByValminAsInteger,ByValmaxAsInteger)Dimmed_valueAsIntegerDim…

VB数组排序模块,使用的是快速排序法,支持 Variant、Double、Long、String……等多种数据类型数组排序。

Option Explicit

Private Sub QuicksortInt(list() As Integer, ByVal min As Integer, ByVal max As Integer)
Dim med_value As Integer
Dim hi As Integer
Dim lo As Integer
Dim i As Integer

‘ If the list has no more than CutOff elements,
‘ finish it off with SelectionSort.
If max <= min Then Exit Sub

‘ Pick the dividing value.
i = Int((max – min + 1) * Rnd + min)
med_value = list(i)

‘ Swap it to the front.
list(i) = list(min)

lo = min
hi = max
Do
‘ Look down from hi for a value < med_value.
Do While list(hi) >= med_value
hi = hi – 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(lo) = list(hi)

‘ Look up from lo for a value >= med_value.
lo = lo + 1
Do While list(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(hi) = list(lo)
Loop

‘ Sort the two sublists.
QuicksortInt list(), min, lo – 1
QuicksortInt list(), lo + 1, max
End Sub
Private Sub QuicksortSingle(list() As Single, ByVal min As Integer, ByVal max As Integer)
Dim med_value As Single
Dim hi As Integer
Dim lo As Integer
Dim i As Integer

‘ If the list has no more than CutOff elements,
‘ finish it off with SelectionSort.
If max <= min Then Exit Sub

‘ Pick the dividing value.
i = Int((max – min + 1) * Rnd + min)
med_value = list(i)

‘ Swap it to the front.
list(i) = list(min)

lo = min
hi = max
Do
‘ Look down from hi for a value < med_value.
Do While list(hi) >= med_value
hi = hi – 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(lo) = list(hi)

‘ Look up from lo for a value >= med_value.
lo = lo + 1
Do While list(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(hi) = list(lo)
Loop

‘ Sort the two sublists.
QuicksortSingle list(), min, lo – 1
QuicksortSingle list(), lo + 1, max
End Sub
Private Sub QuicksortDouble(list() As Double, ByVal min As Integer, ByVal max As Integer)
Dim med_value As Double
Dim hi As Integer
Dim lo As Integer
Dim i As Integer

‘ If the list has no more than CutOff elements,
‘ finish it off with SelectionSort.
If max <= min Then Exit Sub

‘ Pick the dividing value.
i = Int((max – min + 1) * Rnd + min)
med_value = list(i)

‘ Swap it to the front.
list(i) = list(min)

lo = min
hi = max
Do
‘ Look down from hi for a value < med_value.
Do While list(hi) >= med_value
hi = hi – 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(lo) = list(hi)

‘ Look up from lo for a value >= med_value.
lo = lo + 1
Do While list(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(hi) = list(lo)
Loop

‘ Sort the two sublists.
QuicksortDouble list(), min, lo – 1
QuicksortDouble list(), lo + 1, max
End Sub
Private Sub QuicksortString(list() As String, ByVal min As Integer, ByVal max As Integer)
Dim med_value As String
Dim hi As Integer
Dim lo As Integer
Dim i As Integer

‘ If the list has no more than CutOff elements,
‘ finish it off with SelectionSort.
If max <= min Then Exit Sub

‘ Pick the dividing value.
i = Int((max – min + 1) * Rnd + min)
med_value = list(i)

‘ Swap it to the front.
list(i) = list(min)

lo = min
hi = max
Do
‘ Look down from hi for a value < med_value.
Do While list(hi) >= med_value
hi = hi – 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(lo) = list(hi)

‘ Look up from lo for a value >= med_value.
lo = lo + 1
Do While list(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(hi) = list(lo)
Loop

‘ Sort the two sublists.
QuicksortString list(), min, lo – 1
QuicksortString list(), lo + 1, max
End Sub
Private Sub QuicksortVariant(list() As Variant, ByVal min As Integer, ByVal max As Integer)
Dim med_value As Variant
Dim hi As Integer
Dim lo As Integer
Dim i As Integer

‘ If the list has no more than CutOff elements,
‘ finish it off with SelectionSort.
If max <= min Then Exit Sub

‘ Pick the dividing value.
i = Int((max – min + 1) * Rnd + min)
med_value = list(i)

‘ Swap it to the front.
list(i) = list(min)

lo = min
hi = max
Do
‘ Look down from hi for a value < med_value.
Do While list(hi) >= med_value
hi = hi – 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(lo) = list(hi)

‘ Look up from lo for a value >= med_value.
lo = lo + 1
Do While list(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(hi) = list(lo)
Loop

‘ Sort the two sublists.
QuicksortVariant list(), min, lo – 1
QuicksortVariant list(), lo + 1, max
End Sub

Private Sub QuicksortLong(list() As Long, ByVal min As Integer, ByVal max As Integer)
Dim med_value As Long
Dim hi As Integer
Dim lo As Integer
Dim i As Integer

‘ If the list has no more than CutOff elements,
‘ finish it off with SelectionSort.
If max <= min Then Exit Sub

‘ Pick the dividing value.
i = Int((max – min + 1) * Rnd + min)
med_value = list(i)

‘ Swap it to the front.
list(i) = list(min)

lo = min
hi = max
Do
‘ Look down from hi for a value < med_value.
Do While list(hi) >= med_value
hi = hi – 1
If hi <= lo Then Exit Do
Loop
If hi <= lo Then
list(lo) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(lo) = list(hi)

‘ Look up from lo for a value >= med_value.
lo = lo + 1
Do While list(lo) < med_value
lo = lo + 1
If lo >= hi Then Exit Do
Loop
If lo >= hi Then
lo = hi
list(hi) = med_value
Exit Do
End If

‘ Swap the lo and hi values.
list(hi) = list(lo)
Loop

‘ Sort the two sublists.
QuicksortLong list(), min, lo – 1
QuicksortLong list(), lo + 1, max
End Sub

‘ Sort an array of integers.
Public Sub SortIntArray(list() As Integer)
QuicksortInt list, LBound(list), UBound(list)
End Sub
‘ Sort an array of longs.
Public Sub SortLongArray(list() As Long)
QuicksortLong list, LBound(list), UBound(list)
End Sub
‘ Sort an array of singles.
Public Sub SortSingleArray(list() As Single)
QuicksortSingle list, LBound(list), UBound(list)
End Sub
‘ Sort an array of doubles.
Public Sub SortDoubleArray(list() As Double)
QuicksortDouble list, LBound(list), UBound(list)
End Sub
‘ Sort an array of strings.
Public Sub SortStringArray(list() As String)
QuicksortString list, LBound(list), UBound(list)
End Sub
‘ Sort an array of variants.
Public Sub SortVariantArray(list() As Variant)
QuicksortVariant list, LBound(list), UBound(list)
End Sub


相关参考

VB 冒泡算法

加密算法-MD5算法

V

B


相关





VB 释放资源文件到指定目录函数

VB 读取资源文件里面的字符串

VB中资源文件.res的使用方法详解

VB6.0中创建和使用文本资源文件

VB WindowsMediaPlayer 播放

vbWindowsMediaPlayer的常用属性和方法

VB Environ系统环境变量函数大全

VB 去除文本框粘贴功能

VB LISTBOX

VB 删除数组中的重复元素

VB数组快速排序算法

关于三个概念:ActiveXOLECOM

VB 获得磁盘的文件系统

VB中用API实现文件拖放

加密算法-MD5算法

VB中使用MD5算法

VB 全局热键HOOK (不占系统资源版本)

VB 小技巧自定义TextBox文本框右键菜单

VB 写下载者代码

VB 一行代码的诀窍

VBS教程-wscript对象

vb枚举进程

VB中如何让线程或进程在指定的CPU上运行

VB判断指定的WORD文档是否被打开

VB如何读取快捷方式的目标路径

VBAPI控制输入法状态

为系统加载右键注册控件选项【VB 注册控件】

VB如何根据窗口标题获得进程名称

VB快速查找大型文件中包含的字符串

VB实现可执行文件运行时自删除

VB 打开txt,bat,jpg 任意后缀程序

VB 写文件关联程序

VB 自启动建立右键菜单

VB 判断IP能否ping

VB FTP操作类(可上传、下载、创建文件夹等等)

VB部分文件汇总B

Vb 求素数最经典的方法也是最快的方法

vb用数组方式快速导出MSFlexGrid表格数据到Excel表格中

VBMsFlexGrid控件的使用细则

点击MSFlexGrid数据控件的标题进行数据排序

VB 获取鼠标坐标

VBNEW的用法(申请内存空间)

VB CreateObject函数

VB中的New CreateObject的区别

VB ListBox 添加不重复的值

VB 单击ListView控件某列表头进行排序

VB 简单实现简体与繁体互转

VB 阿拉伯数字转换为中文大写数值函数

VB 获取Textbox文本框中的行数函数


更多精彩>>>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/34123.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注