The goal of Homework 11 is to implement a bubble sort function in ESM format that sorts an array of numbers in ascending order.
Specifically, the task requires:
-
Writing a function
bubbleSort(arr)that:- Sorts the array in place using the bubble sort algorithm.
- Optimizes by stopping early if the array is already sorted.
- Throws errors for invalid input types.
-
Using helper functions for array validation and swapping elements:
assertArray(arr)– ensures input is an array.swapElements(arr, i, j)– swaps two elements in an array.
-
Demonstrating the sorting in
homework.jsby:- Printing the original array.
- Sorting it.
- Printing the sorted array.
This project implements bubble sort in JavaScript with modular utilities:
bubbleSort– performs in-place bubble sorting with early exit optimization.array_funcs.js– provides helper functions for:- Array validation
- Index validation
- Printing arrays
- Swapping elements
Project structure:
./src/
├─ homework.js # Demonstration / main entry
├─ utils/
│ ├─ array_funcs.js # Helper functions for arrays
│ └─ bubble_sort.js # bubbleSort function
└─ index.html # HTML entry point for browser demo
The homework focuses on:
- Algorithm implementation – understanding bubble sort mechanics.
- In-place sorting – modifying arrays without extra memory.
- Input validation – ensuring the function handles incorrect types safely.
- ESM modularity – writing reusable, importable modules.
-
Initialization
bubbleSort(arr)first validates thatarris an array.- If the array length is less than 2, it returns immediately.
-
Bubble Sort Algorithm
- Iterates through array elements multiple times.
- Compares adjacent elements and swaps if necessary.
- Tracks if any swaps occurred in the pass.
- Stops early if no swaps are made (array is already sorted).
-
Helper Functions
swapElements(arr, i, j)– swaps two elements safely after validating indices.printArray(arr, inLine)– prints the array with a frame, optionally inline.
-
Demonstration
homework.jscreates an example array.- Prints the original array.
- Sorts it with
bubbleSort. - Prints the sorted array.
import { printArray } from "./utils/print_array.js";
import { bubbleSort } from "./utils/bubble_sort.js";
const arr = [9, 2, 4, 1, 5, 2, 9, 1, 2, 0];
console.log("Original array:");
printArray(arr, true);
bubbleSort(arr);
console.log("Sorted array:");
printArray(arr, true); // 0 1 1 2 2 2 4 5 9 9- Include
homework.jsas a module in HTML:
<script
type="module"
src="./homework.js"
></script>- Or import functions in another module:
import { bubbleSort } from "./utils/bubble_sort.js";
import { printArray } from "./utils/print_array.js";
const arr = [5, 3, 8, 1];
bubbleSort(arr);
printArray(arr, true); // 1 3 5 8- Modern browser or Node.js environment with ESM support.
- No external libraries required.
Status: ✅ Completed
- Bubble sort implemented with early exit optimization.
- Supports arrays of any length.
- Array and index validation included.
- Demonstration script prints original and sorted arrays.
MIT License
This project demonstrates basic sorting in JavaScript:
- Correct in-place bubble sort algorithm.
- Robust input validation.
- Modular ESM design.
- Easy to extend for other array utilities.
Made with ❤️ and JavaScript by Sam-Shepsl Malikin 🎓