在VB中创建快速排序

时间:2018-03-28 08:34:15

标签: vb.net quicksort indexoutofrangeexception

我最近开始学习VB,我试图创建一个快速排序。但是,当我运行它时,它返回一个错误,我不确定错误在代码中的位置。

它返回的错误是

  

System.IndexOutOfRangeException:索引超出了范围   阵列。

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions

Namespace Rextester
    Public Module Program
        Public Sub Main(args() As string)
            Dim quicksort() as Integer = {3,5,9,2,1,6,7}
            Dim counter as Integer

            Console.WriteLine(sort(quicksort, quicksort.length))
        End Sub

        Public Function sort(arr() as Integer, arrLen as Integer)as Integer()
            If arrLen <= 2 Then Return arr
            Dim more(arr.length) as Integer
            Dim less(arr.length) as Integer
            Dim i, l, m as Integer
            l = 0: m = 0
            Dim pivot as Integer = arr(0)

            For i = 1 to (arr.length-1)
                If arr(i) < arr(pivot)
                    Array.Resize(less, less.length+1)
                    less(l)=arr(i)
                    l = l + 1
                End If
                If arr(i) > arr(pivot)
                    more(m)=arr(i)
                    m = m + 1
                End If
           Next i 

           Dim moreVar = sort(more, m)
           Dim lessVar = sort(less, l)
        End Function   
    End Module
End Namespace

0 个答案:

没有答案