当前位置:编程学习 > C/C++ >>

c++有几种排序方法?

答案://对vbs不熟悉,里面很多代码写得比较差...
'快速排序
Option Explicit

Dim Result, I
Dim TestData(100)
const N = 100

Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next

Sub Swap(byRef Array, first, second)
Dim t
t = Array(first)
Array(first) = Array(second)
Array(second) = t
End Sub

'快速排序
Sub QSort(byRef Array, low, hi)
Dim i, j, p
While low < hi
p = Array(hi)
i = low - 1
For j = low To hi-1
If Array(j) <= p Then
i = i + 1
Swap Array, i, j
End If
Next
Swap Array, i+1, j
QSort Array, low, i
low = i + 2
Wend
End Sub

QSort TestData, 0, N - 1

For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next

MsgBox(Result)

'插入排序
Option Explicit

Dim Result, I
Dim TestData(100)
const N = 100

Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next

'插入排序
Sub ISort(byRef Array, low, hi)
Dim i, j, t
For i = low+1 To hi
t = Array(i)
j = i - 1
Do
If j < low Then
Exit Do
End If
If Array(j) > t Then
Array(j+1) = Array(j)
j = j - 1
Else
Exit Do
End If
Loop
Array(j+1) = t
Next
End Sub

ISort TestData, 0, N - 1

For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next

MsgBox(Result)


'堆排序
Option Explicit

Dim Result, I
Dim TestData(100)
const N = 100

Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next

'堆排序
Sub HSort(byRef Array, low, hi)
Dim i, t, j, p, l, r
For i = hi To low + 1 Step -1
j = i
p = Int((j-low+1)/2)+low-1
t = Array(j)
Do
If p = low-1 Then
Exit Do
End If
If t > Array(p) Then
Array(j) = Array(p)
j = p
p = Int((j-low+1)/2)+low-1
Else

Exit Do
End If
Loop
Array(j) = t
Next
For i = hi To low + 1 Step -1
t = Array(i)
Array(i) = Array(low)
j = low
Do
l = (j-low+1)*2+low-1
If l < i Then
r = (j-low+1)*2+low
If r < i Then
If Array(l) < Array(r) Then
l = r
End If
End If
If t < Array(l) Then
Array(j) = Array(l)
j = l
Else
Exit Do
End If
Else
Exit Do
End If
Loop
Array(j) = t
Next
End Sub

HSort TestData, 0, N - 1

For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next

MsgBox(Result)

'计数排序,基数排序
Option Explicit

Dim Result, I
Dim TestData(100)
const N = 100

Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next

'以base^pow位上的数映射到Map
Sub MapTo(byRef Array, byRef Map, low, hi, base, pow)
Dim i, n
n = 1
For i = 1 To pow
n = n * base
Next
For i = low To hi
Map(i) = Int(Array(i) / n) Mod base
Next
End Sub

'以映射进行计数排序
Sub CSortWithMap(byRef Array, byRef Map, low, hi)
Dim Counter(16), Temp(100), i
For i = 0 To uBound(Counter)
Counter(i) = 0
Next
For i = low To hi
Counter(Map(i)) = Counter(Map(i)) + 1
Next
For i = 1 To uBound(Counter)
Counter(i) = Counter(i) + Counter(i-1)
Next
For i = hi To low Step -1
Counter(Map(i)) = Counter(Map(i)) - 1
Temp(Counter(Map(i))) = Array(i)
Next
For i = low To hi
Array(i) = Temp(i)
Next
End Sub

'基数排序
Sub RSort(byRef Array, low, hi)
Dim Map(100), i
For i = 0 To 4
MapTo Array, Map, low, hi, 16, i
CSortWithMap Array, Map, low, hi
Next
End Sub

RSort TestData, 0, N - 1

For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next

MsgBox(Result)

'桶排序
Option Explicit

Dim Result, I
Dim TestData(100)
const N = 100

Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next

