急!!!!有谁能帮我设计一下这个VB程序。这是个“算法”中的“排序”问题。

来自:    更新日期:早些时候
请问一下,能帮我设计一下这个VB程序。这是个“算法”中的“排序”问题。~

Dim a(1 To 50) As Integer
Dim b(1 To 50) As Integer
Dim c(1 To 50) As Integer
Private Sub Command1_Click()
Dim j As Integer
Me.Text1.Text = "排序前数据" + vbCrLf
j = 1
While j <= 50 '产生随机数
a(j) = Int(100 * Rnd())
Me.Text1.Text = Me.Text1.Text + Mid(CStr(a(j)) + " ", 1, 4)
If j Mod 10 = 0 Then '满10个就要换行
Me.Text1.Text = Me.Text1.Text + vbCrLf
End If
j = j + 1
Wend '产生随机数 完毕
For j = 1 To 50
b(j) = a(j)
c(j) = a(j)
Next
End Sub

Private Sub Command2_Click()
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 49
For j = 1 To 49
If b(j) > b(j + 1) Then
k = b(j): b(j) = b(j + 1): b(j + 1) = k
End If
Next
Next
Me.Text1.Text = Me.Text1.Text + "冒泡排序后数据" + vbCrLf
For i = 1 To 50
Me.Text1.Text = Me.Text1.Text + Mid(CStr(b(i)) + " ", 1, 4)
If i Mod 10 = 0 Then
Me.Text1.Text = Me.Text1.Text + vbCrLf
End If
Next
End Sub

Private Sub Command3_Click()
Dim i As Integer, j As Integer, k As Integer
For i = 1 To 50
For j = i To 50
If c(i) < c(j) Then
k = c(i): c(i) = c(j): c(j) = k
End If
Next
Next
Me.Text1.Text = Me.Text1.Text + "选择排序后数据" + vbCrLf
For i = 1 To 50
Me.Text1.Text = Me.Text1.Text + Mid(CStr(c(i)) + " ", 1, 4)
If i Mod 10 = 0 Then
Me.Text1.Text = Me.Text1.Text + vbCrLf
End If
Next
End Sub

许多种算法,每个Sub是一种算法,前面的声明自己看,不是共有的


Option Explicit
' 'variables for the Quick sort iteration as the sub is recursive
Global QSCallCnt As Integer
Global QSSwaps As Integer
' 'variable for the Bubble sort as the sub can be aborted
Global Bcnt As Long
' 'variable for the Selection sort as the sub can be aborted
Global SScnt As Long
' 'used to abandon long sorts
Global SkipFlag As Integer

Public Sub BubbleSortNumbers(iArray As Variant)

Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lTemp As Long
frmSorts.lblIterations(0) = "Working..."

For lLoop1 = UBound(iArray) To LBound(iArray) Step -1


For lLoop2 = LBound(iArray) + 1 To lLoop1
If iArray(lLoop2 - 1) > iArray(lLoop2) Then
lTemp = iArray(lLoop2 - 1)
iArray(lLoop2 - 1) = iArray(lLoop2)
iArray(lLoop2) = lTemp

' '----------------------------------------------------
' 'Required for the speed Test; comment out for real use
' 'update the iterations label
Bcnt = Bcnt + 1
DoEvents
If SkipFlag% Then Exit Sub
' '----------------------------------------------------

End If
Next lLoop2

Next lLoop1

frmSorts.lblIterations(0) = "Elements swapped : " & Bcnt

End Sub


Public Sub SelectionSortNumbers(vArray As Variant)


Dim lLoop1 As Long
Dim lLoop2 As Long
Dim lMin As Long
Dim lTemp As Long
frmSorts.lblIterations(1) = "Working..."

For lLoop1 = LBound(vArray) To UBound(vArray) - 1
lMin = lLoop1

For lLoop2 = lLoop1 + 1 To UBound(vArray)

If vArray(lLoop2) < vArray(lMin) Then
lMin = lLoop2
' '----------------------------------------------------
' 'comment out for real use
' 'update the iterations label
SScnt = SScnt + 1
' '----------------------------------------------------
End If


' '----------------------------------------------------
' 'Required for the speed Test; comment out for real use

DoEvents

If SkipFlag% Then Exit Sub
' '----------------------------------------------------

Next lLoop2

lTemp = vArray(lMin)
vArray(lMin) = vArray(lLoop1)
vArray(lLoop1) = lTemp

Next lLoop1


frmSorts.lblIterations(1) = "Elements swapped : " & SScnt

End Sub


Public Sub ShellSortNumbers(vArray As Variant)

Dim lLoop1 As Long
Dim lHold As Long
Dim lHValue As Long
Dim lTemp As Long
Dim SHcnt As Integer
frmSorts.lblIterations(2) = "Working..."
lHValue = LBound(vArray)

