🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Selection Sort is a simple comparison-based sorting algorithm. The main idea behind it is to divide the list into two parts: a sorted part and an unsorted part, then repeatedly pick the smallest (or largest, depending on sorting order) element from the unsorted sublist and move it to the end of the sorted sublist.
In this blog post, we will learn how to write a Go program to implement Selection Sort.
Program Steps
The main idea behind the Selection Sort is:
1. For each iteration, find the minimum (or maximum) from the unsorted section of an array.
2. Swap it with the leftmost unsorted element.
3. Move the boundary between the sorted and unsorted regions one element to the right.
Code Program
package main
import "fmt"
// Function for the Selection Sort Algorithm
func selectionSort(arr []int) {
length := len(arr)
for i := 0; i < length-1; i++ {
// Assume the min is the first number
minIdx := i
// Test against numbers after i to find the smallest number
for j := i+1; j < length; j++ {
if arr[j] < arr[minIdx] {
minIdx = j
}
}
// Swap the numbers.
arr[i], arr[minIdx] = arr[minIdx], arr[i]
}
}
func main() {
array := []int{64, 25, 12, 22, 11}
fmt.Println("Original Array:", array)
selectionSort(array)
fmt.Println("Sorted Array:", array)
}
Output:
Original Array: [64 25 12 22 11]
Sorted Array: [11 12 22 25 64]
Explanation
Initialization: The algorithm begins by assuming the first element (i.e., arr[0]) is the smallest.
Finding Minimum: For each i, the algorithm then scans through the array from i+1 to the end of the array to find the index minIdx of the smallest element.
Swapping: Once the index of the smallest (or largest, depending on sorting order) element is found, the algorithm swaps arr[i] with arr[minIdx].
Iteration: This process is repeated for each i from 0 to len(arr) - 2.
End Result: At the end of all iterations, the array becomes sorted in ascending order.
The primary advantage of Selection Sort is its simplicity. It performs well on smaller lists. However, for larger datasets, more efficient algorithms like Quick Sort, Merge Sort, or Built-in functions are recommended.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment