注册 登录
编程论坛 VB6论坛

求教,对类似的两组数据分别进行大小排序,其中一组数据报错

ictest 发布于 2023-07-05 12:17, 1171 次点击
求教,有两组数据,数量都是近8000个,a组数据小数点后三位,数值无规律变化;b组数据,里面就两种数值,3.6和3.65,无规律交替出现。
只有本站会员才能查看附件,请 登录

运用了两种方法分别对两组数据分别进行大小排序,(b组数据虽然特殊,但是我想应该也能排得了序,最小值和最大值也就是这两个数),但两种排序方法对a组数据都是有效,对b组数据都会报错,错误一样:
只有本站会员才能查看附件,请 登录


附程序:
只有本站会员才能查看附件,请 登录


压缩包里还有个c文件,里面的数据情况和b组数据一样,排序也会报错。

请问这是什么原因呢?b组数据不能排序有什么问题吗?希望能给解惑一下。
4 回复
#2
ictest2023-07-07 15:02
请问,是不是我的程序里有什么缺陷吗?有没有哪位前辈高手能够不吝赐教啊?
#3
apull2023-07-08 00:23
If px(J) <= Temp Then px(I) = px(J): I = I + 1: Exit Do
这个的<=改成<
另一个也一样

程序代码:

Dim a_px(1 To 10000) As Double
Dim a_sj(1 To 10000) As Double
'Dim b_px(1 To 10000) As Double
'
Dim b_sj(1 To 10000) As Double

Dim hang As Long

Dim K() As String, Num() As Double, s As Long, l As Long, px_i As Long

sub do1(file$)
    Label2.Caption = ""
    Label3.Caption = ""
    Label4.Caption = ""

    hang = 0
    Open App.Path & file For Input As #1
    Do While Not EOF(1)
        Line Input #1, Temp
        hang = hang + 1
        a_sj(hang) = Temp
    Loop
    Close #1
   
    Call qsort(a_sj, 1, hang) '对数组使用二分法
    For px_i = 1 To hang
        a_px(px_i) = a_sj(px_i)
    Next px_i

    Label2.Caption = hang
    Label3.Caption = a_px(1)
    Label4.Caption = a_px(hang)
end sub


Private Sub Command1_Click()
    do1 "\a.txt"   
End Sub


Private Sub Command2_Click()
    do1 "\b.txt"
End Sub


'排序方法一

Sub qsort(px() As Double, ByVal kaishi As Long, ByVal jieshu As Long)
    Dim Temp As Double, J As Long, I As Long
    I = kaishi: J = jieshu '将i、j作为指针,从两侧向中部移动
    If kaishi < jieshu Then '控制是否进入循环
        Temp = px(kaishi) '将数组第一个值赋给temp,暂时充当对比量
        While I < J
            Do While I < J '指针j从右向左移动,当遇到比temp小的数时,将该值移动到指针i的位置,并使i向右移动一位
                'If px(J) <= Temp Then px(I) = px(J): I = I + 1: Exit Do
                If px(J) < Temp Then px(I) = px(J): I = I + 1: Exit Do
                J = J - 1
            Loop
            Do While I < J '指针i从左向右移动,当遇到比temp大的数时,将该值移动到指针j的位置,并使j向左移动一位
                If px(I) > Temp Then px(J) = px(I): J = J - 1: Exit Do
                I = I + 1
            Loop
        Wend
        px(I) = Temp
        Call qsort(px(), kaishi, I - 1) '递归二分法过程进行排序
        Call qsort(px(), I + 1, jieshu)
    Else
        Exit Sub
    End If
End Sub

sub do2(file$)
    Label2.Caption = ""
    Label3.Caption = ""
    Label4.Caption = ""

    hang = 0
    Open App.Path & file For Input As #1
    Do While Not EOF(1)
        Line Input #1, Temp
        hang = hang + 1
        a_sj(hang) = Temp
    Loop
    Close #1   
   
    l = hang: s = 1: ReDim Num(l)
    For px_i = s To l
        Num(px_i) = a_sj(px_i)
    Next px_i
    Call QUICK_SORT(s, l, Num)
    For px_i = s To l
        a_px(px_i) = Num(px_i)
    Next px_i   

    Label2.Caption = hang
    Label3.Caption = a_px(1)
    Label4.Caption = a_px(hang)

end sub


Private Sub Command3_Click()
    do2 "\a.txt"
End Sub

Private Sub Command4_Click()
    do2 "\b.txt"
End Sub

Private Sub Form_Load()
Label2.Caption = ""
Label3.Caption = ""
Label4.Caption = ""
End Sub


'排序方法二
Private Function PARTITION(ByVal p As Long, ByVal r As Long, ByRef a() As Double) As Long
    Dim X As Double, t As Long, I As Long, J As Long
    Randomize
    t = CLng((r - p) * Rnd + p)
    'Call Exchange(a(r), a(t))
    Dim tmp As Double
    tmp = a(r): a(r) = a(t): a(t) = tmp
    X = a(r): I = p - 1
    For J = p To r - 1
        'If a(J) <= X Then I = I + 1: Call Exchange(a(I), a(J))
        If a(J) < X Then
            I = I + 1
        else
            tmp = a(I): a(I) = a(J): a(J) = tmp
        end if
    Next J
    'Call Exchange(a(I + 1), a(r))
    tmp = a(I + 1): a(I + 1) = a(r): a(r) = tmp
    PARTITION = I + 1
End Function
Private Sub QUICK_SORT(ByVal p As Long, ByVal r As Long, ByRef a() As Double)
    If p < r Then
        Dim q As Long
        q = PARTITION(p, r, a)
        Call QUICK_SORT(p, q - 1, a)
        Call QUICK_SORT(q + 1, r, a)
    End If
End Sub


简单修改了下,递归特别费资源,慎用。

[此贴子已经被作者于2023-7-8 00:55编辑过]

#4
ictest2023-07-08 11:43
不得不服,大神就是大神,不但指明了错误,还教授了我一招,do1、do2,打死我也想不到这样写啊。看来vb6是汪洋大海,而我只是沙漠里的一个水分子。
#5
ezindo2023-07-19 14:27
日子一天天过去,讨厌的人会带着讨人厌的话离开,喜欢的人会带着美好的事到来[url]福彩3D[/url]

。把目光放在别处,洒脱还给自己。
1