Do
lHValue = 3 * lHValue + 1
Loop Until lHValue > UBound(vArray)



Do
lHValue = lHValue / 3

For lLoop1 = lHValue + LBound(vArray) To UBound(vArray)
lTemp = vArray(lLoop1)
lHold = lLoop1

Do While vArray(lHold - lHValue) > lTemp
vArray(lHold) = vArray(lHold - lHValue)
lHold = lHold - lHValue
' '----------------------------------------------------
' 'Required for the speed Test; comment out for real use
' 'update the iterations label
SHcnt = SHcnt + 1
DoEvents
' '----------------------------------------------------

If lHold < lHValue Then Exit Do
Loop

vArray(lHold) = lTemp
Next lLoop1


Loop Until lHValue = LBound(vArray)

frmSorts.lblIterations(2) = "Elements swapped : " & SHcnt
End Sub


Public Sub QuickSortNumbers(iArray As Variant, l&, r&)

' 'iArray() The iArray to sort
' 'l& First element of iArray to start sort
' 'r& Last element of iArray to start sort

' '----------------------------------------------------
' 'update the call count label ; comment out for real use
QSCallCnt = QSCallCnt + 1
' '----------------------------------------------------

Dim i&, j&
Dim X
Dim Y

i& = l&
j& = r&
X = iArray((l& + r&) / 2)


While (i& <= j&)


While (iArray(i&) < X And i& < r&)
i& = i& + 1
Wend


While (X l&)
j& = j& - 1
Wend


If (i& <= j&) Then
Y = iArray(i&)
iArray(i&) = iArray(j&)
iArray(j&) = Y
i& = i& + 1
j& = j& - 1

' '----------------------------------------------------
' 'update the swap count label ; comment out for real use
QSSwaps = QSSwaps + 1
' '----------------------------------------------------
End If


Wend



If (l& < j&) Then QuickSortNumbers iArray, l&, j&

If (i& < r&) Then QuickSortNumbers iArray, i&, r&

frmSorts.lblIterations(3) = "Sub was called : " & QSCallCnt & " times"
frmSorts.lblIterations(4) = "Elements Swapped : " & QSSwaps
End Sub

In the form, add the following code:
' 'general declarations
Option Explicit
' 'Used for the counter in the speed test
Dim tmrCounter As Long
' 'flag for the timer
Dim sortMethod As Integer

Private Sub cmdEnd_Click()

Unload Me
End Sub


Private Sub Form_Unload(Cancel As Integer)

Set Form1 = Nothing
End
End Sub


Private Sub cmdSkipBubbleSort_Click()

SkipFlag = True
End Sub


Private Sub cmdSkipSelectionSort_Click()

SkipFlag = True
End Sub


Private Sub cmdSort_Click(Index As Integer)

' 'The example here builds an Array of 15 elements and
' 'places random numbers into it. The string is then printed
' 'to screen. The array is passed to the procedure called
' 'BubbleSortNumbers in the project Module and it performs
'a Selection sort. Then redisplays the sorted elements to Screen.
'
Dim lMyArray(0 To 30) As Long
Dim iLoop As Integer
Dim sBuiltString As String
Randomize

For iLoop = LBound(lMyArray) To UBound(lMyArray)
lMyArray(iLoop) = Int(Rnd * 9) + 1
sBuiltString = sBuiltString & " " & lMyArray(iLoop)
Next iLoop

lblOriginElements = sBuiltString
sBuiltString = ""
Select Case Index
Case 0
Bcnt = 0
Call BubbleSortNumbers(lMyArray)
Case 1
Call SelectionSortNumbers(lMyArray)
Case 2
Call ShellSortNumbers(lMyArray)
Case 3
QSCallCnt = 0
Call QuickSortNumbers(lMyArray, 0, UBound(lMyArray))
End Select


For iLoop = LBound(lMyArray) To UBound(lMyArray)
sBuiltString = sBuiltString & " " & lMyArray(iLoop)
Next iLoop

lblSortedElements = sBuiltString
End Sub


Private Sub cmdSpeedTest_Click()

Dim lMyArray() As Long
ReDim lMyArray(0 To CLng(txtNumberOfElements - 1))
Dim i As Integer
Dim vTemp1 As Variant
Dim vTemp2 As Variant
Dim vTemp3 As Variant
Randomize
tmrCounter = 0
lblSpeedTestStatus.Caption = "Building Array of " & txtNumberOfElements & " Elements ........."

For i% = LBound(lMyArray) To UBound(lMyArray)
lMyArray(i%) = Int(Rnd * 100) + 1
Next i%

