求助!!请问对一维数组排序报溢出堆栈空间错误怎么修改?
我想对数组内的数据按大小进行排序,大约20万条,用了两种方法,都会报:“实时错误 28 ,溢出堆栈空间”错误,我不断减少数据数量,15万条,10万条,5万条,大约在5万条左右就可以正常排序,请问,如何对程序修改,才能使其在20万条左右不会发生错误?排序方法1(这个排序速度较快):
程序代码:
Private Sub Command1_Click() Call qsort(sj, 1, max_num) '对数组使用二分法 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 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
第二种方法(排序速度较慢):
程序代码:
Private Sub Command1_Click() l = max_num: S = 1: ReDim Num(l) For px_i = S To l Num(px_i) = sj(px_i) Next px_i Call QUICK_SORT(S, l, Num) End Sub '快速排序程序=========================================== Private Sub Exchange(ByRef n1 As Double, ByRef n2 As Double) Dim t As Double t = n1: n1 = n2: n2 = t 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)) 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)) Next j Call Exchange(a(i + 1), a(r)) 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
请版主大人和过往高手大人不吝赐教!感谢!