How to implement selection sort in Go
Sorting is a fundamental operation in computer science that involves arranging a collection of elements in a specific order. There are numerous sorting algorithms available, each with its own strengths and weaknesses. One such algorithm is selection sort, which though not the most efficient, provides a simple way to understand the sorting process and is often used as a teaching tool.
Understanding selection sort
Selection sort is an
The algorithm's key steps:
Find the minimum element in the unsorted portion.
Swap the found minimum element with the first element in the unsorted portion.
Expand the sorted portion by moving the boundary one element to the right.
While selection sort is easy to understand, it's not very efficient for large datasets due to its quadratic time complexity. Its worst-case and average-case
The space complexity of the selection sort algorithm is
Implementation in Go
Let's now implement the selection sort algorithm in Go. We'll assume that we are sorting a slice of integers in ascending order.
package mainimport "fmt"func selectionSort(arr []int) {n := len(arr)for i := 0; i < n-1; i++ {// Find the minimum element in the unsorted portionminIndex := ifor j := i + 1; j < n; j++ {if arr[j] < arr[minIndex] {minIndex = j}}// Swap the found minimum element with the first element in the unsorted portionarr[i], arr[minIndex] = arr[minIndex], arr[i]}}func main() {// Example unsorted sliceunsorted := []int{64, 34, 25, 12, 22, 11, 90}fmt.Println("Unsorted slice:", unsorted)// Apply Selection SortselectionSort(unsorted)fmt.Println("Sorted slice:", unsorted)}
Code explanation
Lines 8–19: This starts an outer loop that iterates through the elements of the input slice, up to the second-to-last element. This loop represents the process of expanding the sorted portion.
minIndex := i: Initializes the variableminIndexwith the current indexi. This index represents the smallest element found so far in the unsorted portion of the slice.
Lines 11–15: The
for j := i + 1; j < n; j++This nested loop iterates through the remaining unsorted portion of the slice, starting from the element after the current indexi.if arr[j] < arr[minIndex] {: Compares the value of the element at indexjwith the value of the current minimum element (arr[minIndex]). If the element at indexjis smaller, we updateminIndextoj, which means we've found a new minimum element in the unsorted portion.
Lines 15–18: The
arr[i], arr[minIndex] = arr[minIndex], arr[i]: After the inner loop completes, this line swaps the element at the indexiwith the minimum element found in the unsorted portion (arr[minIndex]). This effectively moves the smallest unsorted element to its correct position in the sorted portion.
Conclusion
While selection sort might not be the fastest sorting algorithm available, it serves as a valuable learning tool for understanding the sorting process. It showcases the importance of selecting the right algorithm for the task at hand, especially when dealing with larger datasets. In real-world scenarios, using more efficient algorithms like quick sort or merge sort is generally recommended for better performance.
Free Resources