Quick Sort Recursive

Quick sort is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order.



									Public Shared Sub QuickSortRecursive(ByRef data As Integer(), left As Integer, right As Integer)
	If left < right Then
		Dim q As Integer = Partition(data, left, right)
		QuickSortRecursive(data, left, q - 1)
		QuickSortRecursive(data, q + 1, right)
	End If
End Sub

Private Shared Function Partition(ByRef data As Integer(), left As Integer, right As Integer) As Integer
	Dim pivot As Integer = data(right)
	Dim temp As Integer
	Dim i As Integer = left

	For j As Integer = left To right - 1
		If data(j) <= pivot Then
			temp = data(j)
			data(j) = data(i)
			data(i) = temp
			i += 1
		End If
	Next

	data(right) = data(i)
	data(i) = pivot

	Return i
End Function
								


Example

									Dim data() As Integer = { -1, 25, -58964, 8547, -119, 0, 78596 }
QuickSortRecursive(data, 0, data.Length - 1)
								


Output

									-58964
-119
-1
0
25
8547
78596