十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Dim i As Integer, j As Integer, X As Single, Y As Single, M As Single
你所需要的网站建设服务,我们均能行业靠前的水平为你提供.标准是产品质量的保证,主要从事网站设计制作、网站制作、企业网站建设、手机网站制作设计、网页设计、成都品牌网站建设、网页制作、做网站、建网站。创新互联拥有实力坚强的技术研发团队及素养的视觉设计专才。
i = L
j = R
'找出数组的中点
M = MyArray((L + R) / 2, 0)
While (i = j)
'找出比中点大的数
While (MyArray(i, 0) M And i R)
i = i + 1
Wend
'找出比中点小的数
While (M MyArray(j, 0) And j L)
j = j - 1
Wend
'互换这两个数
If (i = j) Then
X = MyArray(i, 0)
Y = MyArray(i, 1)
MyArray(i, 0) = MyArray(j, 0)
MyArray(i, 1) = MyArray(j, 1)
MyArray(j, 0) = X
MyArray(j, 1) = Y
i = i + 1
j = j - 1
End If
Wend
'未完成时递归调用
If (L j) Then Call QuickSort(MyArray(), L, j)
If (i R) Then Call QuickSort(MyArray(), i, R)
End Sub
如果你是从vb6刚过渡上vb。net,建议还是用冒泡排序法,容易理解。
如果你正努力学习vb。net的方法,推荐一个例子如下:
Imports System
Imports System.Collections
Public Class SamplesArray
Public Class myReverserClass
Implements IComparer
' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
Function Compare(x As Object, y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function 'IComparer.Compare
End Class 'myReverserClass
Public Shared Sub Main()
' Creates and initializes a new Array and a new custom comparer.
Dim myArr As [String]() = {"The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog"}
Dim myComparer = New myReverserClass()
' Displays the values of the Array.
Console.WriteLine("The Array initially contains the following values:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the default comparer.
Array.Sort(myArr, 1, 3)
Console.WriteLine("After sorting a section of the Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort(myArr, 1, 3, myComparer)
Console.WriteLine("After sorting a section of the Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the default comparer.
Array.Sort(myArr)
Console.WriteLine("After sorting the entire Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort(myArr, myComparer)
Console.WriteLine("After sorting the entire Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(myArr() As [String])
Dim i As Integer
For i = 0 To myArr.Length - 1
Console.WriteLine(" [{0}] : {1}", i, myArr(i))
Next i
Console.WriteLine()
End Sub 'PrintIndexAndValues
End Class 'SamplesArray
'This code produces the following output.
'
'The Array initially contains the following values:
' [0] : The
' [1] : QUICK
' [2] : BROWN
' [3] : FOX
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting a section of the Array using the default comparer:
' [0] : The
' [1] : BROWN
' [2] : FOX
' [3] : QUICK
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting a section of the Array using the reverse case-insensitive comparer:
' [0] : The
' [1] : QUICK
' [2] : FOX
' [3] : BROWN
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting the entire Array using the default comparer:
' [0] : BROWN
' [1] : dog
' [2] : FOX
' [3] : jumps
' [4] : lazy
' [5] : over
' [6] : QUICK
' [7] : the
' [8] : The
'
'After sorting the entire Array using the reverse case-insensitive comparer:
' [0] : the
' [1] : The
' [2] : QUICK
' [3] : over
' [4] : lazy
' [5] : jumps
' [6] : FOX
' [7] : dog
' [8] : BROWN
你直接传一个数组进去,而且是一个结构体数组,array.sort怎么知道根据结构中的哪一个属性进行排序?放一个c#的代码你看看,VB和C#很相似的
class Program
{
static void Main(string[] args)
{
People[] p = new People[3]
{
new People{name="张三"},
new People{name="李四"},
new People{name="张二名"}
};
//重点传一个实现了IComparer接口的类进去,告诉Array.Sort怎么排序
Array.Sort(p, new PeopleCompare());
foreach (var item in p)
{
Console.WriteLine(item.name);
}
Console.ReadKey();
}
}
//People结构体,换成类一样的
public struct People
{
public string name { get; set; }
}
//实现了IComparer接口的类
public class PeopleCompare : IComparer
{
public int Compare(object x, object y)
{
People p1 = (People)x ;
People p2 = (People)y;
return p1.name.CompareTo(p2.name);
}
}
Private Sub Command4_Click()
Dim t As clerk, i%, j%
For i = 0 To n - 1
For j = i To n - 2
If a(i).vc a(j + 1).vc Then
t = a(i): a(i) = a(j + 1): a(j + 1) = t
End If
Next j
Next i
Picture2.Cls
Picture2.Print "学号 姓名 VC VB"
Picture2.Print "---------------------------------------------"
For i = 0 To n - 1
Picture2.Print a(i).number, a(i).name, a(i).vc, a(i).vb
Next i
End Sub
扩展资料
vb数组排序思路:
1、冒泡排序法:
位置相邻两数进行两两比较,在比较时如果发现前面的数比后面的数大,则进行交换,都比较完一轮后,把最大一个数放到最后,如此进行下去即可完成冒泡排序。
2、比较交换法
假设第一个数最小,然后第一个数依次与后面的每个数都进行比较, 若比较时发现后面的数比第一个数小, 则两数位置进行交换, 全部都比较完算一轮,每一轮比较完后,第一个数是最小的数,如此进行即可完成比较排序。
3、选择排序
假设第一个数最小,接着记下最小数所在的位置,然后将最小数依次与后面的每一个数都进行比较,若比较时发现后面的数比最小的数还小,则修改最小数所在位置,全部都比较完算一轮。
每一轮比较完后,最小数所在的位置是否跟假设的是同一个位置,若不是,则最小数与第一个数进行交换位置,如此进行即可完成选择排序。
Dim AA(1 To 10) As Integer, ZGCJ(1 To 10) As Integer, ZDCJ(1 To 10) As Integer
在通用部分声明三个数组
Private Sub Command1_Click()
Text1.Text = "": Text2.Text = "": Text3.Text = ""
Text1.Text = "系统自动生成的十个数:" vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
Randomize
AA(I) = Int(Rnd * 90 + 10)
Text1.Text = Text1.Text AA(I) Space(4)
If I Mod 5 = 0 Then Text1.Text = Text1.Text vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I
End Sub
生成十个数的代码
Private Sub Command2_Click()
Text2.Text = "": Text3.Text = ""
Dim AAA As Integer, BBB As Integer
For I = 1 To 9
For J = I + 1 To 10
If ZGCJ(I) ZGCJ(J) Then
AAA = ZGCJ(I)
ZGCJ(I) = ZGCJ(J)
ZGCJ(J) = AAA
End If
If ZDCJ(J) ZDCJ(I) Then
BBB = ZDCJ(J)
ZDCJ(J) = ZDCJ(I)
ZDCJ(I) = BBB
End If
Next J
Next I
Text2.Text = Text2.Text "从大到小排列:" vbCrLf
For I = 1 To 10
Text2.Text = Text2.Text ZGCJ(I) Space(4)
If I Mod 5 = 0 Then Text2.Text = Text2.Text vbCrLf
Next I
Text3.Text = Text3.Text "从小到大排列:" vbCrLf
For I = 1 To 10
Text3.Text = Text3.Text ZDCJ(I) Space(4)
If I Mod 5 = 0 Then Text3.Text = Text3.Text vbCrLf
Next I
End Sub
排序的代码。
如果需要自己输入数字,可以这样:
'如果要自己输入数字,可以修改下面的代码
Text1.Text = "": Text2.Text = "": Text3.Text = ""
Text1.Text = "系统自动生成的十个数:" vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
Randomize
AA(I) = Int(Rnd * 90 + 10)
Text1.Text = Text1.Text AA(I) Space(4)
If I Mod 5 = 0 Then Text1.Text = Text1.Text vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I
'----------------------------修改为:
Text1.Text = "": Text2.Text = "": Text3.Text = ""
Text1.Text = "用户输入的十个数:" vbCrLf
For I = 1 To 10
AA(I) = 0: ZGCJ(I) = 0: ZDCJ(I) = 0
AA(I) = Val(InputBox("请输入第" I "个数!"))
Text1.Text = Text1.Text AA(I) Space(4)
If I Mod 5 = 0 Then Text1.Text = Text1.Text vbCrLf
ZGCJ(I) = AA(I)
ZDCJ(I) = AA(I)
Next I