VB.Net program to implement quicksort without using recursion

Here, we are going to learn how to implement quicksort without using recursion in VB.Net?
Submitted by Nidhi, on December 10, 2020 [Last updated : March 06, 2023]

Quicksort without using recursion

Here, we will create an array of integers and then read elements from the user. After we sort the array element using quicksort, here we will implement quick sort using iteration, and then print sorted array on the console screen.

Program/Source Code:

The source code to implement quicksort without using recursion is given below. The given program is compiled and executed successfully.

VB.Net code to implement quicksort without using recursion

'VB.Net program to implement quicksort without using recursion.

Module Module1
    Function Partition(ByVal arr() As Integer, ByVal l As Integer, ByVal h As Integer)

        Dim x As Integer = arr(h)
        Dim i As Integer = (l - 1)
        Dim t As Integer = 0

        For j = l To h - 1 Step 1
            If arr(j) <= x Then
                i = i + 1
                t = arr(i)
                arr(i) = arr(j)
                arr(j) = t

            End If
        Next

        t = arr(i + 1)
        arr(i + 1) = arr(h)
        arr(h) = t

        Partition = (i + 1)
    End Function


    Sub QuickSort(ByVal arr() As Integer, ByVal l As Integer, ByVal h As Integer)
        Dim stack As Integer() = New Integer(h - l + 1) {}
        Dim top As Integer = -1

        top = top + 1
        stack(top) = l

        top = top + 1
        stack(top) = h

        While (top >= 0)
            h = stack(top)
            top = top - 1

            l = stack(top)
            top = top - 1

            Dim p As Integer = Partition(arr, l, h)

            If (p - 1 > l) Then
                top = top + 1
                stack(top) = l

                top = top + 1
                stack(top) = p - 1
            End If

            If (p + 1 < h) Then
                top = top + 1
                stack(top) = p + 1

                top = top + 1
                stack(top) = h
            End If
        End While
    End Sub

    Sub Main()
        Dim arr As Integer() = New Integer(5) {}
        Console.WriteLine("Enter array elements: ")
        For i = 0 To 4 Step 1
            Console.Write("Element[{0}]: ", i)
            arr(i) = Integer.Parse(Console.ReadLine())
        Next

        QuickSort(arr, 0, 4)

        Console.WriteLine("Array element after sorting: ")
        For i = 0 To 4 Step 1
            Console.Write("{0} ", arr(i))
        Next
        Console.WriteLine()
    End Sub
End Module

Output

Enter array elements:
Element[0]: 34
Element[1]: 23
Element[2]: 56
Element[3]: 12
Element[4]: 66
Array element after sorting:
12 23 34 56 66
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a function Main(). In the Main() we created an array arr of five elements and we created a subroutine QuickSort() to the elements of the array. here we also created a function Partition() to divide the array into smaller sub-arrays.

VB.Net Array Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.