Recursion and Quick Sort
In this post, I can explain two important concepts in computer science: recursion and quick sort. I need to explain recursion before I can explain quick sort because quick sort use recursion to sort the array.
Recursion
Recursion is a technique in computer science where a function calls itself. This technique is used to solve problems that can be broken down into smaller problems of the same type.
Here is an example of a recursive function that calculates the factorial of a number:
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}
In this example, the factorial
function calls itself with a smaller number until the base case is reached.
The base case is when n
is equal to 0, in which case the function returns 1.
It is important to reinforce the base case, without it we would have an infinite loop, and you can test it with the code below
func factorial(n int) int {
return n * factorial(n-1)
}
You can use ctrl+c to cancel.
Quick Sort
Anyway, now let’s talk about quick sort.
Quick sort is a sorting algorithm that uses recursion to sort an array. Quick sort use the Divide and Conquer strategy to sort the array. But what would be Divide and Conquer strategy?
Divide and Conquer is a technique in computer science where a problem is divided into smaller problems of the same type.
For example, let’s say we have an array:
arr := []int{100, 5, 3, 6, 2, 10}
First, we get the pivot element, which is the first element in the array:
pivot := arr[0]
With the pivot element, we divide the array into two arrays: one with elements smaller than the pivot and another with elements greater than the pivot:
var left []int
var right []int
for i := 1; i < len(arr); i++ {
if arr[i] < pivot {
left = append(left, arr[i])
} else {
right = append(right, arr[i])
}
}
And here, we use recursion to sort the left and right arrays:
left = quickSort(left)
right = quickSort(right)
Finally, we concatenate the left array, the pivot element, and the right array:
return append(append(left, pivot), right...)
I purposely left it for last to say that we cannot forget the base case.
The base case is when the array has only one element, in which case we return the array:
if len(arr) < 2 {
return arr
}
When the array has only one element, we return the array because it is already sorted.
Now we can create the quickSort
function:
func quickSort(arr []int) []int {
if len(arr) < 2 {
return arr
}
pivot := arr[0]
var left []int
var right []int
for i := 1; i < len(arr); i++ {
if arr[i] < pivot {
left = append(left, arr[i])
} else {
right = append(right, arr[i])
}
}
left = quickSort(left)
right = quickSort(right)
return append(append(left, pivot), right...)
}
Now we can test the function:
func main() {
arr := []int{100, 5, 3, 6, 2, 10}
fmt.Println(quickSort(arr))
}
When you run the code above, you will see the sorted array:
[2 3 5 6 10 100]
That’s it! I hope you enjoyed this post. If you have any questions, please let me know in the comments below.
Conclusion
In this post, I explained two important concepts in computer science: recursion and quick sort. I hope I managed to explain it in a simple way, so that most people can understand, especially those who have difficulty or even fear of algorithms!
Hugs.