vTemp1 = lMyArray
vTemp2 = lMyArray
vTemp3 = lMyArray
Frame1.Enabled = False
'----------------------------------------------------------------
' -----------
SkipFlag% = False
cmdSkipBubbleSort.Enabled = True
sortMethod = 1
Bcnt = 0
frmSorts.timSpeedTest.Enabled = True
lblSpeedTestStatus.Caption = "Performing Bubble Sort ......"
Call BubbleSortNumbers(lMyArray)
lblSortTimeReport(0).Caption = "Bubble Sort Time Taken was : " & tmrCounter & " seconds"
timSpeedTest.Enabled = False
frmSorts.lblIterations(0) = "Elements swapped : " & Bcnt
tmrCounter = 0
cmdSkipBubbleSort.Enabled = False
'----------------------------------------------------------------
' -----------
SkipFlag% = False
cmdSkipSelectionSort.Enabled = True
sortMethod = 2
SScnt = 0
frmSorts.timSpeedTest.Enabled = True
lblSpeedTestStatus.Caption = "Performing Selection Sort ......"
Call SelectionSortNumbers(vTemp1)
lblSortTimeReport(1).Caption = "Selection Sort Time Taken was : " & tmrCounter & " seconds"
timSpeedTest.Enabled = False
frmSorts.lblIterations(1) = "Elements swapped : " & SScnt
tmrCounter = 0
cmdSkipSelectionSort.Enabled = False
'----------------------------------------------------------------
' -----------
sortMethod = 3
frmSorts.timSpeedTest.Enabled = True
lblSpeedTestStatus.Caption = "Performing Shell Sort ......"
Call ShellSortNumbers(vTemp2)
lblSortTimeReport(2).Caption = "Shell Sort Time Taken was : " & tmrCounter & " seconds"
timSpeedTest.Enabled = False
tmrCounter = 0
'----------------------------------------------------------------
' -----------
frmSorts.lblIterations(3) = "Working..."

sortMethod = 4
frmSorts.timSpeedTest.Enabled = True
lblSpeedTestStatus.Caption = "Performing Shell Sort ......"
Call QuickSortNumbers(vTemp3, 0, UBound(vTemp3))
lblSortTimeReport(3).Caption = "Quick Sort Time Taken was : " & tmrCounter & " seconds"
timSpeedTest.Enabled = False
lblSpeedTestStatus.Caption = "Completed Speed Test ......"
'----------------------------------------------------------------
' -----------
Frame1.Enabled = True
End Sub


Private Sub timSpeedTest_Timer()

tmrCounter = tmrCounter + 1

If sortMethod = 1 Then
lblSortTimeReport(0).Caption = _
"Bubble Sort Time Taken was : " & tmrCounter & " seconds"
End If


If sortMethod = 2 Then
lblSortTimeReport(1).Caption = _
"Selection Sort Time Taken was : " & tmrCounter & " seconds"
End If


If sortMethod = 3 Then
lblSortTimeReport(2).Caption = _
"Shell Sort Time Taken was : " & tmrCounter & " seconds"
End If


If sortMethod = 4 Then
lblSortTimeReport(2).Caption = _
"Quick Sort Time Taken was : " & tmrCounter & " seconds"
End If

End Sub

Function createRndNumbers(max As Long) As String
Dim i As Integer
For i = 0 To 49
createRndNumbers = createRndNumbers & CLng(max * Rnd(Timer) + 1) & IIf(i < 49, ",", "")
Next i
End Function

Function sort1(numbers As String) As String
Dim i As Integer
Dim j As Integer
Dim list As Variant
Dim tempNumber As Long
Dim str As String
list = Split(numbers, ",")
For i = 0 To UBound(list)
For j = 1 To UBound(list) - i
If CLng(list(j)) < CLng(list(j - 1)) Then
tempNumber = list(j - 1)
list(j - 1) = list(j)
list(j) = tempNumber
End If
Next j
Next i

tempNumber = 0
For i = 0 To UBound(list)
str = str & list(i) & IIf(i < 49, ",", "")
tempNumber = tempNumber + CLng(list(i))
Next i
sort1 = str
End Function

Function displayNumbers(numbers As String) As String
Dim list As Variant
Dim i As Integer
Dim text As String
list = Split(numbers, ",")
For i = 0 To UBound(list)
text = text & list(i) & vbTab & IIf(Right(i, 1) = 9, vbCrLf, "")
Next i
MsgBox text
End Function

核心算法函数写好了,你自己创建工程,画窗体按钮吧
另外没写选择降序排序函数,自己百度吧

授人以鱼,不如授人以渔。基本的排序法,不会真的不会做吧,那过级考试怎么过呀,建议你好好复习下课本。

Private Sub Command1_Click()
Dim i%, j%, s!(1 To 3), t!
For i = 1 To 3 '几个数改成几
s(i) = Val(InputBox("请输入第" & i & "个数:"))
Next

