Basic Sorting Algorithms in Dart: A Comprehensive Guide

Sorting is a fundamental concept in computer science and programming. In Dart, implementing basic sorting algorithms helps you understand how data can be arranged in a specific order. This guide covers several fundamental sorting algorithms and provides Dart implementations for each.

1. Bubble Sort

Bubble Sort is a simple comparison-based algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

Implementation:

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]) {
        // Swap
        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];
  bubbleSort(numbers);
  print('Sorted array: $numbers');
}

2. Selection Sort

Selection Sort is an in-place comparison-based algorithm. It divides the list into a sorted and an unsorted region, repeatedly selecting the smallest (or largest) element from the unsorted region and moving it to the end of the sorted region.

Implementation:

selectionSort(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;
      }
    }
    // Swap
    int temp = arr[minIndex];
    arr[minIndex] = arr[i];
    arr[i] = temp;
  }
}

void main() {
  List<int> numbers = [64, 34, 25, 12, 22, 11, 90];
  selectionSort(numbers);
  print('Sorted array: $numbers');
}

3. Insertion Sort

Insertion Sort builds the final sorted array one item at a time. It takes each element from the input data and finds the location it belongs to in the already sorted part of the array and inserts it there.

Implementation:

insertionSort(List<int> arr) {
  int n = arr.length;
  for (int i = 1; i < n; i++) {
    int key = arr[i];
    int j = i - 1;
    while (j >= 0 && arr[j] > key) {
      arr[j + 1] = arr[j];
      j--;
    }
    arr[j + 1] = key;
  }
}

void main() {
  List<int> numbers = [64, 34, 25, 12, 22, 11, 90];
  insertionSort(numbers);
  print('Sorted array: $numbers');
}

4. Merge Sort

Merge Sort is a divide-and-conquer algorithm that divides the array into halves, recursively sorts each half, and then merges the sorted halves. It has a time complexity of O(n log n).

Implementation:

mergeSort(List<int> arr) {
  if (arr.length <= 1) return;

  int middle = arr.length ~/ 2;
  List<int> left = arr.sublist(0, middle);
  List<int> right = arr.sublist(middle);

  mergeSort(left);
  mergeSort(right);

  int i = 0, j = 0, k = 0;
  while (i < left.length && j < right.length) {
    if (left[i] <= right[j]) {
      arr[k++] = left[i++];
    } else {
      arr[k++] = right[j++];
    }
  }
  while (i < left.length) {
    arr[k++] = left[i++];
  }
  while (j < right.length) {
    arr[k++] = right[j++];
  }
}

void main() {
  List<int> numbers = [64, 34, 25, 12, 22, 11, 90];
  mergeSort(numbers);
  print('Sorted array: $numbers');
}

5. Quick Sort

Quick Sort is a divide-and-conquer algorithm that selects a 'pivot' element and partitions the array around the pivot. Elements smaller than the pivot are moved to the left, and elements larger are moved to the right.

Implementation:

quickSort(List<int> arr, int low, int high) {
  if (low < high) {
    int pi = partition(arr, low, high);

    quickSort(arr, low, pi - 1);
    quickSort(arr, pi + 1, high);
  }
}

int partition(List<int> arr, int low, int high) {
  int pivot = arr[high];
  int i = (low - 1);

  for (int j = low; j < high; j++) {
    if (arr[j] < pivot) {
      i++;
      // Swap
      int temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
    }
  }

  // Swap
  int temp = arr[i + 1];
  arr[i + 1] = arr[high];
  arr[high] = temp;

  return i + 1;
}

void main() {
  List<int> numbers = [64, 34, 25, 12, 22, 11, 90];
  quickSort(numbers, 0, numbers.length - 1);
  print('Sorted array: $numbers');
}

Conclusion

Understanding and implementing basic sorting algorithms in Dart provides a solid foundation in algorithm design and analysis. Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort each have their strengths and weaknesses, making them suitable for different scenarios. By mastering these algorithms, you can enhance your problem-solving skills and optimize your code for various applications.

Did you find this article valuable?

Support Michael Piper by becoming a sponsor. Any amount is appreciated!