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
分享標題:vb.net數組升序排列,對數組進行升序排序的函數
鏈接地址:http://m.kartarina.com/article20/hddsjo.html
成都網站建設公司_創新互聯,為您提供網站收錄、全網營銷推廣、軟件開發、域名注冊、網站設計公司、用戶體驗
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