Selection Sort is a classic sorting algorithm that is simple to understand and implement. It works by repeatedly finding the minimum (or maximum) element from the unsorted portion of the list and placing it at the beginning (or end). While not the most efficient sorting method for large datasets, Selection Sort is an excellent way for beginners to learn basic sorting logic and iteration techniques in Dart.

with hands-on learning.
get the skills and confidence to land your next move.
Understanding Selection Sort also helps build a foundation for learning more advanced algorithms like Quick Sort or Merge Sort. Sorting is a common task in programming, whether you are organizing numbers, arranging strings alphabetically, or managing records in an app. Practicing Selection Sort in Dart allows beginners to see how loops, comparisons, and swapping elements work together in a clear and visual way.
Program 1: Basic Selection Sort Using Loops
This program demonstrates the straightforward approach to Selection Sort in Dart. It finds the smallest element in the unsorted part and swaps it with the first element of that section.
void selectionSortBasic(List<int> arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
void main() {
List<int> numbers = [64, 25, 12, 22, 11];
selectionSortBasic(numbers);
print("Sorted array: $numbers");
}
In this example, the outer loop moves the boundary of the unsorted subarray, while the inner loop searches for the minimum element. Swapping ensures the smallest value is placed in the correct position. Beginners can clearly visualize how the array gradually becomes sorted with each pass.
Program 2: Selection Sort in Descending Order
Changing the comparison operator allows sorting in descending order. This program swaps elements so the largest element moves to the front instead of the smallest.
void selectionSortDescending(List<int> arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
}
void main() {
List<int> numbers = [3, 7, 1, 9, 2];
selectionSortDescending(numbers);
print("Descending order: $numbers");
}
This variation introduces beginners to the concept of customizing sorting order, reinforcing how simple changes in comparison logic can produce different results.
Program 3: Selection Sort Using a Comparator Function
A comparator function allows sorting arrays based on custom criteria, such as absolute values. This makes the algorithm more flexible.
void selectionSortComparator(List<int> arr, int Function(int, int) compare) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int selectedIndex = i;
for (int j = i + 1; j < n; j++) {
if (compare(arr[j], arr[selectedIndex]) < 0) {
selectedIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[selectedIndex];
arr[selectedIndex] = temp;
}
}
int compareByAbsolute(int a, int b) => a.abs() - b.abs();
void main() {
List<int> numbers = [-10, 2, -30, 4, 5];
selectionSortComparator(numbers, compareByAbsolute);
print("Sorted by absolute value: $numbers");
}
Using a comparator function demonstrates to beginners how to extend sorting algorithms for different types of data or custom sorting logic without rewriting the entire algorithm.
Program 4: Recursive Selection Sort
Selection Sort can also be implemented recursively. Each recursive call finds the minimum element and moves it to the correct position.
void selectionSortRecursive(List<int> arr, [int start = 0]) {
int n = arr.length;
if (start >= n - 1) return;
int minIndex = start;
for (int i = start + 1; i < n; i++) {
if (arr[i] < arr[minIndex]) {
minIndex = i;
}
}
int temp = arr[start];
arr[start] = arr[minIndex];
arr[minIndex] = temp;
selectionSortRecursive(arr, start + 1);
}
void main() {
List<int> numbers = [29, 10, 14, 37, 13];
selectionSortRecursive(numbers);
print("Sorted array (recursive): $numbers");
}
This recursive approach helps beginners understand recursion by breaking down the problem into smaller subproblems while keeping the logic of Selection Sort intact.
Program 5: Selection Sort for Objects Using a Key Function
You can sort lists of objects or maps in Dart by defining a key function. This example sorts a list of maps by the “age” property.
void selectionSortByKey(List<Map<String, dynamic>> arr, int Function(Map) key) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (key(arr[j]) < key(arr[minIndex])) {
minIndex = j;
}
}
var temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
void main() {
List<Map<String, dynamic>> people = [
{'name': 'Edward', 'age': 25},
{'name': 'Samantha', 'age': 20},
{'name': 'Lucia', 'age': 30},
];
selectionSortByKey(people, (p) => p['age']);
print("Sorted by age: $people");
}
This version introduces beginners to working with complex data structures in Dart and shows how sorting can be customized using keys.
Frequently Asked Questions (FAQ)
Here are some common beginner questions about Selection Sort in Dart:
Q1: Is Selection Sort efficient for large datasets?
Selection Sort has a time complexity of O(n²), which is not efficient for large datasets. It is mainly educational and suitable for small lists.
Q2: Can Selection Sort handle custom objects?
Yes, by using a key function or comparator, you can sort custom objects, maps, or complex data structures.
Q3: Why use recursion if loops work fine?
Recursive Selection Sort helps beginners understand recursion and problem decomposition. It is mostly educational rather than practical.
Q4: Can Selection Sort sort in descending order?
Absolutely. By adjusting the comparison logic, you can sort arrays in ascending or descending order.
Q5: What is the advantage of using a comparator or key function?
It makes the algorithm flexible, allowing sorting based on different criteria without changing the core logic.
Conclusion
Selection Sort is an excellent starting point for beginners learning sorting algorithms in Dart. By exploring variations like descending order, comparator functions, recursion, and key-based sorting, learners gain a solid foundation in algorithmic thinking. Practicing these versions helps build confidence and prepares programmers for more advanced sorting techniques like Quick Sort or Merge Sort. The key is to experiment with different inputs, understand each step, and see how the algorithm transforms the list gradually.