🎓 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
Sorting an array in ascending order is a common operation in programming. It allows us to arrange the elements in increasing order, making it easier to search, compare, or present the data. In this blog post, we will explore a Kotlin program that efficiently sorts an array in ascending order. We will walk through the program step by step, explaining the logic behind it.
Kotlin Program: Sorting an Array in Ascending Order
To sort an array in ascending order in Kotlin, we can utilize a sorting algorithm called "Selection Sort." Let's dive into the code:fun selectionSort(array: IntArray) {
val n = array.size
for (i in 0 until n - 1) {
var minIndex = i
for (j in i + 1 until n) {
if (array[j] < array[minIndex]) {
minIndex = j
}
}
if (minIndex != i) {
val temp = array[i]
array[i] = array[minIndex]
array[minIndex] = temp
}
}
}
fun main() {
val numbers = intArrayOf(10, 5, 8, 20, 13)
println("Original Array: ${numbers.joinToString(", ")}")
selectionSort(numbers)
println("Sorted Array (Ascending Order): ${numbers.joinToString(", ")}")
}
Output:
Original Array: 10, 5, 8, 20, 13
Sorted Array (Ascending Order): 5, 8, 10, 13, 20Explanation:
The selectionSort() function takes an integer array as input and sorts it in ascending order.
We retrieve the size of the array using the size property and store it in the variable n.
We use a nested loop structure to perform the selection sort algorithm. The outer loop iterates from 0 to n - 1, representing the current position for selecting the minimum element.
Inside the outer loop, we initialize the minIndex variable to the current position.
The inner loop starts from i + 1 and iterates until n, comparing each element with the element at minIndex. If a smaller element is found, we update minIndex to the index of that element.
After completing the inner loop, we check if the minIndex is different from the current position i. If it is, we swap the elements at positions i and minIndex to place the minimum element in the correct position.
The outer loop continues until all elements are sorted, resulting in the array being sorted in ascending order.
In the main() function, we create a sample input array (val numbers = intArrayOf(10, 5, 8, 20, 13)) and print the original array to the console using println(). Then, we call the selectionSort() function to sort the array in ascending order and print the sorted array to the console.
Conclusion
In this blog post, we have discussed a Kotlin program that efficiently sorts an array in ascending order using the selection sort algorithm. Sorting an array is a fundamental operation in programming, and understanding this program equips you with the necessary skills to manipulate and organize data.
Feel free to explore and modify the code to suit your specific needs. Sorting algorithms are essential for various applications, such as data analysis, searching, and organizing information. Happy coding!
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