Bubble Sort DSA | Full Explanation with Examples

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
Bubble Sort DSA

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:

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.

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.

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.

Donโ€™t miss new tips!

We donโ€™t spam! Read our [link]privacy policy[/link] for more info.

Leave a Comment

Translate ยป
Scroll to Top