Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
In this tutorial, we’ll implement the bubble sort algorithm in Kotlin. Being one of the simplest algorithms, bubble sort repeatedly steps through a given list and compares its adjacent elements, and swaps them if they are arranged in the wrong order until the list is sorted. It’s among the most suitable algorithms for small datasets.
Let’s review the steps involved in performing a Bubble Sort:
Let’s see an example Bubble Sort implementation in Kotlin:
fun bubbleSort(arr: IntArray) {
val n = arr.size
for (i in 0 until n - 1) {
for (j in 0 until n - i - 1) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
val temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
}
This code sorts a list of elements and arranges them in ascending order. To use the bubbleSort() function, we can pass an IntArray with our unsorted elements. After the code compiles, the array will be sorted in ascending order.
Let’s see it in action:
val arr = intArrayOf(64, 34, 25, 12, 22, 11, 90)
val expected = intArrayOf(11, 12, 22, 25, 34, 64, 90)
bubbleSort(arr)
assertArrayEquals(expected, arr)
Let’s now use the same bubble sort algorithm to sort elements in a list in descending order:
fun bubbleSortDescending(arr: IntArray) {
val n = arr.size
for (i in 0 until n - 1) {
for (j in 0 until n - i - 1) {
if (arr[j] < arr[j + 1]) {
val temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
}
Bubble Sort is a simple comparison-based sorting algorithm. Let’s look at a few of its key characteristics:
In terms of time complexity, bubble sort can be analyzed as best-case time complexity O(n), average-case time complexity O(n^2), and worst-case time complexity O(n^2). This is because the normal bubble sort iterates through a list of elements and sorts them, as we discussed earlier. However, in an average and worst-case scenario, bubble sorting may require multiple passes to sort elements in a list.
Bubble sort has a space complexity of O(1) because it doesn’t need any additional data structures or memory allocation proportional to the input size. The sorting is usually done in place by just swapping the adjacent elements.
Bubble sort isn’t regarded as an adaptive sorting algorithm. This is because it doesn’t take advantage of any pre-existing order in the list. Regardless of the initial order, bubble sort performs the same number of comparisons and swaps for a given list size.
Bubble sort is often used as a teaching tool or for educational purposes due to its simplicity and ease of implementation. However, for practical applications, more efficient sorting algorithms such as quicksort or merge sort are preferred.
In this article, we’ve discussed the bubble sort algorithm in Kotlin. We went through the implementation of bubble sort for both the ascending and descending order and the analysis of bubble sort, including its cons.