Below are the detailed steps to implement QuickSort on a 1-dimensional array of values in a generic computer programming language, followed by sample implementations in QB64 BASIC, FreeBASIC, and PureBASIC. QuickSort is a divide-and-conquer algorithm that works by selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. The sub-arrays are then recursively sorted.
Generic Steps for QuickSort Implementation:
1. Choose a Pivot
Select an element from the array to serve as the pivot. Common choices are the first element, the last element, the middle element, or a random element. The choice of pivot can affect performance, especially for already sorted or nearly sorted lists.
2. Partition
Rearrange the array so that all elements less than the pivot are to its left, and all elements greater are to its right. This step also returns the final position of the pivot.
3. Recursively Sort Subarrays
Apply the QuickSort algorithm to the subarray of elements less than the pivot and to the subarray of elements greater than the pivot.
Detailed Step-by-Step QuickSort Algorithm:
- Input: An array
A of length n
- Output: Array
A sorted in ascending order
-
Base Case:
- If
n ≤ 1, return A (since it's already sorted).
- If the array is empty, return immediately as there's nothing to sort.
-
Select Pivot:
- Choose a pivot element from
A. For simplicity, let's use the first element A[0].
-
Partition:
- Initialize indices:
i = 1, j = 1, and pivot = A[0].
- Loop from
j = 1 to n-1:
- If
A[j] < pivot, then swap A[i] and A[j], and increment i.
- Increment
j.
- Swap
A[0] (the pivot) with A[i-1]. The pivot is now in its final sorted position.
- Return the final index of the pivot,
i-1.
-
Recursive Calls:
- QuickSort on the subarray
A[0..(i-2)] (elements before the pivot).
- QuickSort on the subarray
A[i..(n-1)] (elements after the pivot).
-
Return Sorted Array:
- After recursive calls,
A is sorted.
Sample Implementations:
QB64 BASIC
REM QuickSort in QB64 BASIC
SUB QuickSort(A() AS INTEGER, ByVal first AS INTEGER, ByVal last AS INTEGER)
' Base case: If the subarray has 1 or 0 elements, it's already sorted
IF first >= last THEN EXIT SUB
' Partition the array and get the pivot index
pivotIndex = Partition(A(), first, last)
' Recursively sort the subarrays on either side of the pivot
QuickSort A(), first, pivotIndex - 1
QuickSort A(), pivotIndex + 1, last
END SUB
FUNCTION Partition(A() AS INTEGER, ByVal first AS INTEGER, ByVal last AS INTEGER) AS INTEGER
' Choose the first element as the pivot
pivot = A(first)
i = first + 1
j = first + 1
' Partition the array around the pivot
WHILE j <= last
IF A(j) < pivot THEN
SWAP A(i), A(j)
i = i + 1
END IF
j = j + 1
WEND
' Place the pivot in its final sorted position
SWAP A(first), A(i - 1)
Partition = i - 1
END FUNCTION
DIM myArray(9) AS INTEGER
myArray(0) = 5
myArray(1) = 2
myArray(2) = 9
myArray(3) = 1
myArray(4) = 7
myArray(5) = 3
myArray(6) = 6
myArray(7) = 8
myArray(8) = 4
myArray(9) = 0
' Sort the array using QuickSort
QuickSort myArray(), 0, UBOUND(myArray)
' Print the sorted array
FOR i = 0 TO UBOUND(myArray)
PRINT myArray(i);
NEXT i
FreeBASIC
' QuickSort in FreeBASIC
Sub QuickSort(ByRef A() As Integer, ByVal first As Integer, ByVal last As Integer)
' Base case: If the subarray has 1 or 0 elements, it's already sorted
If first >= last Then Return
' Partition the array and get the pivot index
Dim pivotIndex As Integer = Partition(A(), first, last)
' Recursively sort the subarrays on either side of the pivot
QuickSort A(), first, pivotIndex - 1
QuickSort A(), pivotIndex + 1, last
End Sub
Function Partition(ByRef A() As Integer, ByVal first As Integer, ByVal last As Integer) As Integer
' Choose the first element as the pivot
Dim pivot As Integer = A(first)
Dim i As Integer = first + 1
Dim j As Integer = first + 1
' Partition the array around the pivot
While j <= last
If A(j) < pivot Then
Swap A(i), A(j)
i += 1
End If
j += 1
Wend
' Place the pivot in its final sorted position
Swap A(first), A(i - 1)
Return i - 1
End Function
Dim As Integer myArray(0 To 9) = {5, 2, 9, 1, 7, 3, 6, 8, 4, 0}
' Sort the array using QuickSort
QuickSort(myArray(), LBound(myArray), UBound(myArray))
' Print the sorted array
For i As Integer = LBound(myArray) To UBound(myArray)
Print myArray(i);
Next i
PureBASIC (Pure BASIC)
; QuickSort in PureBASIC
Procedure QuickSort(Array A(), First, Last)
; Base case: If the subarray has 1 or 0 elements, it's already sorted
If First >= Last
ProcedureReturn
EndIf
; Partition the array and get the pivot index
PivotIndex = Partition(Array A(), First, Last)
; Recursively sort the subarrays on either side of the pivot
QuickSort(Array A(), First, PivotIndex - 1)
QuickSort(Array A(), PivotIndex + 1, Last)
EndProcedure
Procedure Partition(Array A(), First, Last)
; Choose the first element as the pivot
Protected Pivot = A(First)
Protected i = First + 1
Protected j = First + 1
; Partition the array around the pivot
While j <= Last
If A(j) < Pivot
Swap A(i), A(j)
i + 1
EndIf
j + 1
Wend
; Place the pivot in its final sorted position
Swap A(First), A(i - 1)
ProcedureReturn i - 1
EndProcedure
NewList myArray.i()
AddElement(myArray())
For i = 0 To 9
myArray() = i
Next i
myArray(0) = 5
myArray(1) = 2
myArray(2) = 9
myArray(3) = 1
myArray(4) = 7
myArray(5) = 3
myArray(6) = 6
myArray(7) = 8
myArray(8) = 4
myArray(9) = 0
; Sort the array using QuickSort
QuickSort(myArray(), 0, ListSize(myArray()) - 1)
; Print the sorted array
For i = 0 To ListSize(myArray()) - 1
Print(Str(myArray(i)))
Next i
Note:
- PureBASIC uses dynamic lists, so the array is defined and populated differently compared to QB64 BASIC and FreeBASIC.
- Swap is used implicitly in QB64 BASIC and FreeBASIC but explicitly defined or handled differently in PureBASIC examples due to language differences.
- Error handling and edge cases (e.g., handling an empty array) are not included for brevity but should be considered in a production environment. For instance, you could add checks at the beginning of the
QuickSort procedure to return immediately if the array is empty or if first >= last, ensuring the function works correctly for these edge cases. Additionally, duplicate elements are handled naturally by the QuickSort algorithm as it partitions elements relative to the pivot, so no special handling is required for duplicates.