🎓 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
In this article, we will discuss working and implementation of Selection sort algorithm.
Selection Sort Overview
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving the unsorted array boundary by one element to the right.
This algorithm is not suitable for large datasets as its average and worst case complexities are of Ο(n2), where n is the number of items.
How Selection Sort Works?
Consider the following depicted integer array as an example.
For the first position in the sorted list, the whole list is scanned sequentially. The first position where 20 is stored presently, we search the whole list and find that 15 is the lowest value.
So we replace 20 with 15. After one iteration 15, which happens to be the minimum value in the list, appears in the first position of the sorted list.
For the second position, where 23 is residing, we start scanning the rest of the list in a linear manner.
We find that 20 is the second lowest value in the list and it should appear in the second place. We swap these values.
After two iterations, the two least values are positioned at the beginning in a sorted manner.
Now, scan the rest of the list in a linear manner and check third smallest value in the list 21 and it stayed in the same position.
Let's scan the rest of the list in a linear manner and find fourth smallest value in the list that 23 and replace 25 with 23.
Advantages
- Easy to implement
- In-place sort (requires no additional storage space)
Disadvantages
- Doesn’t scale well: O(n2)
This algorithm is called selection sort since it repeatedly selects the smallest element.
Let's implement Selection Sort algorithm using Java.
Selection Sort Algorithm Implementation in Java
import java.util.Arrays;
/**
* Class demonstrate the Selection sort algorithm
* @author Ramesh Fadatare
*
*/
public class SelectionSort {
void selectionSort(int arr[]) {
int size = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < size - 1; i++) {
// Find the minimum element in unsorted array
int minIndex = i;
for (int j = i + 1; j < size; j++)
if (arr[j] < arr[minIndex])
minIndex = j;
// Swap the found minimum element with the first
// element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
// Driver code to test above
public static void main(String args[]) {
SelectionSort selectionSortb = new SelectionSort();
int arr[] = { 20,10,5,6,2,3,4};
System.out.println("Before Sorting an array : " + Arrays.toString(arr));
selectionSortb.selectionSort(arr);
System.out.println("After Sorting an array : " + Arrays.toString(arr));
}
}
Output:
Before Sorting an array : [20, 10, 5, 6, 2, 3, 4]
After Sorting an array : [2, 3, 4, 5, 6, 10, 20]
Performance
- Worst case complexity: O(n2)
- Best case complexity: O(n2)
- Average case complexity: O(n2)
- Worst case space complexity: O(1) auxiliary
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
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment