Skip to content

malikinss/telran-frontend-11

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Homework 11 – Bubble Sort

Task Definition

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:

  1. 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.
  2. 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.
  3. Demonstrating the sorting in homework.js by:

    • Printing the original array.
    • Sorting it.
    • Printing the sorted array.

📝 Description

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


🎯 Purpose

The homework focuses on:

  1. Algorithm implementation – understanding bubble sort mechanics.
  2. In-place sorting – modifying arrays without extra memory.
  3. Input validation – ensuring the function handles incorrect types safely.
  4. ESM modularity – writing reusable, importable modules.

🔍 How It Works

  1. Initialization

    • bubbleSort(arr) first validates that arr is an array.
    • If the array length is less than 2, it returns immediately.
  2. 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).
  3. Helper Functions

    • swapElements(arr, i, j) – swaps two elements safely after validating indices.
    • printArray(arr, inLine) – prints the array with a frame, optionally inline.
  4. Demonstration

    • homework.js creates an example array.
    • Prints the original array.
    • Sorts it with bubbleSort.
    • Prints the sorted array.

📜 Output Example

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

📦 Usage

  1. Include homework.js as a module in HTML:
<script
	type="module"
	src="./homework.js"
></script>
  1. 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

✅ Dependencies

  • Modern browser or Node.js environment with ESM support.
  • No external libraries required.

📊 Project Status

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.

📄 License

MIT License


🧮 Conclusion

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 🎓

About

JavaScript implementation of in-place bubble sort using ESM modules, with robust input validation and helper functions for array handling and element swapping. Includes demonstration scripts for printing original and sorted arrays, optimized for early exit when the array is already sorted.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors