Sorting is one of the most common tasks in programming because organized data is easier to search, analyze, and manage. Whenever you see numbers arranged from smallest to largest or names ordered alphabetically, a sorting algorithm is working behind the scenes. Among all sorting techniques, Quick Sort stands out because of its speed and efficiency. It is widely used in real-world applications where performance matters.
with hands-on learning.
get the skills and confidence to land your next move.
Quick Sort works on a smart idea called “divide and conquer.” Instead of sorting the entire list at once, it selects a value called a pivot, places it in the correct position, and then sorts the elements on both sides of the pivot. In VB .NET, Quick Sort is an excellent algorithm for beginners to learn because it introduces recursion, logical thinking, and efficient problem solving. Even though the name sounds advanced, the concept becomes clear once you see it in action.
Program 1: Basic Quick Sort Using Recursion
This program shows the classic recursive implementation of Quick Sort in VB .NET. It uses the last element as the pivot and sorts the array step by step.
Module Module1
Sub QuickSort(arr() As Integer, low As Integer, high As Integer)
If low < high Then
Dim pi As Integer = Partition(arr, low, high)
QuickSort(arr, low, pi - 1)
QuickSort(arr, pi + 1, high)
End If
End Sub
Function Partition(arr() As Integer, low As Integer, high As Integer) As Integer
Dim pivot As Integer = arr(high)
Dim i As Integer = low - 1
For j As Integer = low To high - 1
If arr(j) <= pivot Then
i += 1
Swap(arr, i, j)
End If
Next
Swap(arr, i + 1, high)
Return i + 1
End Function
Sub Swap(arr() As Integer, i As Integer, j As Integer)
Dim temp As Integer = arr(i)
arr(i) = arr(j)
arr(j) = temp
End Sub
Sub Main()
Dim numbers() As Integer = {10, 7, 8, 9, 1, 5}
Dim num As Integer
QuickSort(numbers, 0, numbers.Length - 1)
Console.WriteLine("Sorted array:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis program works by selecting a pivot and placing it in its correct position. The elements smaller than the pivot go to the left, while larger ones move to the right. Beginners can understand this by focusing on how the array is gradually broken into smaller parts and sorted.
Program 2: Quick Sort with Display of Original and Sorted Array
This version prints the array before and after sorting, making it easier to visualize the result.
Module Module1
Sub QuickSort(arr() As Integer, low As Integer, high As Integer)
If low < high Then
Dim pi As Integer = Partition(arr, low, high)
QuickSort(arr, low, pi - 1)
QuickSort(arr, pi + 1, high)
End If
End Sub
Function Partition(arr() As Integer, low As Integer, high As Integer) As Integer
Dim pivot As Integer = arr(high)
Dim i As Integer = low - 1
For j As Integer = low To high - 1
If arr(j) <= pivot Then
i += 1
Dim temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next
Dim temp2 = arr(i + 1)
arr(i + 1) = arr(high)
arr(high) = temp2
Return i + 1
End Function
Sub Main()
Dim numbers() As Integer = {25, 17, 31, 13, 2}
Dim num As Integer
Console.WriteLine("Original array:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.WriteLine()
QuickSort(numbers, 0, numbers.Length - 1)
Console.WriteLine("Sorted array:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleBy displaying both arrays, beginners can clearly see how Quick Sort rearranges values. This makes learning more interactive and builds confidence.
Program 3: Quick Sort in Descending Order
This program sorts the array in descending order by adjusting the comparison logic.
Module Module1
Sub QuickSort(arr() As Integer, low As Integer, high As Integer)
If low < high Then
Dim pi As Integer = Partition(arr, low, high)
QuickSort(arr, low, pi - 1)
QuickSort(arr, pi + 1, high)
End If
End Sub
Function Partition(arr() As Integer, low As Integer, high As Integer) As Integer
Dim pivot As Integer = arr(high)
Dim i As Integer = low - 1
For j As Integer = low To high - 1
If arr(j) >= pivot Then
i += 1
Dim temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next
Dim temp2 = arr(i + 1)
arr(i + 1) = arr(high)
arr(high) = temp2
Return i + 1
End Function
Sub Main()
Dim numbers() As Integer = {4, 9, 1, 7, 3}
Dim num As Integer
QuickSort(numbers, 0, numbers.Length - 1)
Console.WriteLine("Sorted array in descending order:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis example helps beginners understand how flexible Quick Sort is. A small change in logic can completely change the sorting behavior.
Program 4: Quick Sort Using a Wrapper Procedure
This version wraps Quick Sort inside a reusable procedure, making the code cleaner.
Module Module1
Sub SortArray(arr() As Integer)
QuickSort(arr, 0, arr.Length - 1)
End Sub
Sub QuickSort(arr() As Integer, low As Integer, high As Integer)
If low < high Then
Dim pi As Integer = Partition(arr, low, high)
QuickSort(arr, low, pi - 1)
QuickSort(arr, pi + 1, high)
End If
End Sub
Function Partition(arr() As Integer, low As Integer, high As Integer) As Integer
Dim pivot As Integer = arr(high)
Dim i As Integer = low - 1
For j As Integer = low To high - 1
If arr(j) <= pivot Then
i += 1
Dim temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next
Dim temp2 = arr(i + 1)
arr(i + 1) = arr(high)
arr(high) = temp2
Return i + 1
End Function
Sub Main()
Dim numbers() As Integer = {11, 3, 15, 7, 8}
Dim num As Integer
SortArray(numbers)
Console.WriteLine("Sorted array:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis approach teaches beginners good programming habits by separating logic into reusable methods. It also makes the main program easier to read.
Program 5: Quick Sort with Negative Numbers
This program proves that Quick Sort works perfectly with both negative and positive numbers.
Module Module1
Sub QuickSort(arr() As Integer, low As Integer, high As Integer)
If low < high Then
Dim pi As Integer = Partition(arr, low, high)
QuickSort(arr, low, pi - 1)
QuickSort(arr, pi + 1, high)
End If
End Sub
Function Partition(arr() As Integer, low As Integer, high As Integer) As Integer
Dim pivot As Integer = arr(high)
Dim i As Integer = low - 1
For j As Integer = low To high - 1
If arr(j) <= pivot Then
i += 1
Dim temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next
Dim temp2 = arr(i + 1)
arr(i + 1) = arr(high)
arr(high) = temp2
Return i + 1
End Function
Sub Main()
Dim numbers() As Integer = {3, -2, 7, -5, 1}
Dim num As Integer
QuickSort(numbers, 0, numbers.Length - 1)
Console.WriteLine("Sorted array with negative numbers:")
For Each num In numbers
Console.Write(num & " ")
Next
Console.ReadLine()
End Sub
End ModuleThis example reassures beginners that Quick Sort is robust and works for real-world data that includes negative values.
Frequently Asked Questions (FAQ)
This section answers common beginner questions about Quick Sort in VB .NET.
Q1. Is Quick Sort faster than Merge Sort?
Quick Sort is usually faster in practice, but Merge Sort has more consistent performance.
Q2. Does Quick Sort use extra memory?
Quick Sort uses less extra memory than Merge Sort because it sorts in place.
Q3. Can Quick Sort handle negative numbers?
Yes, it works with negative, zero, and positive numbers without any changes.
Q4. Is Quick Sort stable?
No, Quick Sort is not a stable sorting algorithm by default.
Q5. Why is Quick Sort popular?
It is fast, efficient, and widely used in real-world applications.
Conclusion
Quick Sort is one of the most powerful and widely used sorting algorithms in programming. Learning how to implement Quick Sort in VB .NET helps beginners understand recursion, efficient sorting, and problem-solving techniques. Although it may look complex at first, breaking it into simple steps makes it easy to grasp.
By practicing these VB .NET Quick Sort programs, beginners can strengthen their understanding of algorithms and improve their coding skills. With regular practice, Quick Sort becomes a valuable tool that prepares learners for advanced programming and real-world software development.




