Bubble sort in DSA is a basic Data Structure Algorithm, generally asked in almost all interviews. After the many interviews survey it found that almost 80% interviewee asks this bubble sort question. So in this tutorial we will learn about sorting an array or list using the bubble sort algorithms.
![Bubble Sort DSA](http://www.fluttertpoint.in/wp-content/uploads/2024/03/Orange-Modern-Minimal-Blog-Writing-YouTube-Thumbnail-1-1024x576.png)
We will sort an array by Bubble Sort with examples in multiple languages, so see the complete tutorial below.
What is Bubble sort Algorithm in DSA?
Problem: The problem is that you have to compare the adjustment element with each other and swipe them if require. Following are the steps considered while doing bubble sort using dart:
Steps for Bubble Sort in DSA
- Iterate over the list upto its length – 1.
- Again do iteration inside it upto length – i – 1, because an item already reduced from i position, so we will iterate for remaining items.
- Check if the j is greater than its next element (j + 1), if yes then swipe the number, swipe j and j+1 to each other.
- Here the for loops runs length-i-1, we are reducing the i element because when loops completed for the ith number that means current i number sorted and pushed at last in the list so donโt needed to sort them.
Dart:
void main(){
List<int> data = [23, 4, 1, 6, 8, 66];
int n = data.length;
for(int i = 0; i<n-1; i++){
for(int j = 0; j < n-i-1; j++){
if(data[j] > data[j+1]){
int temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
print(data);
}
//output:
[1, 4, 6, 8, 23, 66]
JavaScript:
- Iterate over the list upto its length – 1.
- Again do iteration inside it upto length – i – 1, because an item already reduced from i position, so we will iterate for remaining items.
- Check if the j is greater than its next element (j + 1), if yes then swipe the number, swipe j and j+1 to each other.
function bubbleSort(){
const data = [23, 4, 1, 6, 8, 66];
var n = data.length;
for(let i = 0; i<n-1; i++){
for(let j = 0; j < n-i-1; j++){
if(data[j] > data[j+1]){
var temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
}
console.log(data);
}
//call the function
bubbleSort();
//output
[ 1, 4, 6, 8, 23, 66 ]
Java:
- Iterate over the list upto its length – 1.
- Again do iteration inside it upto length – i – 1, because an item already reduced from i position, so we will iterate for remaining items.
- Check if the j is greater than its next element (j + 1), if yes then swipe the number, swipe j and j+1 to each other.
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] numbers = {64, 34, 25, 12, 22, 11, 90};
System.out.print("Original array: ");
printArray(numbers);
bubbleSort(numbers);
System.out.print("Sorted array: ");
printArray(numbers);
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
//output
Original array: 64 34 25 12 22 11 90
Sorted array: 11 12 22 25 34 64 90
Almost same approaches applied for all these languages. Its pretty simple.
Time Complexity:
The Time Complexity is O(N2) because there are two nested for loops.
Auxiliary Space:
The Auxiliary space is O(N) due the to only used the temp variable.
Thanks for reaching out us. You can have look into more tutorials. See more about the bubble sort from here.