看了你說遞歸的效率低。那么你可以不用的。
十年的都昌網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整都昌建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“都昌網(wǎng)站設(shè)計(jì)”,“都昌網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
給出的方法就是先生成第一個排列,然后每次調(diào)用下面的函數(shù)給出下一個排列,這樣生成的效率很高,這個函數(shù)可以內(nèi)聯(lián)。
這個是很經(jīng)典的排列組合算法啊?在網(wǎng)上能搜到一大堆。
大概是那種帶指向的移動的算法。我給你搜一個吧。
我找了幾個,這個是我覺得說的比較清楚的,你可以仔細(xì)參考一下,看不懂的話再搜點(diǎn)別的好了。。
全排列的算法跟這個不太一樣的。需要有點(diǎn)改動的。
至于語言的話,應(yīng)該不會有太大問題吧。。basic版的確實(shí)比較少,現(xiàn)在我也比較懶不想動手寫。。還是要靠你自己啦。
★生成排列的算法:
比如要生成5,4,3,2,1的全排列,首先找出一個最小的排列12345, 然后依次調(diào)用n!次STL算法中的next_permutation()即可輸出所有的全排列情況。所以這種算法的細(xì)節(jié)就是STL algorithm中next_permutation()的實(shí)現(xiàn)機(jī)制。詳細(xì)的實(shí)現(xiàn)代碼,大伙可以參考侯捷的《STL源代碼剖析》,在這里我只說一下我的理解:
1 首先從最尾端開始往前尋找兩個相鄰元素,令第一個元素為*i,第二個元素為*ii,且滿足*i*ii,找到這樣一組相鄰的元素后。
2 再從最尾端開始往前檢驗(yàn),找出第一個大于*i的元素,令為*k,將i,k元素對調(diào)。
3 再將ii及ii之后的所有元素顛倒排列,此即所求之"下一個"排列。
prev_permutation()算法的思路也基本相同,只不過它們尋找的"拐點(diǎn)"不同,在next_permutation()算法中尋找的是峰值拐點(diǎn),而在prev_permutation()算法中尋找的是谷值拐點(diǎn)。另外,在第二步中,prev_permutation()要找的是第一個小于*i的元素而不是第一個大于*i的元素。
具體例子,有空再舉,現(xiàn)在時(shí)間太晚了:)
★生成組合的算法:
如下面截圖所示,分全組合和r-組合兩種情況。
這里有一段核心代碼:
//--------------------------------------------------------
// Generate next combination (algorithm from Rosen p. 286)
//--------------------------------------------------------
public int[] getNext () {
if (numLeft.equals (total)) {
numLeft = numLeft.subtract (BigInteger.ONE);
return a;
}
int i = r - 1;
while (a[i] == n - r + i) {
i--;
}
a[i] = a[i] + 1;
for (int j = i + 1; j r; j++) {
a[j] = a[i] + j - i;
}
numLeft = numLeft.subtract (BigInteger.ONE);
return a; //這里返回的a數(shù)組,存儲的就是下標(biāo)的排列組合。
}
到這里,也許大伙會有一個疑問,假如要求的不是數(shù)字的排列組合,而是字符或字符串的排列組合呢?怎么辦?其實(shí)很簡單,你只要拿數(shù)組的下標(biāo)來做排列組合,返回他們下標(biāo)的排列組合,然后再到原數(shù)組中讀取字符串值,就可以輸出全部的排列組合結(jié)果。
如果你是從vb6剛過渡上vb。net,建議還是用冒泡排序法,容易理解。
如果你正努力學(xué)習(xí)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
先把strA排序,
ind = 2
if len(strA) = 0 then return
strB(1) = strA(1)
for each s in strA
if (strA(ind) strA(ind - 1) then
count = 0
strB(ind) = strA(ind)
else
strB(ind) = strA(ind - 1)
end if
ind = ind + 1
next s
vb語法忘了。。。大概是這么個意思吧。。。。 排序N LOG N,后面是線性的N,所以總共是NLOGN
加了單引號就是一個常量字符串了,對于每一行都是一樣的
像這種放在最前面的字段,order by 1 就可以了
網(wǎng)頁標(biāo)題:vb點(diǎn)虐
排序算法 vb 排序算法代碼
轉(zhuǎn)載注明:http://bm7419.com/article38/ddepopp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、建站公司、App設(shè)計(jì)、網(wǎng)站內(nèi)鏈、網(wǎng)站維護(hù)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)