Bubble Sort with Ballerina: A Step-by-Step Guide
Bubble Sort Using Ballerina:
Sorting algorithms are the fundamental concept of computer science. Bubble Sort is one of the most straightforward sorting algorithms to learn. In this blog, we’ll implement Bubble Sort using the Ballerina programming language.
What is Bubble Sort?:
Bubble Sort is the simplest sorting algorithm that repeatedly swaps adjacent elements if they’re not in the correct order. However, it’s not ideal for large data sets, as it has high average and worst-case time complexity.
Steps of Bubble Sort:
The key idea is to swap with the next element if they’re in the wrong order.
After each outer loop iteration, the largest element will settle into its correct position.
Implement this with a nested loop structure.
The outer loop runs from 0 to
arrayLength - 1
.The inner loop runs from 0 to
arrayLength - i - 2
.The outer loop helps count iterations, meaning each pass sorts one more element.
The inner loop compares adjacent elements, moving the larger one to the rightmost position in the unsorted section of the array.
Implementation of Bubble Sort in Ballerina:
First of all, set up a new ballerina project.
To do this, open a terminal and write the following command:
bal new bubble_sort_example
Open the
main.bal
file and write the code of bubble sort.Save the code and run the program using the following command in the terminal:
bal run
.
Code Snippet:
Following is the coding implementation of bubble sort:
import ballerina/io;
// Bubble Sort function
function bubbleSort(int[] arr) {
int arrayLength = arr.length();
// Traverse the whole array
foreach int i in 0 ..< arrayLength - 1 {
// Last i elements are already in place
foreach int j in 0 ..< arrayLength - i - 2 {
if arr[j] > arr[j + 1] {
// Swap if the elements are not in correct order
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public function main() {
int[] arr = [64, 34, 25, 12, 22, 11, 90];
// Call the bubbleSort
bubbleSort(arr);
io:println("Sorted Array Using Bubble Sort: ", arr);
}
Time and Space Complexity:
Time complexity: O(n^2)
Space complexity: O(1)
Subscribe to my newsletter
Read articles from Faria Karim directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Faria Karim
Faria Karim
Currently, I am working as an "Associate Software Engineer" at "Kaz Software". I have completed my graduation from North South University in the year 2020 with the highest distinction. I always love to explore new ideas. I am a curious and self-motivated person. So, I always try to keep learning and share my ideas with other people. I enjoy problem-solving a lot. I have solved around 100+ problems in various online judges and I am also a three-star coder at Hackerrank. Moreover, I have participated in the ACM ICPC two times. I have completed the off-sight round of ACM ICPC of 2018. I have some basic knowledge of c/c++, Java, Javascript, Php, and Python. I have done several projects using these programming languages. I have hands-on experience on various backend frameworks like Node.js, Django, Flask, and Laravel. I have also used React.js as the front-end library for a few of my projects. Currently, I am more into React.js. For web-based projects, I have worked on both MySQL and NoSQL (MongoDB) as the database system. Machine learning is my another matter of interest. I have research experience in the fuzzy system. I have done a few projects using Tensorflow, Keras, CNN, and LSTM. Recently I have grown my interest in game development. I have built two games. One is using construct 2 and the another one is in Unity 3D. Side by side, I am also trying to figure out the things on ionic. In my free time, I mostly like to surf the internet, watch movies, and also love to do animations and illustrations using Powerpoint.