'桶排序
Sub BSort(byRef Array, low, hi)
Dim Bucket(400, 100), Counter(400), i, j, t, idx
For i = 0 To 399
Counter(i) = 0
Next
For i = low To hi
idx = Int(Array(i) / 100)
j = Counter(idx) - 1
t = Array(i)
Do
If j < 0 Then
Exit Do
End If
If Array(j) > t Then
Bucket(idx, j+1) = Bucket(idx, j)
j = j - 1
Else
Exit Do
End If
Loop
Bucket(idx, j+1) = t
Counter(idx) = Counter(idx) + 1
Next
t = low
For i = 0 To 399
For j = 1 To Counter(i)
Array(t) = Bucket(i, j-1)
t = t + 1
Next
Next
End Sub

BSort TestData, 0, N - 1

For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next

MsgBox(Result)

'选择,冒泡排序
Option Explicit

Dim Result, I
Dim TestData(100)
const N = 100

Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next

'选择排序
Sub SSort(byRef Array, low, hi)
Dim t, i, j, pos
For i = hi To low + 1 Step -1
pos = i
For j = i - 1 To low Step -1
If Array(pos) < Array(j) Then
pos = j
End IF
Next
t = Array(i)
Array(i) = Array(pos)
Array(pos) = t
Next
End Sub

'冒泡排序
Sub BubbleSort(byRef Array, low, hi)
Dim t, i, j
For i = hi-1 To low Step -1
For j = low To i
If Array(j) > Array(j+1) Then
t = Array(j)
Array(j) = Array(j+1)
Array(j+1) = t
End IF
Next
Next
End Sub

BubbleSort TestData, 0, N - 1

For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next

MsgBox(Result)

'希尔排序
Option Explicit

Dim Result, I
Dim TestData(100)
const N = 100

Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next

Sub SortWithStep(byRef Array, low, hi, stp)
Dim s, i, j, t
For s = 0 To stp-1
For i = s+low+stp To hi Step stp
t = Array(i)
j = i-stp
Do
If j < s+low Then
Exit Do
End If
If Array(j) > t Then
Array(j+stp) = Array(j)
j = j - stp
Else
Exit Do
End If
Loop
Array(j+stp) = t
Next
Next
End Sub

'希尔排序
Sub ShellSort(byRef Array, low, hi)
Dim Step
Step = Int((hi-low+1)/2) + 1
Do
SortWithStep Array, low, hi, Step
If Step < 1 Then
Exit Do
Else
Step = Int(Step/2)
End If
Loop
End Sub

ShellSort TestData, 0, N - 1

For I = 0 To N - 1
Result = Result & TestData(I) & VbTab
Next

MsgBox(Result)

'归并排序
Option Explicit

Dim Result, I
Dim TestData(100)
const N = 100

Randomize
For I = 0 To N - 1
TestData(I) = ROUND(RND() * 32768)
Next

Sub MergeTo(byRef Array, byRef Dest, low, mid, hi)
Dim i, j, k, d
i = low
j = mid
d = low
Do
If i = mid or j = hi+1 Then
Exit Do
End If
If Array(i) < Array(j) Then
Dest(d) = Array(i)
i = i + 1
Else
Dest(d) = Array(j)
j = j + 1
End If
d = d + 1
Loop
For k = i To mid - 1
Dest(d) = Array(k)
d = d + 1
Next
For k = j To hi
Dest(d) = Array(k)
d = d + 1
Next
End Sub

Sub Merge(byRef Array, byRef Temp, low, hi, length)
Dim i, j
for i = low To hi-length step length*2
j = i+2*length-1
If j > hi Then
j = hi
End If
MergeTo Array, Temp, i, i+length, j
next
For j = i To hi
Temp(j) = Array(j)
Next
End Sub

'归并排序,非递归
Sub MergeSort(byRef Array, low, hi)
Dim Temp(100), i, Length
Length = hi-low+1
i = 1
Do
Merge Array, Temp, low, hi, i
i = i * 2
Merge Temp, Array

上一个:DOS应该怎么学?还有C++
下一个:C++问题:struct结构体与类

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,