← Back to Home

Code-Alongs

Code-Along 1: Sorting Algorithms

Learning Objectives

  • Implement common sorting algorithms in Java
  • Analyze the time and space complexity of different sorting algorithms
  • Compare algorithm performance based on Big O notation
  • Understand how to choose the appropriate sorting algorithm for different scenarios

Overview

This code-along guides you through implementing and analyzing various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, and Quick Sort. You'll learn about their runtime complexity and practical applications while working through Java implementations.

Key Concepts

  • Bubble Sort: O(n²) time complexity - compares adjacent elements and swaps them if they're in the wrong order
  • Selection Sort: O(n²) time complexity - finds the minimum element and places it at the beginning
  • Insertion Sort: O(n²) time complexity but performs well on small or nearly sorted data
  • Quick Sort: O(n log n) average time complexity - a divide-and-conquer algorithm that uses a pivot element

Code Example

// Example of Bubble Sort implementation
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;
            }
        }
    }
}
                

Code-Along 2: Comparing Apples to Oranges

Learning Objectives

  • Design and implement the Comparable interface for custom classes
  • Create Comparator implementations for flexible object comparison
  • Use Collections.sort() with both Comparable objects and Comparators
  • Compare and contrast different approaches to sorting custom objects

Overview

This code-along demonstrates how to compare and sort different types of objects in Java. You'll implement both the Comparable interface for natural ordering and Comparator classes for custom comparison logic. The code-along uses fruit classes (like Apple and Orange) to illustrate how to meaningfully compare dissimilar objects.

Key Concepts

  • Natural Ordering: Implementing Comparable to define how objects of a class are naturally ordered
  • Custom Comparators: Creating separate comparison logic without modifying the original classes
  • Sort Contract: Understanding the consistency requirements between equals() and compareTo()
  • Chained Comparisons: Implementing multi-criteria sorting with Comparators

Code Example

// Implementing Comparable in a custom class
public class Apple implements Comparable<Apple> {
    private String variety;
    private double weight;
    private double price;
    
    // Constructor and getters/setters...
    
    @Override
    public int compareTo(Apple other) {
        // Compare apples based on price
        return Double.compare(this.price, other.price);
    }
}

// Creating a custom Comparator
public class FruitWeightComparator implements Comparator<Fruit> {
    @Override
    public int compare(Fruit f1, Fruit f2) {
        return Double.compare(f1.getWeight(), f2.getWeight());
    }
}