VB里面排序要怎么做啊....
要将输入的数字排序并列在LISTBOX里面,输入数字是用inputbox来输入.....我是初学者,真是头痛..实在做不出来...
高手们帮我做做吧..谢谢啦!
要将输入的数字排序并列在LISTBOX里面,输入数字是用inputbox来输入.....我是初学者,真是头痛..实在做不出来...
高手们帮我做做吧..谢谢啦!
解决方案 »
- 想讨教一个关于键盘的上下左右键问题?
- 关于用vb读取excel数据,在线等待
- 请教: 请问怎样VB把string类型转换成其他语言的char类型?
- vb初学者,找几位大侠学习vb
- 在打印DataReport报表,发如何动态改变目标打印机?
- 取得驱动器的序列号
- 求助:使用WAVECOM等GSM MODEM发短信息的控件,在网上找了一些,都有限制
- 制作控件问题?我想做一个按钮控件,在里面做了Label控件如何把Label的caption属性转化为我的控件的caption属性?
- 一般情况下,ado对象绑定的问题。
- 用ACCESS可以存储AVI、WAV以及各种图片文件吗?最好有例子。
- 如何在用户权限下获取硬盘序列号?
- 导入EXCLE保存问题???急
将inputbox每次输入的值存入数据库
然后在将这些记录按从大到小,也就是order by 字段 desc显示出来就可以了
为数组中的元素排序是编程中常会用到一种技巧,排序算法也有多种,这里提供一种,主要特点是把算法部分放入一个通用模块中,通过性强,只要提供原函数、接收函数、数组维数及升降序条件,只需一行调用语句即可在任何时候任何地方对任何数字元素数组完成排序任务。
建立一个模块(当然也可以使用任何一个已存在的通用模块),拷入如下代码。
Option Explicit'自定义排序函数
Function Paixu(shuzu(), shuzu0(), Max As Integer, shunxu As Boolean)
Dim c
Dim b, d, f, l, h As Integer
'临时数组,如果排序的数组维数大于1000请将下面定义的维数相应扩大
Dim n(1000), k(1000) As Double
c = shuzu(1)
For f = 1 To Max:For b = f To Max
If shuzu(b) <= c Then l = b: c = shuzu(l): Exit For
Next:Next
For h = 1 To Max
c = shuzu(l)
For f = 1 To Max:For b = f To Max
If k(b) = 0 Then If shuzu(b) >= c Then d = b: c = shuzu(d): Exit For
Next:Next
n(h) = d: k(d) = 1: d = 0
Next
For h = 1 To Max '将原数组元素按递增顺序写入
If shunxu = False Then shuzu0(h) = shuzu(n(h)) Else shuzu0(Max + 1 - h) = shuzu(n(h))
Next
End Function 模块中的自定义函数Function Paixu(shuzu(), shuzu0(), Max As Integer, shunxu As Boolean)传送过来的变量共有四个,shuzu()是原数组名;shuzu0()为排序后的数组名;Max为数组维数;shunxu=True时升序,shunxu=False时降序。--------------------------------------------------------------------------------
作为示例下面说明调用此函数完成排序的方法
在窗体放置两个文本框分别用来显示随机生成的数组内元素和经排序后的元素;放置四个命令按钮
Command1生成随机字符串;
Command2生成随机数;
Command3执行升序排列;
Command4执行降序排列。
排序函数Paixu()应用于你的程序中时红色粗体的数字(维数)必须相同。
窗体Form1代码 Option Explicit
Dim shuzu(26) '原数组
Dim shuzu0(26) '排序后的数组Private Sub Command1_Click() '生成26个随机字串于数组shuzu()
Dim a As Integer
Text1.Text = "": Text2.Text = ""
Randomize (Timer)
For a = 1 To 26
shuzu(a) = Chr(65 + Int(Rnd(1) * 26))
Text1 = Text1 + Trim(shuzu(a)) + " "
Next
End SubPrivate Sub Command2_Click() '生成26个随机数字于数组shuzu()
Dim a As Integer
Text1.Text = "": Text2.Text = ""
Randomize (Timer)
For a = 1 To 26
shuzu(a) = 1 + Int(Rnd(1) * 100)
Text1 = Text1 + Trim(shuzu(a)) + " "
Next
End SubPrivate Sub Command3_Click() '调用自定义函数升序排列
Dim a As Integer
Text2.Text = ""
Call Trim(Paixu(shuzu(), shuzu0(), 26, True))
For a = 1 To 26
Text2.Text = Text2.Text + Trim(shuzu0(a)) + " "
Next
End SubPrivate Sub Command4_Click() '调用自定义函数降序排列
Dim a As Integer
Text2.Text = ""
Call Trim(Paixu(shuzu(), shuzu0(), 26, False))
For a = 1 To 26
Text2.Text = Text2.Text + Trim(shuzu0(a)) + " "
Next
End Sub
我得要写编码...不能偷懒的哦...
阿忠高手的代码太多了...我本来头就已经是大的了...
可不可以给个简单点的啊....而且我这里就一个"输入"的COMMAND按钮,按了以后就跳一个框框出来..在里面输入数字,按框框里的OK就把数字传到LISTBOX里面了...而且在LISTBOX里面显示出来的是要排好序的...输入数字的个数没有限制的....
帮帮我吧..这个练习做不出来,周末也不能按时下班回家了....呜呜呜~~~~~~~~~~~~~~~~
Call BubbleSort(mArray(), Order)
Call Insertion(mArray(), Order)
Call Bucket(mArray(), Order)
Call Selection(mArray(), Order)
Call ShellSort(mArray(), Order)
Call QuickSort(mArray(), 0, UBound(mArray))
Call Heap(mArray())
Option Explicit
Global Const ZERO = 0
Global Const ASCENDING_ORDER = 0
Global Const DESCENDING_ORDER = 1
Global gIterations
Sub BubbleSort(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim TEMP
Dim NextElement
NextElement = ZERO
Do While (NextElement < UBound(MyArray))
Index = UBound(MyArray)
Do While (Index > NextElement)
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Index -
1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index -
1)
MyArray(Index - 1) = TEMP
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Index -
1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Index -
1)
MyArray(Index - 1) = TEMP
End If
End If
Index = Index - 1
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Loop
End Sub
Sub Bucket(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim NextElement
Dim TheBucket
NextElement = LBound(MyArray) + 1
While (NextElement <= UBound(MyArray))
TheBucket = MyArray(NextElement)
Index = NextElement
Do
If Index > LBound(MyArray) Then
If nOrder = ASCENDING_ORDER Then
If TheBucket < MyArray(Index
- 1) Then
MyArray(Index) = MyArray(I
ndex - 1)
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If TheBucket >= MyArray(Index
- 1) Then
MyArray(Index) = MyArray(I
ndex - 1)
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
MyArray(Index) = TheBucket
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
End Sub
Sub Heap(MyArray())
Dim Index
Dim Size
Dim TEMP
Size = UBound(MyArray) Index = 1
While (Index <= Size)
Call HeapSiftup(MyArray(), Index)
Index = Index + 1
gIterations = gIterations + 1
Wend
Index = Size
While (Index > 0)
TEMP = MyArray(0)
MyArray(0) = MyArray(Index)
MyArray(Index) = TEMP
Call HeapSiftdown(MyArray(), Index - 1)
Index = Index - 1
gIterations = gIterations + 1
Wend
End Sub Sub HeapSiftdown(MyArray(), M)
Dim Index
Dim Parent
Dim TEMP
Index = 0
Parent = 2 * Index
Do While (Parent <= M) If (Parent < M And MyArray(Parent) < MyArray(Pa
rent + 1)) Then
Parent = Parent + 1
End If
If MyArray(Index) >= MyArray(Parent) Then
Exit Do
End If
TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP Index = Parent
Parent = 2 * Index
gIterations = gIterations + 1
Loop
End Sub
Sub HeapSiftup(MyArray(), M)
Dim Index
Dim Parent
Dim TEMP
Index = M
Do While (Index > 0)
Parent = Int(Index / 2)
If MyArray(Parent) >= MyArray(Index) Then
Exit Do
End If TEMP = MyArray(Index)
MyArray(Index) = MyArray(Parent)
MyArray(Parent) = TEMP
Index = Parent
gIterations = gIterations + 1
Loop End Sub
Sub Insertion(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim TEMP
Dim NextElement NextElement = LBound(MyArray) + 1
While (NextElement <= UBound(MyArray))
Index = NextElement
Do
If Index > LBound(MyArray) Then
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(In
dex - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(I
ndex - 1)
MyArray(Index - 1) = TEM
P
Index = Index - 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(I
ndex - 1) Then
TEMP = MyArray(Index)
MyArray(Index) = MyArray(I
ndex - 1)
MyArray(Index - 1) = TEM
P
Index = Index - 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
gIterations = gIterations + 1
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
End Sub
Sub QuickSort(MyArray(), L, R)
Dim I, J, X, Y
I = L
J = R
X = MyArray((L + R) / 2) While (I <= J)
While (MyArray(I) < X And I < R)
I = I + 1
Wend
While (X < MyArray(J) And J > L)
J = J - 1
Wend
If (I <= J) Then
Y = MyArray(I)
MyArray(I) = MyArray(J)
MyArray(J) = Y
I = I + 1
J = J - 1
End If
gIterations = gIterations + 1
Wend
If (L < J) Then Call QuickSort(MyArray(), L, J)
If (I < R) Then Call QuickSort(MyArray(), I, R)
End Sub Sub Selection(MyArray(), ByVal nOrder As Integer)
Dim Index
Dim Min
Dim NextElement
Dim TEMP
NextElement = 0
While (NextElement < UBound(MyArray))
Min = UBound(MyArray)
Index = Min - 1
While (Index >= NextElement)
If nOrder = ASCENDING_ORDER Then
If MyArray(Index) < MyArray(Min) ThenMin = Index
End If
ElseIf nOrder = DESCENDING_ORDER Then
If MyArray(Index) >= MyArray(Min) The
n
Min = Index
End If
End If
Index = Index - 1
gIterations = gIterations + 1
Wend
TEMP = MyArray(Min)
MyArray(Min) = MyArray(NextElement)
MyArray(NextElement) = TEMP
NextElement = NextElement + 1
gIterations = gIterations - 1
Wend
End Sub
Sub ShellSort(MyArray(), ByVal nOrder As Integer)
Dim Distance
Dim Size
Dim Index
Dim NextElement
Dim TEMP
Size = UBound(MyArray) - LBound(MyArray) + 1
Distance = 1
While (Distance <= Size)
Distance = 2 * Distance
Wend
Distance = (Distance / 2) - 1 While (Distance > 0) NextElement = LBound(MyArray) + Distance While (NextElement <= UBound(MyArray))
Index = NextElement
Do
If Index >= (LBound(MyArray) + Dista
nce) Then
If nOrder = ASCENDING_ORDER ThenIf MyArray(Index) < My
Array(Index - Distance) Then
TEMP = MyArray(Ind
ex)
MyArray(Index) = M
yArray(Index - Distance)
MyArray(Index - Di
stance) = TEMP
Index = Index -
Distance
gIterations = gIte
rations + 1
Else
Exit Do
End If
ElseIf nOrder = DESCENDING_ORDER
Then
If MyArray(Index) >= M
yArray(Index - Distance) Then
TEMP = MyArray(Ind
ex)
MyArray(Index) = M
yArray(Index - Distance)
MyArray(Index - Di
stance) = TEMP
Index = Index -
Distance
gIterations = gIte
rations + 1
Else
Exit Do
End If
End If
Else
Exit Do
End If
Loop
NextElement = NextElement + 1
gIterations = gIterations + 1
Wend
Distance = (Distance - 1) / 2
gIterations = gIterations + 1
Wend End Sub
以上代码保存于: SourceCode Explorer(源代码数据库)
复制时间: 2005-11-11 17:03:46
软件版本: 1.0.882
软件作者: Shawls
E-Mail: [email protected]
QQ: 9181729