Sorting is a fundamental concept in programming that allows us to organize data in a structured and meaningful way. Whether you are working with numbers, strings, or complex objects, sorting helps improve the efficiency of searching and data analysis. Among the many sorting techniques, Bubble Sort is one of the simplest and most beginner-friendly algorithms to understand.

with hands-on learning.
get the skills and confidence to land your next move.
Bubble Sort works by repeatedly comparing adjacent elements in an array and swapping them if they are in the wrong order. This process continues until the array is fully sorted. While Bubble Sort is not the most efficient sorting algorithm for large datasets, it provides a clear introduction to basic programming concepts like loops, conditionals, and array manipulation. In Dart, implementing Bubble Sort is straightforward, making it a great exercise for beginners looking to strengthen their programming skills.
Program 1: Bubble Sort Using Simple Loops
This program demonstrates the classic approach to Bubble Sort using nested loops. It sorts an array of integers in ascending order.
void bubbleSort(List<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]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void main() {
List<int> numbers = [64, 34, 25, 12, 22, 11, 90];
print('Original Array: $numbers');
bubbleSort(numbers);
print('Sorted Array: $numbers');
}
In this approach, the outer loop controls the number of passes, while the inner loop compares adjacent elements and swaps them if needed. Beginners can easily see how elements “bubble” to their correct position with each pass.
Program 2: Optimized Bubble Sort
This version of Bubble Sort adds a small optimization: if during a pass no elements are swapped, the array is already sorted, and we can exit early.
void optimizedBubbleSort(List<int> arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) break;
}
}
void main() {
List<int> numbers = [5, 1, 4, 2, 8];
print('Original Array: $numbers');
optimizedBubbleSort(numbers);
print('Sorted Array: $numbers');
}
This optimization prevents unnecessary passes when the array is already sorted, making the algorithm slightly more efficient. Beginners can learn the importance of adding checks to improve performance without changing the overall logic.
Program 3: Bubble Sort in Descending Order
Sometimes, we may need to sort in descending order rather than ascending. This version reverses the comparison to achieve that.
void bubbleSortDescending(List<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]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void main() {
List<int> numbers = [10, 3, 15, 7, 8];
print('Original Array: $numbers');
bubbleSortDescending(numbers);
print('Sorted Array (Descending): $numbers');
}
By simply changing the comparison operator, we can reverse the sorting order. This is a great way for beginners to understand how small modifications can change algorithm behavior.
Program 4: Recursive Bubble Sort
Bubble Sort can also be implemented recursively. This approach provides an introduction to recursion while achieving the same sorting effect.
void recursiveBubbleSort(List<int> arr, int n) {
if (n == 1) return;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
recursiveBubbleSort(arr, n - 1);
}
void main() {
List<int> numbers = [64, 34, 25, 12, 22];
print('Original Array: $numbers');
recursiveBubbleSort(numbers, numbers.length);
print('Sorted Array: $numbers');
}
In this recursive version, the function repeatedly calls itself with a smaller portion of the array until the base case is reached. Beginners can see how recursion can replace loops while achieving the same logic.
Program 5: Bubble Sort with Custom Comparator
In some scenarios, we might want to sort based on custom rules, such as sorting by absolute values. This program demonstrates Bubble Sort using a comparator function.
void bubbleSortWithComparator(List<int> arr, bool Function(int, int) compare) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (compare(arr[j], arr[j + 1])) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
bool compareAbsolute(int a, int b) => a.abs() > b.abs();
void main() {
List<int> numbers = [-3, 1, -7, 4, 2];
print('Original Array: $numbers');
bubbleSortWithComparator(numbers, compareAbsolute);
print('Sorted by Absolute Values: $numbers');
}
This version introduces a flexible approach where the sorting logic is defined by a comparator function. Beginners can learn how to make algorithms more adaptable and reusable for different scenarios.
Frequently Asked Questions (FAQ)
Sorting is an essential concept, and here are some common questions beginners ask about Bubble Sort in Dart.
Q1: What is Bubble Sort?
Bubble Sort is a simple sorting algorithm that repeatedly compares and swaps adjacent elements until the array is sorted.
Q2: Is Bubble Sort efficient for large datasets?
Bubble Sort is not very efficient for large datasets because its time complexity is O(n²). Algorithms like Merge Sort or Quick Sort are better choices for large arrays.
Q3: Can Bubble Sort be used for strings in Dart?
Yes, Bubble Sort can be adapted to sort strings or other objects by comparing their values using the appropriate operators or comparator functions.
Q4: What is the main advantage of Bubble Sort?
The main advantage is its simplicity. It is easy to understand and implement, making it ideal for beginners learning basic sorting concepts.
Q5: How can Bubble Sort be optimized?
Adding a flag to detect if any swaps occurred in a pass allows the algorithm to exit early if the array is already sorted, improving efficiency.
Conclusion
Bubble Sort in Dart is an excellent starting point for beginners to understand how sorting algorithms work. It introduces fundamental programming concepts like loops, conditionals, recursion, and array manipulation. By exploring different variations — including optimized, descending, recursive, and comparator-based versions — learners can see how small changes can adapt an algorithm for different purposes.
Practicing Bubble Sort not only builds your confidence but also prepares you for more advanced sorting algorithms like Merge Sort, Quick Sort, and Heap Sort. Sorting is a core skill in programming, and mastering Bubble Sort is a step toward becoming a more versatile and confident developer.