For i = 1 To 3
For j = i + 1 To 3
If s(j) < s(i) Then t = s(i): s(i) = s(j): s(j) = t
Next
Print s(i);
Next
End Sub



急!!!!有谁能帮我设计一下这个VB程序。这是个“算法”中的“排序”问题。视频

相关评论:
  • 18078386747谁能帮我设计一份宴会菜单啊(急!)?
    宁贱解菜单 一 驰名卤水拼 精美八味碟 盐水河虾 玫瑰牛肉 香菜腊鸭 葱油海蛰 剁椒皮蛋 药芹生仁 黄瓜沾酱 果汁山药 白灼基围虾 金斗鸵鸟松 脆皮一口香 一品烩鱼肚 椰汁西米露 菜胆衬羊方 八宝扣甲鱼 西兰扒海参 云吞老鸡煲 美极桂花鱼 百叶炒蒲芹 银丝香酥饼 雪影飘香 三鲜长寿面 时令水果盆 菜单...

  • 18078386747急,谁能帮我设计藏头诗?
    宁贱解华夏古风传医德,佗药济世堪楷模.转借圣手开良方,世人皆把孚命托.之于感慨如斯深,丁氏名医得其神.忠于生命诚为民,信义高崇众人尊.

  • 18078386747名字设计签名免费 谁能帮我设计下我的名字:黄仪明 谢谢了!
    宁贱解你好,以上图片是你要的签名,希望你喜欢 如果对我的回答满意,请点击我的回答下方选择满意回答按钮。谢谢~

  • 18078386747谁能帮我设计一下艺术签名,我的名字叫沈超(不是个性签名,是自己写名字...
    宁贱解恶心

  • 18078386747急!!!有谁能帮我设计一下这个VB程序。这是个“算法”中的“排序”问题...
    宁贱解Function createRndNumbers(max As Long) As String Dim i As Integer For i = 0 To 49 createRndNumbers = createRndNumbers & CLng(max * Rnd(Timer) + 1) & IIf(i < 49, ",", "")Next i End Function Function sort1(numbers As String) As String Dim i As Integer Dim j As ...

  • 18078386747免费设计自己的签名,请问谁能给我设计一下,我的姓名是:吴和淦,帮忙给...
    宁贱解免费设计自己的签名,请问谁能给我设计一下,我的姓名是:吴和淦,帮忙给设计一下了 吴和淦潇洒大气签 艺术签\/常用签\/连笔签 反书签 花体签\/明星签\/一笔签 公文签\/商务签

  • 18078386747谁能帮我设计一下签名,要很个性得连笔字 名字:王威风和刘书英 谢谢了...
    宁贱解谁能帮我设计一下签名,要很个性得连笔字 名字:王威风和刘书英 谢谢了!  我来答 1个回答 #热议# 你觉得同居会更容易让感情变淡吗? 旧人离Hrx 2013-05-16 · TA获得超过455个赞 知道答主 回答量:50 采纳率:0% 帮助的人:32.2万 我也去答题访问个人页 关注 展开全部 本回答由提问者...

  • 18078386747谁能帮我设计一个艺术签名?我的名字叫李小青!
    宁贱解李小青 签名设计已做好了 请查看下图 点击图片以后就会放大观看的 请另存为 再从你的电脑上打开图片 我推荐你选用最中间的艺术签 它又叫 一笔签 明星签 如果有邮箱的 请等待一会儿查看邮箱 我会用QQ邮箱发给你的 如果没有提邮箱的事 最好不要再补充 因为问题太多了 我可能没时间回头再看 ...

  • 18078386747帮我设计一个艺术签名
    宁贱解请帮我设计一个艺术签名,好看点的,名字我用信息发给你,要帮忙的话,先发信息给我,我在发名字给你..谢谢..名字王晓满...在线等... 请帮我设计一个艺术签名,好看点的,名字我用信息发给你,要帮忙的话,先发信息给我,我在发名字给你..谢谢..名字 王晓满...在线等. 展开  我来答 54个回答 #热议...

  • 18078386747谁能帮我设计一下签名 求一笔成的 名字《娄战军》谢谢了!!
    宁贱解我的 谁能帮我设计一下签名 求一笔成的 名字《娄战军》谢谢了!!  我来答 2个回答 #活动# 百度知道那些年,你见过的“奇妙”问答? 297272745 2013-10-13 · TA获得超过2118个赞 知道小有建树答主 回答量:1805 采纳率:0% 帮助的人:560万 我也去答题访问个人页 关注 展开全部 追问 有...

  • 相关主题精彩

    版权声明:本网站为非赢利性站点,内容来自于网络投稿和网络,若有相关事宜,请联系管理员

    Copyright © 喜物网