2 | 2 | 2 | 1 |
3 | 3 | 1 | 2 |
4 | 1 | 3 | 3 |
1 | 4 | 4 | 4 |
N + (N - 1) + (N - 2) + ... + 1 = N * (N + 1) / 2
Dim Counts() As Integer

ReDim Counts(min_item To max_item)
接下來,算法檢查列表中的每一個項目,并增加對應(yīng)該項目的數(shù)組元素的值,當(dāng)這一階段完成后,Counts(I)的數(shù)值就是就是數(shù)值為I的基礎(chǔ)上的個數(shù)。程序掃描Counts數(shù)組,將counts轉(zhuǎn)換成被排序列表中的偏移量。For I = min To MaxCounts(List(I)) = Counts(List(I)) + 1Next I
最后將數(shù)據(jù)進行排序next_spot = 1For i = min_value To max_valuethis_count = counts(i)counts(i) = next_spotnext_spot = next_spot + this_countNext i
下面是實現(xiàn)該算法的VB代碼
算法 | 優(yōu)點 | 缺點 |
---|---|---|
汽泡排序法 | 對以初步排序的數(shù)據(jù)來說這種方法的速度很快 | 在其它情況下運行速度較慢 |
選擇排序法 | 非常簡單 | 對大量數(shù)據(jù)的排序速度很慢 |
容易明白 | ||
對于少量數(shù)據(jù)的排序來說速度很快 | ||
快速排序法 | 對大量數(shù)據(jù)的排序來說速度很快 | 如果有大量重復(fù)的數(shù)據(jù)就比較麻煩 |
計數(shù)排序法 | 當(dāng)數(shù)據(jù)數(shù)值較小(1-1000之間)時,速度非???/td> | 當(dāng)數(shù)據(jù)數(shù)值較大時,速度較慢 |
需額外的內(nèi)存 | ||
只能對整數(shù)類型的數(shù)據(jù)排序 |