<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Genix</title>
    <description>The latest articles on Forem by Genix (@m__mdy__m).</description>
    <link>https://forem.com/m__mdy__m</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1229085%2F75cfc1d1-dd3f-4bc4-b477-f74958b0128a.jpg</url>
      <title>Forem: Genix</title>
      <link>https://forem.com/m__mdy__m</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/m__mdy__m"/>
    <language>en</language>
    <item>
      <title>FTX: Asynchronous File Tree Explorer for Vim and Neovim</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Thu, 08 Jan 2026 07:35:16 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/ftx-asynchronous-file-tree-explorer-for-vim-and-neovim-2o96</link>
      <guid>https://forem.com/m__mdy__m/ftx-asynchronous-file-tree-explorer-for-vim-and-neovim-2o96</guid>
      <description>&lt;h2&gt;
  
  
  Abstract
&lt;/h2&gt;

&lt;p&gt;FTX (File Tree eXplorer) is a lightweight, asynchronous file tree explorer built entirely in Vimscript with first-class Git integration. Unlike traditional file browsers that block the editor during operations, FTX leverages Vim's native job API and a custom promise-based async engine to provide non-blocking file tree rendering, real-time Git status updates, and responsive interaction. This article introduces FTX's design philosophy, core features, and the architectural decisions that make it both performant and maintainable—from its Git-inspired cache system to its Go-like concurrency primitives.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Story Behind FTX
&lt;/h2&gt;

&lt;p&gt;For months, I relied on NERDTree as my file browser. It worked, but something felt heavy. The blocking operations, the occasional lag when navigating large directories—it all added friction to my workflow. So I decided to try Netrw, Vim's built-in file explorer. It was minimal, familiar, and perfectly functional. But after a while, it felt too barebones. I missed having a structured tree view, and more importantly, I desperately missed seeing Git status at a glance.&lt;/p&gt;

&lt;p&gt;My workflow is deeply tied to Git. I need to know what's changed, what's staged, what state my branch is in—without running &lt;code&gt;git status&lt;/code&gt; in a separate terminal. Even my Bash prompt is heavily configured to show Git info. I wanted that same level of awareness inside my file tree.&lt;/p&gt;

&lt;p&gt;One night, while browsing file explorer source code, I stumbled upon &lt;a href="https://github.com/francoiscabrol/ranger.vim" rel="noopener noreferrer"&gt;ranger.vim&lt;/a&gt;. It was strikingly simple—a clean, single-file implementation. That sparked an idea: &lt;em&gt;I could build my own. Something tailored exactly to my needs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I sketched out ideas, discussed architecture patterns, and made a decision: &lt;strong&gt;I was going to build FTX.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What is FTX?
&lt;/h2&gt;

&lt;p&gt;FTX is an asynchronous file tree explorer for Vim 8.0+ and Neovim 0.4+. It's designed around three core principles:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Async by design&lt;/strong&gt; – All file operations and Git status checks run in the background using Vim's job API. Opening deep directories or refreshing Git status never blocks the editor.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Git-first workflow&lt;/strong&gt; – Real-time Git status indicators (&lt;code&gt;+&lt;/code&gt; staged, &lt;code&gt;*&lt;/code&gt; modified, &lt;code&gt;?&lt;/code&gt; untracked) appear directly in the tree. Branch information, commit tracking (ahead/behind), and stash detection are all visible without leaving Vim.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Zero dependencies&lt;/strong&gt; – FTX is pure Vimscript with no external dependencies beyond Vim/Neovim itself.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;FTX isn't trying to replace project managers or become a Swiss Army knife. It does one thing well: show you your files and exactly what Git thinks about them—quickly, reliably, and without getting in your way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project repository:&lt;/strong&gt; &lt;a href="https://github.com/m-mdy-m/ftx.vim" rel="noopener noreferrer"&gt;github.com/m-mdy-m/ftx.vim&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Core Features
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Async Everything
&lt;/h3&gt;

&lt;p&gt;File tree rendering, directory traversal, and Git status parsing all happen asynchronously. FTX uses Vim's &lt;code&gt;+job&lt;/code&gt; and &lt;code&gt;+timers&lt;/code&gt; features to schedule work without blocking the UI. You can navigate, edit, and work normally while FTX updates in the background.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git Integration
&lt;/h3&gt;

&lt;p&gt;Every file can display a Git status symbol:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;+&lt;/code&gt; – Staged for commit&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;*&lt;/code&gt; – Modified, not staged&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;?&lt;/code&gt; – Untracked by Git&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-&lt;/code&gt; – Deleted&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;!&lt;/code&gt; – Merge conflict&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;→&lt;/code&gt; – Renamed&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;◌&lt;/code&gt; – Ignored (optional)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The status line shows branch info: &lt;code&gt;[main] ↑2 ↓1 $&lt;/code&gt; tells you you're on &lt;code&gt;main&lt;/code&gt;, 2 commits ahead, 1 behind, with an active stash. Press &lt;code&gt;gi&lt;/code&gt; for detailed branch information, or &lt;code&gt;gb&lt;/code&gt; for Git blame (if enabled).&lt;/p&gt;

&lt;h3&gt;
  
  
  Multi-File Operations
&lt;/h3&gt;

&lt;p&gt;Mark files with &lt;code&gt;m&lt;/code&gt;, then batch open them (&lt;code&gt;mo&lt;/code&gt;) or stage them to Git (&lt;code&gt;mg&lt;/code&gt;). The marking system makes working with multiple files fast and intuitive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flexible Display Modes
&lt;/h3&gt;

&lt;p&gt;FTX works as both a split window (like Netrw) and a project drawer (like NERDTree). Use &lt;code&gt;:FTX . -drawer&lt;/code&gt; for fixed-width sidebar mode with smart quit handling and auto-focus restoration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Customizable
&lt;/h3&gt;

&lt;p&gt;Configure icons, colors, keymaps, Git update intervals, and behavior. FTX provides sensible defaults but gets out of your way if you want something different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Full feature documentation:&lt;/strong&gt; &lt;a href="//doc/README.md"&gt;doc/README.md&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Configuration guide:&lt;/strong&gt; &lt;a href="//doc/config.md"&gt;doc/config.md&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Keymaps reference:&lt;/strong&gt; &lt;a href="//doc/keymaps.md"&gt;doc/keymaps.md&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Git features:&lt;/strong&gt; &lt;a href="//doc/git.md"&gt;doc/git.md&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Architecture Overview
&lt;/h2&gt;

&lt;p&gt;FTX's architecture is built around five interconnected systems: the async engine, cache layer, tree management, Git integration, and rendering pipeline. Each is designed to be modular, testable, and performant.&lt;/p&gt;
&lt;h3&gt;
  
  
  Async Engine
&lt;/h3&gt;

&lt;p&gt;At the heart of FTX is a custom async engine inspired by JavaScript Promises and Go's concurrency model. Instead of blocking Vim while reading directories or running Git commands, FTX schedules work on a task queue and processes it using timer-based workers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key components:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Promises&lt;/strong&gt; (&lt;code&gt;autoload/ftx/async/promise.vim&lt;/code&gt;): An ECMAScript-like Promise implementation based on &lt;a href="https://github.com/stefanpenner/es6-promise" rel="noopener noreferrer"&gt;es6-promise&lt;/a&gt;. Promises allow chaining async operations with &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;.catch()&lt;/code&gt;, making complex workflows readable and composable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Job API&lt;/strong&gt; (&lt;code&gt;autoload/ftx/async/job.vim&lt;/code&gt;): A unified wrapper around Vim's &lt;code&gt;job_start()&lt;/code&gt; and Neovim's &lt;code&gt;jobstart()&lt;/code&gt; that returns Promises. This lets FTX run shell commands (like &lt;code&gt;git status&lt;/code&gt;) asynchronously and handle their output when ready.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scheduler&lt;/strong&gt; (&lt;code&gt;autoload/ftx/async/internal/&lt;/code&gt;): A Go-inspired goroutine-style scheduler with channels, wait groups, and a worker pool. Tasks are queued and executed by timer-driven workers, scaling up to handle load and shutting down when idle.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This design ensures that no matter how large your repository or how deep your directory tree, FTX remains responsive.&lt;/p&gt;
&lt;h3&gt;
  
  
  Cache System
&lt;/h3&gt;

&lt;p&gt;FTX's cache is inspired by Git's tree/blob architecture. Instead of naively re-reading directories on every refresh, FTX uses content-based hashing to detect changes and reuse cached results when possible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each file and directory gets a hash based on its modification time and content (for directories, the list of children).&lt;/li&gt;
&lt;li&gt;Before rebuilding a tree, FTX checks if the hash has changed. If not, it returns the cached result.&lt;/li&gt;
&lt;li&gt;This dramatically reduces disk I/O and makes navigation feel instant, especially in large projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementation:&lt;/strong&gt; &lt;code&gt;autoload/ftx/tree/cache.vim&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Tree Management
&lt;/h3&gt;

&lt;p&gt;The tree module (&lt;code&gt;autoload/ftx/tree/tree.vim&lt;/code&gt;) handles directory traversal, node expansion, and state tracking. Directories can be lazily loaded—only expanding when the user requests it—which keeps memory usage low and initial render fast.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Node structure:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="s1"&gt;'path'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'/full/path/to/file'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'filename'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'depth'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'is_dir'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'is_expanded'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="s1"&gt;'children'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...],&lt;/span&gt;
  &lt;span class="s1"&gt;'git_status'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'*'&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nodes are flattened into a displayable list by &lt;code&gt;tree#flatten()&lt;/code&gt;, which recursively collects visible nodes based on expansion state. This flat list is then passed to the renderer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git Integration
&lt;/h3&gt;

&lt;p&gt;Git status checking runs asynchronously every few seconds (configurable via &lt;code&gt;g:ftx_git_update_time&lt;/code&gt;). FTX spawns a &lt;code&gt;git status --porcelain&lt;/code&gt; job, parses the output, and updates an internal cache mapping file paths to status symbols.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Branch tracking&lt;/strong&gt; (&lt;code&gt;autoload/ftx/git/branch.vim&lt;/code&gt;): Parses &lt;code&gt;git status -b&lt;/code&gt; to extract branch name, ahead/behind counts, and checks for stashed changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blame support&lt;/strong&gt; (&lt;code&gt;autoload/ftx/git/blame.vim&lt;/code&gt;): If enabled, press &lt;code&gt;gb&lt;/code&gt; on a file to see the last 10 commits with author, time, and message in a scrollable popup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Documentation:&lt;/strong&gt; &lt;a href="//doc/git.md"&gt;doc/git.md&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Rendering Pipeline
&lt;/h3&gt;

&lt;p&gt;The renderer (&lt;code&gt;autoload/ftx/renderer/default.vim&lt;/code&gt;) transforms tree nodes into display lines with icons, colors, and Git status indicators. It:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Iterates over flattened nodes&lt;/li&gt;
&lt;li&gt;Builds display strings (indentation + icons + filename)&lt;/li&gt;
&lt;li&gt;Applies syntax highlighting rules&lt;/li&gt;
&lt;li&gt;Updates the buffer content in one atomic operation&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Syntax groups (&lt;code&gt;FTXDir&lt;/code&gt;, &lt;code&gt;FTXGitModified&lt;/code&gt;, &lt;code&gt;FTXIconCollapsed&lt;/code&gt;, etc.) are dynamically defined based on configuration. Users can override colors and icons by setting &lt;code&gt;g:ftx_colors&lt;/code&gt; and &lt;code&gt;g:ftx_icons&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Drawer Mode
&lt;/h3&gt;

&lt;p&gt;Drawer mode (&lt;code&gt;autoload/ftx/internal/drawer/&lt;/code&gt;) provides NERDTree-style behavior:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Auto-resize&lt;/strong&gt;: Preserves drawer width across tab switches&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Auto-restore focus&lt;/strong&gt;: Returns focus to the previous window when closing other windows&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Smart quit&lt;/strong&gt;: Prevents accidentally closing Vim when the drawer is the last window&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These behaviors are implemented via autocommands and buffer-local state tracking.&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;vim-plug:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight viml"&gt;&lt;code&gt;Plug &lt;span class="s1"&gt;'m-mdy-m/ftx.vim'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Vim 8 packages:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/m-mdy-m/ftx.vim ~/.vim/pack/ftx/start/ftx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Neovim packages:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/m-mdy-m/ftx.vim ~/.config/nvim/pack/ftx/start/ftx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Basic Usage
&lt;/h3&gt;

&lt;p&gt;Open FTX in current directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;:&lt;/span&gt;FTX &lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Toggle FTX:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;:&lt;/span&gt;FTXToggle
&lt;span class="c"&gt;" or press F2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open as project drawer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="p"&gt;:&lt;/span&gt;FTX &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;drawer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Auto-open on directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;vim ~/projects
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Common Keymaps
&lt;/h3&gt;

&lt;p&gt;Inside FTX buffer:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Key&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;o&lt;/code&gt;, &lt;code&gt;&amp;lt;Enter&amp;gt;&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Open file / Toggle directory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Open in tab&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;s&lt;/code&gt;, &lt;code&gt;v&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Open in split/vsplit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;r&lt;/code&gt;, &lt;code&gt;R&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Refresh tree / Git status&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;I&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Toggle hidden files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;O&lt;/code&gt;, &lt;code&gt;C&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Expand/Collapse all&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;m&lt;/code&gt;, &lt;code&gt;M&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Toggle mark / Clear marks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;mo&lt;/code&gt;, &lt;code&gt;mg&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Open marked / Stage marked&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;gi&lt;/code&gt;, &lt;code&gt;gb&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Git branch info / Blame&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;yy&lt;/code&gt;, &lt;code&gt;yn&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Yank path / filename&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;?&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Show help&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;q&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Close&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Full keymap reference:&lt;/strong&gt; &lt;a href="//doc/keymaps.md"&gt;doc/keymaps.md&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Configuration
&lt;/h2&gt;

&lt;p&gt;FTX provides extensive configuration options. Here are the essentials:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight viml"&gt;&lt;code&gt;&lt;span class="c"&gt;" Window settings&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_width&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'left'&lt;/span&gt;  " &lt;span class="nb"&gt;or&lt;/span&gt; &lt;span class="s1"&gt;'right'&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_show_hidden&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_auto_sync&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;      " Sync &lt;span class="k"&gt;to&lt;/span&gt; current &lt;span class="k"&gt;file&lt;/span&gt;

&lt;span class="c"&gt;" Git settings&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_enable_git&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_git_update_time&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2000&lt;/span&gt;  " Update interval &lt;span class="p"&gt;(&lt;/span&gt;ms&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_git_blame&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;           " Enable blame feature

&lt;span class="c"&gt;" Icons and colors&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_enable_icons&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_icon_expanded&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'▾'&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_icon_collapsed&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'▸'&lt;/span&gt;

&lt;span class="c"&gt;" Custom file type icons&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_icons&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="se"&gt;      \&lt;/span&gt; &lt;span class="s1"&gt;'vim'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="se"&gt;      \&lt;/span&gt; &lt;span class="s1"&gt;'md'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="se"&gt;      \&lt;/span&gt; &lt;span class="s1"&gt;'js'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="se"&gt;      \&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;" Custom colors&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;g:ftx_colors&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="se"&gt;      \&lt;/span&gt; &lt;span class="s1"&gt;'vim'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'guifg=#019733 ctermfg=35'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="se"&gt;      \&lt;/span&gt; &lt;span class="s1"&gt;'py'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'guifg=#3572A5 ctermfg=67'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="se"&gt;      \&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Complete configuration guide:&lt;/strong&gt; &lt;a href="//doc/config.md"&gt;doc/config.md&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Under the Hood
&lt;/h2&gt;

&lt;p&gt;For those curious about the implementation details, here's a glimpse into the technical foundation of FTX.&lt;/p&gt;

&lt;h3&gt;
  
  
  Promise-Based Async
&lt;/h3&gt;

&lt;p&gt;FTX's Promise implementation follows the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise" rel="noopener noreferrer"&gt;ECMAScript Promise spec&lt;/a&gt;, based on the &lt;a href="https://github.com/stefanpenner/es6-promise" rel="noopener noreferrer"&gt;es6-promise&lt;/a&gt; library. Promises wrap asynchronous operations and allow chaining:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight viml"&gt;&lt;code&gt;ftx#async#&lt;span class="nb"&gt;fs&lt;/span&gt;#&lt;span class="nb"&gt;readdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/path'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;then&lt;span class="p"&gt;({&lt;/span&gt;entries &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; process&lt;span class="p"&gt;(&lt;/span&gt;entries&lt;span class="p"&gt;)})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;then&lt;span class="p"&gt;({&lt;/span&gt;result &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; cache&lt;span class="p"&gt;(&lt;/span&gt;result&lt;span class="p"&gt;)})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;err &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; handle_error&lt;span class="p"&gt;(&lt;/span&gt;err&lt;span class="p"&gt;)})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes complex async workflows (read directory → filter → sort → render) composable and readable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Goroutine-Style Concurrency
&lt;/h3&gt;

&lt;p&gt;The internal scheduler (&lt;code&gt;autoload/ftx/async/internal/&lt;/code&gt;) uses Go-inspired concurrency primitives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Channels&lt;/strong&gt; (&lt;code&gt;channels.vim&lt;/code&gt;): Non-blocking message passing between tasks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wait groups&lt;/strong&gt; (&lt;code&gt;waiter.vim&lt;/code&gt;): Synchronize multiple async operations&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Worker pool&lt;/strong&gt; (&lt;code&gt;worker.vim&lt;/code&gt;): Dynamically scales workers to process queued tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Workers run on Vim timers, executing one task per tick. When the queue is empty, workers shut down. This keeps overhead minimal while providing concurrency when needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Git-Like Cache
&lt;/h3&gt;

&lt;p&gt;Inspired by Git's &lt;a href="https://git-scm.com/book/en/v2/Git-Internals-Git-Objects" rel="noopener noreferrer"&gt;object storage model&lt;/a&gt;, FTX computes content hashes for files and directories. A directory's hash depends on its children's names, so moving files invalidates the cache correctly.&lt;/p&gt;

&lt;p&gt;Cache keys are &lt;code&gt;path:hash&lt;/code&gt; pairs. When checking the cache, FTX recomputes the hash and compares—only re-reading if content has changed. This approach is both efficient and correct.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modular Design
&lt;/h3&gt;

&lt;p&gt;FTX is split into logical modules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;autoload/ftx/async/&lt;/code&gt; – Async engine (promises, jobs, scheduler)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;autoload/ftx/tree/&lt;/code&gt; – Tree management (build, cache, filter, nodes)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;autoload/ftx/git/&lt;/code&gt; – Git integration (status, branch, blame)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;autoload/ftx/renderer/&lt;/code&gt; – Display rendering and syntax&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;autoload/ftx/internal/&lt;/code&gt; – Window/buffer management, drawer mode&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;autoload/ftx/mapping/&lt;/code&gt; – Keymap handlers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;autoload/ftx/helpers/&lt;/code&gt; – Utilities (path, platform, logging)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each module has a clear responsibility and minimal coupling. This makes FTX maintainable and testable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source code:&lt;/strong&gt; &lt;a href="https://github.com/m-mdy-m/ftx.vim" rel="noopener noreferrer"&gt;github.com/m-mdy-m/ftx.vim&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Inspiration and Credits
&lt;/h2&gt;

&lt;p&gt;FTX stands on the shoulders of great explorers that came before:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;netrw&lt;/strong&gt;: Vim's built-in file browser, the gold standard for simplicity&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NERDTree&lt;/strong&gt;: The classic tree explorer, beloved by many&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;fern.vim&lt;/strong&gt;: Modern async architecture and clean design&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ranger.vim&lt;/strong&gt;: Elegant single-file simplicity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;FTX aims to carve out its own niche: a fast, Git-aware, no-nonsense file tree that gets out of your way.&lt;/p&gt;




&lt;h2&gt;
  
  
  Community and Feedback
&lt;/h2&gt;

&lt;p&gt;FTX is designed for real workflows, but it can only improve with feedback from the people who use it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repository:&lt;/strong&gt; &lt;a href="https://github.com/m-mdy-m/ftx.vim" rel="noopener noreferrer"&gt;github.com/m-mdy-m/ftx.vim&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Documentation:&lt;/strong&gt; &lt;code&gt;:help ftx&lt;/code&gt; or &lt;a href="//doc/ftx.txt"&gt;doc/ftx.txt&lt;/a&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Issues &amp;amp; PRs:&lt;/strong&gt; &lt;a href="https://github.com/m-mdy-m/ftx.vim/issues" rel="noopener noreferrer"&gt;github.com/m-mdy-m/ftx.vim/issues&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try it, break it, and let me know what you think.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What's your take on file explorers in Vim?&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>vim</category>
      <category>neovim</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Make Vim Useful Again with VEX</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Thu, 25 Dec 2025 14:15:18 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/make-vim-useful-again-with-vex-mah</link>
      <guid>https://forem.com/m__mdy__m/make-vim-useful-again-with-vex-mah</guid>
      <description>&lt;p&gt;Hey everyone!&lt;/p&gt;

&lt;p&gt;After months of tweaking, testing, and countless evenings spent in the terminal, I’m really excited to share the evolution of my Vim setup into something I’m now calling &lt;strong&gt;VEX&lt;/strong&gt; — the &lt;strong&gt;Vim Ecosystem eXtension&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What started as a personal &lt;code&gt;.vimrc&lt;/code&gt; file slowly grew into a sprawling, hard-to-manage configuration. I wanted the power of a modern IDE—smart completion, easy navigation, seamless LSP support—but without leaving the comfort and speed of Vim. I also wanted it to work instantly, on any machine I sat down at. The result is VEX: a modern, modular, and batteries-included Vim configuration that’s just one &lt;code&gt;git clone&lt;/code&gt; away from a full-powered editing environment.&lt;/p&gt;

&lt;p&gt;The project lives here, fully open-source:&lt;br&gt;
&lt;a href="https://github.com/m-mdy-m/.vimrc" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  So, what is VEX exactly?
&lt;/h3&gt;

&lt;p&gt;VEX is a complete Vim ecosystem packaged into a single repository. It’s not a new editor or a plugin; it’s a carefully curated, pre-configured environment that layers on top of your existing Vim (version 8.0+). Think of it as a distribution for Vim, focused on developer experience and consistency.&lt;/p&gt;

&lt;p&gt;The core philosophy is &lt;strong&gt;sensible defaults with zero friction&lt;/strong&gt;. You shouldn’t spend hours configuring LSP servers, setting up fuzzy finders, or getting Git integration to work. VEX handles that, providing a cohesive experience across Linux, macOS, and even WSL out of the box. The installation process is documented step-by-step, including distribution-specific notes for Linux:&lt;br&gt;
&lt;a href="https://github.com/m-mdy-m/.vimrc/blob/main/docs/installation/linux.md" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc/blob/main/docs/installation/linux.md&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What makes it different?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;It’s Truly One-Command Ready&lt;/strong&gt;&lt;br&gt;
The biggest goal was eliminating setup steps. With VEX, you run &lt;code&gt;make install&lt;/code&gt; and you’re done. It installs Vim configuration, pulls in a curated set of plugins, and can optionally install Language Server Protocol (LSP) servers for you. Start Vim, and everything—from syntax highlighting to intelligent Go-To-Definition—just works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modular &amp;amp; Customizable Installation&lt;/strong&gt;&lt;br&gt;
While the full experience is recommended, I built VEX to be flexible. Maybe you’re on a slow connection, or you only want the core config. You can install it in different modes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--minimal&lt;/code&gt; for just the essentials&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--skip-lsp&lt;/code&gt; if you want to manage your own language servers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--skip-plugins&lt;/code&gt; if you only want the VEX configuration structure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This modularity applies throughout the system. Plugins, settings, and keymaps are all documented and designed to be easy to adjust without breaking the overall setup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Management Layer on Top of Vim&lt;/strong&gt;&lt;br&gt;
VEX adds a lightweight command-line interface to manage your environment. Instead of remembering how to update each plugin or LSP server, you have simple commands like &lt;code&gt;vex update&lt;/code&gt;, &lt;code&gt;vex doctor&lt;/code&gt;, and &lt;code&gt;vex plugin list&lt;/code&gt;. All available shell and Vim commands are documented here:&lt;br&gt;
&lt;a href="https://github.com/m-mdy-m/.vimrc/blob/main/docs/reference/commands.md" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc/blob/main/docs/reference/commands.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This turns Vim from a static config file into a manageable, updatable application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Modern IDE Features, Vim Philosophy&lt;/strong&gt;&lt;br&gt;
Under the hood, VEX integrates well-known and battle-tested plugins like &lt;code&gt;vim-lsp&lt;/code&gt; for language intelligence, &lt;code&gt;fzf&lt;/code&gt; for fast fuzzy searching, &lt;code&gt;vim-gitgutter&lt;/code&gt; for inline Git diffs, and &lt;code&gt;vim-airline&lt;/code&gt; for a clean, informative status line. Key mappings are designed to feel intuitive—things like &lt;code&gt;Ctrl+N&lt;/code&gt; for toggling the file explorer—without overriding core Vim muscle memory.&lt;/p&gt;

&lt;p&gt;All default key mappings are documented in one place, so nothing feels “hidden”:&lt;br&gt;
&lt;a href="https://github.com/m-mdy-m/.vimrc/blob/main/docs/configuration/keymaps.md" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc/blob/main/docs/configuration/keymaps.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s the quick start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/m-mdy-m/.vimrc.git vex
&lt;span class="nb"&gt;cd &lt;/span&gt;vex
make &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, just run &lt;code&gt;vim&lt;/code&gt;. The welcome screen greets you, and you're ready to code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Built on the shoulders of the Vim ecosystem
&lt;/h3&gt;

&lt;p&gt;One thing I want to be very clear about: VEX is not trying to reinvent Vim, and it’s definitely not trying to replace the ecosystem that’s been evolving for decades.&lt;/p&gt;

&lt;p&gt;VEX exists because of that ecosystem.&lt;/p&gt;

&lt;p&gt;Most of the heavy lifting is done by plugins many of us already know and trust—&lt;code&gt;vim-plug&lt;/code&gt; for plugin management, &lt;code&gt;vim-lsp&lt;/code&gt; for LSP support, &lt;code&gt;fzf&lt;/code&gt; for fuzzy finding, &lt;code&gt;vim-gitgutter&lt;/code&gt; for Git integration, &lt;code&gt;vim-airline&lt;/code&gt; for the status line, plus tools like &lt;code&gt;vim-surround&lt;/code&gt; and &lt;code&gt;vim-easymotion&lt;/code&gt; for editing and navigation.&lt;/p&gt;

&lt;p&gt;What VEX does differently is curation and integration. Instead of throwing plugins together and hoping they don’t fight each other, I spent a lot of time making sure they feel like parts of a single system. Key mappings don’t collide. Features are discoverable. Defaults are predictable.&lt;/p&gt;

&lt;p&gt;Plugin behavior and management are fully documented here:&lt;br&gt;
&lt;a href="https://github.com/m-mdy-m/.vimrc/blob/main/docs/configuration/plugins.md" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc/blob/main/docs/configuration/plugins.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the complete, authoritative plugin list lives directly in the repo:&lt;br&gt;
&lt;a href="https://github.com/m-mdy-m/.vimrc/blob/main/src/plugins/install.vim" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc/blob/main/src/plugins/install.vim&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Where VEX is headed
&lt;/h3&gt;

&lt;p&gt;Right now, VEX is in a place I’m genuinely comfortable using every day. It’s stable, fast, and does what I expect without surprises. That said, it’s far from “finished”—mostly because I don’t think a developer environment ever really is.&lt;/p&gt;

&lt;p&gt;The next phase is about refinement. Documentation will keep improving, especially for people who want to tweak settings safely. LSP support can always be smoother across more languages. And there are plenty of small quality-of-life improvements that only show up after long-term daily use.&lt;/p&gt;

&lt;p&gt;All documentation lives in the &lt;code&gt;docs/&lt;/code&gt; directory and is structured around configuration, reference commands, and installation guides:&lt;br&gt;
&lt;a href="https://github.com/m-mdy-m/.vimrc/tree/main/docs" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc/tree/main/docs&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s Discuss &amp;amp; Contribute!
&lt;/h3&gt;

&lt;p&gt;I’m genuinely excited about where VEX is right now, and I’d really love to hear your thoughts and experiences.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Does a batteries-included Vim setup like this appeal to you, or do you prefer building everything piece by piece?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Which parts of Vim configuration feel empowering to you, and which parts feel like pure friction or wasted time?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;If you tried VEX, what would you change first—keymaps, plugins, defaults, or the overall structure?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;VEX started as a personal response to setup fatigue, but I don’t want it to evolve in a vacuum. Real feedback from real workflows is what shapes a tool like this in a meaningful way.&lt;/p&gt;

&lt;p&gt;If you care about fast, focused, and distraction-free development environments—and if you believe Vim can feel modern without losing its soul—I’d love for you to join the conversation and help shape where VEX goes next.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Star the repo:&lt;/strong&gt; &lt;a href="https://github.com/m-mdy-m/.vimrc" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explore the docs:&lt;/strong&gt; &lt;a href="https://github.com/m-mdy-m/.vimrc/tree/main/docs" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/.vimrc/tree/main/docs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Browse plugins &amp;amp; config:&lt;/strong&gt; everything is transparent and documented in the repo&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contribute ideas, issues, or PRs:&lt;/strong&gt; even small suggestions are welcome&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;VEX isn’t trying to be everything for everyone. It’s trying to be a solid, opinionated Vim environment that respects your time. If that resonates with you, let’s talk—what’s your take?&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>programming</category>
      <category>showdev</category>
      <category>vim</category>
    </item>
    <item>
      <title>PSX: The Project Structure Checker</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Sat, 20 Dec 2025 07:50:06 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/psx-the-project-structure-checker-263j</link>
      <guid>https://forem.com/m__mdy__m/psx-the-project-structure-checker-263j</guid>
      <description>&lt;p&gt;You know that feeling when you start a new project and spend the first hour just setting up folders, config files, and all the boring stuff? Or when you clone a repo and realize half the essential files are missing?&lt;/p&gt;

&lt;p&gt;Yeah — I got tired of that too. So I built &lt;strong&gt;PSX&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is PSX?
&lt;/h2&gt;

&lt;p&gt;PSX is a command-line tool that validates your project structure and fixes it automatically. Think of it as a linter, but for your entire project layout instead of just code.&lt;/p&gt;

&lt;p&gt;It checks for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Essential files (README, LICENSE, .gitignore, CHANGELOG)&lt;/li&gt;
&lt;li&gt;Proper folder structure (&lt;code&gt;src/&lt;/code&gt;, &lt;code&gt;tests/&lt;/code&gt;, &lt;code&gt;docs/&lt;/code&gt;, &lt;code&gt;cmd/&lt;/code&gt;, &lt;code&gt;internal/&lt;/code&gt;, &lt;code&gt;pkg/&lt;/code&gt; depending on project type)&lt;/li&gt;
&lt;li&gt;Documentation (CONTRIBUTING, SECURITY, API docs, ADRs)&lt;/li&gt;
&lt;li&gt;CI / DevOps basics (Docker, docker-compose, minimal CI/workflow templates)&lt;/li&gt;
&lt;li&gt;Basic quality tools and standards (EditorConfig, Go linter integration)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the best part? &lt;strong&gt;It doesn't just tell you what's wrong — it fixes it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I Built This
&lt;/h2&gt;

&lt;p&gt;I was working on multiple projects — Node.js, Go, some experiments — and every single time I had to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create the same folders over and over&lt;/li&gt;
&lt;li&gt;Copy-paste &lt;code&gt;.gitignore&lt;/code&gt; from old projects&lt;/li&gt;
&lt;li&gt;Write boilerplate README files&lt;/li&gt;
&lt;li&gt;Set up the same (or half-broken) CI templates&lt;/li&gt;
&lt;li&gt;Remember which files go where&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It was repetitive and boring. So I automated it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;Quick install for Linux/macOS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-sSL&lt;/span&gt; https://raw.githubusercontent.com/m-mdy-m/psx/main/scripts/install.sh | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Windows (PowerShell):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;irm&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;https://raw.githubusercontent.com/m-mdy-m/psx/main/scripts/install.ps1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;iex&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a full step-by-step installation guide, see the official INSTALLATION page:&lt;br&gt;
&lt;a href="https://github.com/m-mdy-m/psx/blob/main/docs/INSTALLATION.md" rel="noopener noreferrer"&gt;docs/INSTALLATION.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or grab binaries from the releases on GitHub: &lt;a href="https://github.com/m-mdy-m/psx/releases" rel="noopener noreferrer"&gt;releases&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Important: Project Type
&lt;/h3&gt;

&lt;p&gt;PSX focuses on &lt;strong&gt;Go&lt;/strong&gt; and &lt;strong&gt;Node.js&lt;/strong&gt; projects. You must tell PSX which type of project you're working on. Add a small config (&lt;code&gt;psx.yml&lt;/code&gt;) in your project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;project&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;go"&lt;/span&gt;      &lt;span class="c1"&gt;# or "nodejs"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PSX uses &lt;code&gt;project.type&lt;/code&gt; to decide which rules and templates to apply. If you don't set it, PSX will ask you to set it in the config.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Usage
&lt;/h3&gt;

&lt;p&gt;Check your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;my-project
psx check
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll get output like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Detected: go  (based on project.type in psx.yml)

ERRORS (2)
✗ README_REQUIRED
    README.md file not found in project root

✗ LICENSE_REQUIRED
    No LICENSE file found

Summary: 2 errors, 0 warnings
Status: FAILED ✗
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now fix it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;psx fix
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PSX will ask before creating each file (interactive mode). Or run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;psx fix &lt;span class="nt"&gt;--all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and everything gets created automatically based on the templates and your configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Cool Parts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Explicit, predictable project types
&lt;/h3&gt;

&lt;p&gt;PSX targets Go and Node.js workflows and applies &lt;strong&gt;type-specific rules&lt;/strong&gt; when you set &lt;code&gt;project.type&lt;/code&gt;. This keeps behavior predictable and templates focused.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Node.js&lt;/strong&gt;: Expects &lt;code&gt;package.json&lt;/code&gt;, checks for &lt;code&gt;src/&lt;/code&gt; or &lt;code&gt;lib/&lt;/code&gt;, looks for test files and basic Node layouts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Go&lt;/strong&gt;: Expects &lt;code&gt;go.mod&lt;/code&gt;, checks for &lt;code&gt;cmd/&lt;/code&gt;, &lt;code&gt;internal/&lt;/code&gt;, &lt;code&gt;pkg/&lt;/code&gt; and common Go conventions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Custom Rules (psx.yml)
&lt;/h3&gt;

&lt;p&gt;Don't like the defaults? Create a &lt;code&gt;psx.yml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;

&lt;span class="na"&gt;project&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nodejs"&lt;/span&gt;

&lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;readme&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;error&lt;/span&gt;      &lt;span class="c1"&gt;# Must have&lt;/span&gt;
  &lt;span class="na"&gt;license&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;warning&lt;/span&gt;   &lt;span class="c1"&gt;# Should have&lt;/span&gt;
  &lt;span class="na"&gt;changelog&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;info&lt;/span&gt;    &lt;span class="c1"&gt;# Nice to have&lt;/span&gt;

&lt;span class="na"&gt;ignore&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;node_modules/&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;dist/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can define which items are errors/warnings/infos and add custom files and folder structures.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Templates for everything
&lt;/h3&gt;

&lt;p&gt;PSX ships with language-specific templates and organized resource files (README templates, multiple LICENSE types, &lt;code&gt;.gitignore&lt;/code&gt; snippets, Docker configs, ADR/docs templates). Templates live with the embedded resources and are easy to customize. ADR and documentation templates are grouped for clarity so you can find and adapt them quickly.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Custom files &amp;amp; folders
&lt;/h3&gt;

&lt;p&gt;Need project-specific structure? Add custom config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;custom&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;files&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;.env.example"&lt;/span&gt;
      &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;NODE_ENV=development&lt;/span&gt;
        &lt;span class="s"&gt;PORT=3000&lt;/span&gt;

  &lt;span class="na"&gt;folders&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;src/api"&lt;/span&gt;
      &lt;span class="na"&gt;structure&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;controllers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{}&lt;/span&gt;
        &lt;span class="na"&gt;middlewares&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{}&lt;/span&gt;
        &lt;span class="na"&gt;routes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See the example config in &lt;code&gt;examples/psx.examples.yml&lt;/code&gt; for more advanced setups.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. CI / Docker support
&lt;/h3&gt;

&lt;p&gt;PSX can scaffold Docker files and simplified CI/workflow templates that are appropriate for the selected project type. The templates are intentionally minimal and focused — PSX won't push a huge set of opinionated, hard-to-maintain workflows. If you want complex, highly custom CI, you can keep your own workflows and use PSX to maintain the basics.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Designed for automation
&lt;/h3&gt;

&lt;p&gt;PSX outputs machine-friendly JSON and has flags suitable for CI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Validate Structure&lt;/span&gt;
  &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
    &lt;span class="s"&gt;psx check --output json&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;--fail-on warning&lt;/code&gt; in pipelines if you want stricter enforcement.&lt;/p&gt;

&lt;h2&gt;
  
  
  How It's Built
&lt;/h2&gt;

&lt;p&gt;PSX is written in &lt;strong&gt;Go 1.25&lt;/strong&gt; — a single static binary, no runtime dependencies, works everywhere.&lt;/p&gt;

&lt;p&gt;Key components (linked so you can read the code/templates directly):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Config Loader&lt;/strong&gt; — &lt;a href="https://github.com/m-mdy-m/psx/tree/main/internal/config" rel="noopener noreferrer"&gt;internal/config/&lt;/a&gt; — Reads YAML configs and validates them against the schema.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rules Engine&lt;/strong&gt; — &lt;a href="https://github.com/m-mdy-m/psx/tree/main/internal/rules" rel="noopener noreferrer"&gt;internal/rules/&lt;/a&gt; — Runs checks in parallel, using a centralized, simplified rules implementation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resources&lt;/strong&gt; — &lt;a href="https://github.com/m-mdy-m/psx/tree/main/internal/resources" rel="noopener noreferrer"&gt;internal/resources/&lt;/a&gt; — Where templates and message files live; templates are organized for discoverability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fixer&lt;/strong&gt; — &lt;a href="https://github.com/m-mdy-m/psx/blob/main/internal/rules/fixer.go" rel="noopener noreferrer"&gt;internal/rules/fixer.go&lt;/a&gt; — Creates missing files and folders using the embedded templates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reporter&lt;/strong&gt; — &lt;a href="https://github.com/m-mdy-m/psx/tree/main/internal/reporter" rel="noopener noreferrer"&gt;internal/reporter/&lt;/a&gt; — Table or JSON output for humans and automation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The codebase was refactored to be simpler and easier to maintain: consolidated checker/fixer flows, clearer resource separation, and a smaller, more focused command surface (&lt;code&gt;check&lt;/code&gt; and &lt;code&gt;fix&lt;/code&gt;). You can browse the rest of the internals here: &lt;a href="https://github.com/m-mdy-m/psx/tree/main/internal" rel="noopener noreferrer"&gt;internal/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What PSX Doesn’t Do
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;PSX is intentionally narrow: it focuses on practical, repeatable scaffolding for Go and Node.js.&lt;/li&gt;
&lt;li&gt;It doesn't try to auto-detect project type — you set &lt;code&gt;project.type&lt;/code&gt; so behaviour is explicit.&lt;/li&gt;
&lt;li&gt;It doesn't scaffold every possible CI/quality tool; instead it provides minimal, maintainable templates and a clear place for you to extend things via configs and custom templates.&lt;/li&gt;
&lt;li&gt;Plugin system and multi-project scanning are not part of the core (for now).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Want to Contribute?
&lt;/h2&gt;

&lt;p&gt;PSX is open source (MIT). If you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find bugs → Open an issue&lt;/li&gt;
&lt;li&gt;Want features → Start a discussion&lt;/li&gt;
&lt;li&gt;Have ideas → Pull requests welcome&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code is structured to be easy to extend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;New rules? Edit &lt;code&gt;rules.yml&lt;/code&gt;/&lt;code&gt;internal/resources&lt;/code&gt; and add templates.&lt;/li&gt;
&lt;li&gt;New templates? Place updated YAML templates under the resources area and rebuild.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Philosophy
&lt;/h2&gt;

&lt;p&gt;I built PSX with one principle: &lt;strong&gt;Don't make me think about boring stuff.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When I start a project, I want to write code, not spend 30 minutes setting up folders and config files. When I clone a repo, I want to know it has proper structure without manual inspection.&lt;/p&gt;

&lt;p&gt;PSX handles the boring parts so you can focus on the interesting parts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try It
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Install&lt;/span&gt;
curl &lt;span class="nt"&gt;-sSL&lt;/span&gt; https://raw.githubusercontent.com/m-mdy-m/psx/main/scripts/install.sh | bash

&lt;span class="c"&gt;# Check a project&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;your-project
&lt;span class="c"&gt;# make sure psx.yml contains `project.type: "go"` or `"nodejs"`&lt;/span&gt;
psx check

&lt;span class="c"&gt;# Fix issues&lt;/span&gt;
psx fix

&lt;span class="c"&gt;# Customize&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"version: 1"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; psx.yml
&lt;span class="c"&gt;# Add your rules...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Contribute or Share Your Ideas!
&lt;/h3&gt;

&lt;p&gt;PSX is under active development and the idea is still evolving. If you like the project, please &lt;strong&gt;star&lt;/strong&gt; the repo, open issues, submit pull requests, or share your ideas on GitHub: &lt;a href="https://github.com/m-mdy-m/psx" rel="noopener noreferrer"&gt;https://github.com/m-mdy-m/psx&lt;/a&gt;. Whether you spot a bug, want a new rule or template — your feedback matters.&lt;/p&gt;

&lt;p&gt;I’d love to hear your thoughts — does PSX’s focused, type-first design make project setup more predictable and useful for you? Would you prefer broader language support, a plugin system, or richer CI templates? Start a discussion on GitHub, or email suggestions and security reports to &lt;strong&gt;&lt;a href="mailto:bitsgenix@gmail.com"&gt;bitsgenix@gmail.com&lt;/a&gt;&lt;/strong&gt;. Let’s build something that actually saves time.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>programming</category>
      <category>career</category>
      <category>go</category>
    </item>
    <item>
      <title>Introduction to Analysis of Algorithms</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Tue, 07 Oct 2025 00:01:04 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/introduction-to-analysis-of-algorithms-46b0</link>
      <guid>https://forem.com/m__mdy__m/introduction-to-analysis-of-algorithms-46b0</guid>
      <description>&lt;p&gt;Algorithm analysis is a concept that I’ve always had a kind of reverent fear of, but it’s time to get down to it — even if it’s a 20,000-page article. Well, let’s talk about “analyzing” first. (We definitely know what an algorithm is — if not, check out this article: &lt;a href="https://dev.to/m__mdy__m/what-is-an-algorithm-really-5agc"&gt;https://dev.to/m__mdy__m/what-is-an-algorithm-really-5agc&lt;/a&gt;&lt;br&gt;
). In short, if we want to narrow down what “analysis” means, it means studying or examining something in a systematic way. When we say “algorithm analysis,” we mean that we want to evaluate an algorithm, examine it, and figure out what the best algorithm to write for our problem is or what the best algorithm to use is. We might also use other terms, like “algorithm review,” “algorithm consensus,” etc.&lt;/p&gt;
&lt;h2&gt;
  
  
  1.1 What is Correctness?
&lt;/h2&gt;

&lt;p&gt;To show that an algorithm is &lt;em&gt;correct&lt;/em&gt;, we must demonstrate that it does the thing it is supposed to do. The difficulty is that an algorithm unfolds over time and may perform a variable number of steps (loops, recursion, etc.), so we need a framework to reason about its behavior. A common framework for proving correctness of algorithms (and programs) is &lt;strong&gt;Hoare logic&lt;/strong&gt;. Hoare logic relies on induction, invariants, and logical reasoning; here we will use these ideas informally.&lt;/p&gt;

&lt;p&gt;We use two assertions called the &lt;strong&gt;precondition&lt;/strong&gt; and the &lt;strong&gt;postcondition&lt;/strong&gt;. By &lt;em&gt;correctness&lt;/em&gt; we mean: whenever the precondition holds before the algorithm runs, the postcondition holds after it finishes. By &lt;em&gt;termination&lt;/em&gt; we mean: whenever the precondition holds, the algorithm stops after a finite number of steps. If an algorithm satisfies the correctness assertion but we do not prove termination, we call that &lt;strong&gt;partial correctness&lt;/strong&gt;. When we have both partial correctness and termination we call it &lt;strong&gt;total correctness&lt;/strong&gt;. These terms connect a concrete problem specification to an algorithm that claims to solve it. Therefore we choose preconditions and postconditions to reflect the problem specification we want the algorithm to satisfy.&lt;/p&gt;

&lt;p&gt;We can make these concepts more precise if we introduce a small standard notation. Boolean connectives: 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∧\land&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∧&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 means “and”, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∨\lor&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∨&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 means “or”, and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;¬\neg&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;¬&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 means “not”. We use 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;→\rightarrow&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 for Boolean implication (note that 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(x→y)(x \rightarrow y)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is logically equivalent to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(¬x∨y)(\neg x \lor y)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;¬&lt;/span&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∨&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
), and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;↔\leftrightarrow&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;↔&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 for logical equivalence (
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;α↔β\alpha \leftrightarrow \beta&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;α&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;↔&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;β&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 abbreviates 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(α→β)∧(β→α)(\alpha \rightarrow \beta) \land (\beta \rightarrow \alpha)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;α&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;β&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∧&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;β&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;α&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
). Quantifiers: 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∀\forall&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∀&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is universal quantification (“for all”), and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∃\exists&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∃&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is existential quantification (“there exists”). We often use the informal abbreviation “
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;⇒\Rightarrow&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;⇒&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
” to mean “implies” in English sentences (for example, “2 | x 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;⇒\Rightarrow&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;⇒&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 x is even”), while “
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;⇏\nRightarrow&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mrel amsrm"&gt;⇏&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
” could be used to mean “does not imply”.&lt;/p&gt;

&lt;p&gt;Now suppose 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is an algorithm and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;IAI_A&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is the set of all well-formed inputs for 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. Intuitively, if 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;I∈IAI \in I_A&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;∈&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 then it makes sense to feed input 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;II&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to algorithm 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. (We can formalize “well-formed” further, but intuition will do: e.g., an algorithm expecting a pair of integers should not be given a matrix.) Let 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O=A(I)O = A(I)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 denote the output of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 on input 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;II&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, if an output exists. Let 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;αA\alpha_A&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;α&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 be a precondition of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;βA\beta_A&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;β&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 be a postcondition of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. If input 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;II&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 satisfies the precondition we write 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;αA(I)\alpha_A(I)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;α&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
; if output 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;OO&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 satisfies the postcondition we write 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;βA(O)\beta_A(O)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;β&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. Then &lt;strong&gt;partial correctness&lt;/strong&gt; of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 with respect to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;αA\alpha_A&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;α&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;βA\beta_A&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;β&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 can be expressed as:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∀I∈IA,  (αA(I)∧∃O (O=A(I)))→βA(A(I)).
\forall I \in I_A,\; \big(\alpha_A(I) \land \exists O\,(O = A(I))\big) \rightarrow \beta_A(A(I)).
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∀&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;∈&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="delimsizing size1"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;α&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;∧&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∃&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="delimsizing size1"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;β&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;In words: for every well-formed input 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;II&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, if 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;II&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 satisfies the precondition and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;A(I)A(I)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 produces an output (i.e., 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 terminates on 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;II&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
), then that output satisfies the postcondition. &lt;strong&gt;Total correctness&lt;/strong&gt; is (1.1) together with the additional claim that for all 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;I∈IAI \in I_A&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;∈&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;A(I)A(I)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 terminates — so an 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;OO&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 with 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O=A(I)O = A(I)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 always exists.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.1.&lt;/strong&gt; Modify (1.1) so it states total correctness.&lt;/p&gt;

&lt;p&gt;A central concept in algorithm analysis is the &lt;strong&gt;loop invariant&lt;/strong&gt;. A loop invariant is a statement that remains true after each execution of a loop body (for example, after every iteration of a &lt;code&gt;while&lt;/code&gt; or &lt;code&gt;for&lt;/code&gt; loop). Finding the right invariant and proving it is a creative task. If an algorithm terminates, a loop invariant helps prove 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;αA(I)→βA(A(I))\alpha_A(I) \rightarrow \beta_A(A(I))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;α&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;β&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. After establishing a correct loop invariant, we use it to prove partial correctness of the algorithm. Thus the criterion for choosing a loop invariant is that it should assist in proving the postcondition. In practice there may be many possible loop invariants (and correspondingly many choices of pre- and postconditions) that yield a valid correctness proof; the art lies in selecting useful ones. Typically, to show that a chosen invariant holds after each loop iteration we use induction, often relying on the precondition as an assumption in the induction step.&lt;/p&gt;
&lt;h2&gt;
  
  
  1.2 Why Analyze an Algorithm?
&lt;/h2&gt;

&lt;p&gt;Ļere are several answers to this basic question, depending on one’s frame of reference: the intended use of the algorithm, the importance of the algorithm in relationship to others from both practical and theoretical standpoints, the difficulty of analysis, and the accuracy and precision of the required answer. Ļe most straightforward reason for analyzing an algorithm is to discover its characteristics in order to evaluate its suitability for various applications or compare it with other algorithms for the same application. Ļecharacteristics of interest are most often the primary resources of time and space, particularly time. Put simply, we want to know how long an implementation of a particular algorithm will run on a particular computer, and how much space it will require. We generally strive to keep the analysis independent of particular implementations—we concentrate instead on obtaining results for essential characteristics of the algorithm that can be used to derive&lt;br&gt;
precise estimates of true resource requirements on various actual machines. In practice, achieving independence between an algorithm and characteristics of its implementation can be difficult to arrange. Ļe quality of the implementation and properties of compilers, machine architecture, and other major facets of the programming environment have dramatic effects on performance. We must be cognizant of such effects to be sure the results of analysis are useful. On the other hand, in some cases, analysis of an algorithm can help identify ways for it to take full advantage of the programming environment. Occasionally, some property other than time or space is of interest, and the focus of the analysis changes accordingly. For example, an algorithm on a mobile device might be studied to determine the effect upon battery life, or an algorithm for a numerical problem might be studied to determine how accurate an answer it can provide. Also, it is sometimes appropriate to address multiple resources in the analysis. For example, an algorithm that uses a large amount of memory may use much less time than an algorithm that gets by with very little memory. Indeed, one prime motivation for doing a careful analysis is to provide accurate information to help in making proper tradeoff decisions in such situations.&lt;/p&gt;
&lt;h2&gt;
  
  
  1.3 Theory of Algorithms
&lt;/h2&gt;

&lt;p&gt;The prime goal of the theory of algorithms is to classify algorithms according to their performance characteristics. The following mathematical notations are convenient for doing so:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Definition:&lt;/strong&gt; Given a function 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(N)f(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
,  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(f(N))O(f(N))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 denotes the set of all 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;g(N)g(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 such that 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∣g(N)/f(N)∣|g(N)/f(N)| &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is bounded from above as 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N→∞N \rightarrow \infty&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∞&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Ω(f(N))\Omega(f(N))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Ω&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 denotes the set of all 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;g(N)g(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 such that 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∣g(N)/f(N)∣|g(N)/f(N)| &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is bounded from below by a (strictly) positive number as 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N→∞N \rightarrow \infty&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∞&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ(f(N))\Theta(f(N))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 denotes the set of all 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;g(N)g(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 such that 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;∣g(N)/f(N)∣|g(N)/f(N)| &lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;span class="mord mathnormal"&gt;g&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is bounded from both above and below as 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N→∞N \rightarrow \infty&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;→&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;∞&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.  &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These notations, adapted from classical analysis, were advocated for use in the analysis of algorithms in a paper by Knuth in 1976 [&lt;a href="https://www-cs-faculty.stanford.edu/~knuth/aa.html" rel="noopener noreferrer"&gt;one&lt;/a&gt; | &lt;a href="https://doc.lagout.org/science/0_Computer%20Science/7_Technical%20Papers/Selected%20Papers%20on%20Computer%20Science%20-%20Donald%20E.%20Knuth.pdf" rel="noopener noreferrer"&gt;first&lt;/a&gt;]. They have come into widespread use for making mathematical statements about bounds on the performance of algorithms. The 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;OO&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
-notation provides a way to express an upper bound; the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Ω\Omega&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Ω&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
-notation provides a way to express a lower bound; and the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ\Theta&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
-notation provides a way to express matching upper and lower bounds. In mathematics, the most common use of the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;OO&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
-notation is in the context of asymptotic series.  &lt;/p&gt;

&lt;p&gt;In the theory of algorithms, the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;OO&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
-notation is typically used for three purposes: to hide constants that might be irrelevant or inconvenient to compute, to express a relatively small “error” term in an expression describing the running time of an algorithm, and to bound the worst case. Nowadays, the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Ω\Omega&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Ω&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
- and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ\Theta&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
-notations are directly associated with the theory of algorithms, though similar notations are used in mathematics.  &lt;/p&gt;

&lt;p&gt;Since constant factors are being ignored, derivation of mathematical results using these notations is simpler than if more precise answers are sought. For example, both the “natural” logarithm 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;ln⁡N≡log⁡eN\ln N \equiv \log_e N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;ln&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≡&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop"&gt;lo&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;e&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and the “binary” logarithm 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;lg⁡N≡log⁡2N\lg N \equiv \log_2 N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≡&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop"&gt;lo&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 often arise, but they are related by a constant factor, so we can refer to either as being 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(log⁡N)O(\log N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 if we are not interested in more precision. More to the point, we might say that the running time of an algorithm is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ(Nlog⁡N)\Theta(N \log N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 seconds just based on an analysis of the frequency of execution of fundamental operations and an assumption that each operation takes a constant number of seconds on a given computer, without working out the precise value of the constant.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example 1.4
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Show that&lt;/strong&gt; 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(N)=Nlg⁡N+O(N)f(N) = N \lg N + O(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 &lt;strong&gt;implies that&lt;/strong&gt; 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(N)=Θ(Nlog⁡N)f(N) = \Theta(N \log N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.&lt;/p&gt;

&lt;p&gt;As an illustration of the use of these notations to study the performance characteristics of algorithms, we consider &lt;strong&gt;methods for sorting a set of numbers in an array&lt;/strong&gt;. The &lt;strong&gt;input&lt;/strong&gt; is the numbers in the array, in arbitrary and unknown order; the &lt;strong&gt;output&lt;/strong&gt; is the same numbers in the array, rearranged in ascending order. This is a well-studied and fundamental problem: we will consider an algorithm for solving it, then show that algorithm to be &lt;em&gt;optimal&lt;/em&gt; in a precise technical sense.&lt;/p&gt;

&lt;p&gt;First, we will show that it is possible to solve the sorting problem efficiently using a well-known recursive algorithm called &lt;strong&gt;mergesort&lt;/strong&gt;. Mergesort, and nearly all of the algorithms treated in this book, are described in detail in &lt;strong&gt;Sedgewick and Wayne [30]&lt;/strong&gt;, so we give only a brief description here. Readers interested in further details on variants of the algorithms, implementations, and applications are encouraged to consult the books by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.youseficlass.ir/wp-content/uploads/2023/07/ebooksclub.org__Introduction_to_Algorithms__Third_Edition.pdf" rel="noopener noreferrer"&gt;Cormen, Leiserson, Rivest, and Stein&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://mrce.in/ebooks/Algorithms%204th%20Ed.pdf" rel="noopener noreferrer"&gt;Sedgewick&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://haio.ir/wp-content/uploads/2024/12/Donald-Knuth-The-Art-of-Computer-Programming-Vol.-1_-Fundamental-Algorithms-3rd-Edition-Addison-Wesley-Professional-1997.pdf" rel="noopener noreferrer"&gt;Knuth&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="http://lib.ysu.am/disciplines_bk/279a02d696c8b6eab3856901511b6ed7.pdf" rel="noopener noreferrer"&gt;Gonnet and Baeza-Yates&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mergesort works as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Divide:&lt;/strong&gt; The array is split in the middle into two halves.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Conquer:&lt;/strong&gt; Recursively sort each half.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Combine:&lt;/strong&gt; Merge the two sorted halves together to produce the fully sorted array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is an example of the &lt;strong&gt;divide-and-conquer algorithm design paradigm&lt;/strong&gt;, where a problem is solved by recursively solving smaller subproblems and using their solutions to solve the original problem. The recursive structure of algorithms like mergesort leads naturally to mathematical descriptions of their performance characteristics.&lt;/p&gt;

&lt;p&gt;To accomplish the &lt;strong&gt;merge&lt;/strong&gt; step efficiently, we use two auxiliary arrays, &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;, to hold the subarrays. For efficiency, it is best to declare these arrays external to the recursive method. Invoking the method with &lt;code&gt;mergesort(a, 0, N-1)&lt;/code&gt; will sort the array &lt;code&gt;{% katex inline %}a[0 \dots N-1]{% endkatex %}&lt;/code&gt;. After the recursive calls, the two halves of the array are sorted.&lt;/p&gt;

&lt;p&gt;Then, we move:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the first half of &lt;code&gt;{% katex inline %}a{% endkatex %}&lt;/code&gt; to the auxiliary array &lt;code&gt;b[]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the second half of &lt;code&gt;{% katex inline %}a{% endkatex %}&lt;/code&gt; to the auxiliary array &lt;code&gt;c[]&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We also add a &lt;strong&gt;sentinel value&lt;/strong&gt; &lt;code&gt;INFTY&lt;/code&gt; (assumed to be larger than all array elements) at the end of each auxiliary array. This ensures that once one array is exhausted, the remaining elements of the other array are moved into &lt;code&gt;a[]&lt;/code&gt; without additional bounds checks.&lt;/p&gt;

&lt;p&gt;The merge is then straightforward: for each index &lt;code&gt;k&lt;/code&gt; from 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;lolo&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;l&lt;/span&gt;&lt;span class="mord mathnormal"&gt;o&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;hihi&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;hi&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, move the smaller of &lt;code&gt;b[i]&lt;/code&gt; and &lt;code&gt;c[j]&lt;/code&gt; into &lt;code&gt;a[k]&lt;/code&gt;, then increment the respective index &lt;code&gt;i&lt;/code&gt; or &lt;code&gt;j&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;mergesort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Recursively sort the two halves&lt;/span&gt;
    &lt;span class="n"&gt;mergesort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;mergesort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Copy to auxiliary arrays&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// Add sentinels&lt;/span&gt;
    &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;INFTY&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;INFTY&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// Merge back to a[]&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; 
            &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++];&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; 
            &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++];&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  2 Analysis of Algorithms.
&lt;/h2&gt;

&lt;p&gt;Although the analysis of sorting and merge sort that we have reviewed demonstrates the inherent "difficulty" of the sorting problem, there are many important questions about sorting (and merge sort) that it does not address at all. How long might an implementation of mergesort be expected to run on a particular computer? How might its running time compare to other O(NlogN) methods? (Ļere are many.) How does it compare to sorting methods that are fast on average, but perhaps not in the worst case? How does it compare to sorting methods that are not based on compares among elements? To answer such questions, a more detailed analysis is required. In this section we brieły describe the process of doing such an analysis. To analyze an algorithm, we must ŀrst identify the resources of primary interest so that the detailed analysis may be properly focused. We describe the process in terms of studying the running time since it is the resource most relevant here. A complete analysis of the running time of an algorithm involves the following steps:&lt;br&gt;
• Implement the algorithm completely.&lt;br&gt;
• Determine the time required for each basic operation.&lt;br&gt;
• Identify unknown quantities that can be used to describe the frequency of execution of the basic operations.&lt;br&gt;
• Develop a realistic model for the input to the program.&lt;br&gt;
• Analyze the unknown quantities, assuming the modeled input.&lt;br&gt;
• Calculate the total running time by multiplying the time by the frequency for each operation, then adding all the products.&lt;br&gt;
Ļe ŀrst step in the analysis is to carefully implement the algorithm on a&lt;br&gt;
particular computer. We reserve the term program to describe such an implementation. One algorithm corresponds to many programs. A particular implementation not only provides a concrete object to study, but also can give useful empirical data to aid in or to check the analysis. Presumably the implementation is designed to make efficient use of resources, but it is a mistake to overemphasize efficiency too early in the process. Indeed, a primary application for the analysis is to provide informed guidance toward better implementations. Ļe next step is to estimate the time required by each component instruction of the program. In principle and in practice, we can often do so with great precision, but the process is very dependent on the characteristics of the computer system being studied. Another approach is to simply run the program for small input sizes to “estimate” the values of the constants, or to do so indirectly in the aggregate. &lt;br&gt;
Indeed, to determine the total running time of the program, it is necessary to study the branching structure of the program in order to express the frequency of execution of the component instructions in terms of unknown mathematical quantities. If the values of these quantities are known, then we can derive the running time of the entire program simply by multiplying the frequency and time requirements of each component instruction and adding these products. Many programming environments have tools that can simplify this task. At the ŀrst level of analysis, we concentrate on quantities that have large frequency values or that correspond to large costs; in principle the analysis can be reŀned to produce a fully detailed answer. We often refer to the “cost” of an algorithm as shorthand for the “value of the quantity in question” when the context allows. Ļe next step is to model the input to the program, to form a basis for the mathematical analysis of the instruction frequencies. Ļe values of the unknown frequencies are dependent on the input to the algorithm: the problem size (usually we name that N) is normally the primary parameter used to express our results, but the order or value of input data items ordinarily affects the running time as well. By “model,” we mean a precise description of typical inputs to the algorithm. For example, for sorting algorithms, it is normally convenient to assume that the inputs are randomly ordered and distinct, though the programs normally work even when the inputs are not distinct. Another possibility for sorting algorithms is to assume that the inputs are random numbers taken from a relatively large range. Ļese two models can be shown to be nearly equivalent. Most often, we use the simplest available model of “random” inputs, which is often realistic. Several different models can be used for the same algorithm: one model might be chosen to make the analysis as simple as possible; another model might better rełect the actual situation in which the program is to be used. Ļe last step is to analyze the unknown quantities, assuming the modeled input. For average-case analysis, we analyze the quantities individually, then multiply the averages by instruction times and add them to ŀnd the running time of the whole program. For worst-case analysis, it is usually difficult to get an exact result for the whole program, so we can only derive an upper bound, by multiplying worst-case values of the individual quantities by instruction times and summing the results. Ļis general scenario can successfully provide exact models in many situations. Knuth’s books are based on this precept. Unfortunately, the details in such an exact analysis are often daunting. Accordingly, we typically seek approximate models that we can use to estimate costs. Ļe ŀrst reason to approximate is that determining the cost details of all individual operations can be daunting in the context of the complex architectures and operating systems on modern computers. Accordingly, we typically study just a few quantities in the “inner loop” of our programs, implicitly hypothesizing that total cost is well estimated by analyzing just those quantities. Experienced programmers regularly “proŀle” their implementations to identify “bottlenecks,” which is a systematic way to identify such quantities. For example, we typically analyze compare-based sorting algorithms by just counting compares. Such an approach has the important side beneŀt that it is machine independent. Carefully analyzing the number of compares used by a sorting algorithm can enable us to predict performance on many different computers. Associated hypotheses are easily tested by experimentation, and we can reŀne them, in principle, when appropriate. For example, we might reŀne comparison-based models for sorting to include data movement, which may require taking caching effects into account.&lt;/p&gt;
&lt;h2&gt;
  
  
  2.1 Types of Cases in Algorithm Analysis
&lt;/h2&gt;

&lt;p&gt;The analysis of algorithms necessarily involves considering different scenarios under which an algorithm might execute. When we examine an algorithm's performance, we typically distinguish among several types of cases, each providing distinct insights into the algorithm's behavior. These case analyses form the foundation of our understanding of algorithmic performance and allow us to make informed decisions about algorithm selection and optimization.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.1.1 Worst-Case Analysis
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;worst-case analysis&lt;/strong&gt; of an algorithm determines the maximum number of steps (or resource consumption) that the algorithm could possibly require for any input of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. Formally, if we denote by 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(I)T(I)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 the running time of algorithm 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 on input 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;II&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;INI_N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 represents the set of all possible inputs of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, then the worst-case time complexity is:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tworst(N)=max⁡I∈INT(I).
T_{\text{worst}}(N) = \max_{I \in I_N} T(I).
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;worst&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;I&lt;/span&gt;&lt;span class="mrel mtight"&gt;∈&lt;/span&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;max&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;



&lt;p&gt;Worst-case analysis is particularly important in safety-critical systems and real-time applications where we must guarantee that the algorithm completes within a specific time bound. For example, in avionics software or medical device control systems, knowing the absolute maximum execution time is essential for system reliability.&lt;/p&gt;

&lt;p&gt;When we express worst-case complexity using asymptotic notation, we typically write 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tworst(N)=O(f(N))T_{\text{worst}}(N) = O(f(N))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;worst&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, meaning the worst-case running time grows no faster than 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(N)f(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 as 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 becomes large. For mergesort, we can show that 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tworst(N)=O(Nlog⁡N)T_{\text{worst}}(N) = O(N \log N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;worst&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, which holds regardless of the initial arrangement of the input array.&lt;/p&gt;

&lt;p&gt;The worst-case scenario often corresponds to a specific pathological input configuration. For instance, in quicksort with a naive pivot selection strategy, the worst case occurs when the array is already sorted or reverse-sorted, leading to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(N2)O(N^2)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 behavior. Identifying these worst-case inputs is itself an important part of algorithm analysis, as it can inform the design of better pivot selection strategies or other algorithmic improvements.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.1.2 Best-Case Analysis
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;best-case analysis&lt;/strong&gt; examines the minimum number of steps required by an algorithm over all possible inputs of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. Formally:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tbest(N)=min⁡I∈INT(I).
T_{\text{best}}(N) = \min_{I \in I_N} T(I).
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;best&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;I&lt;/span&gt;&lt;span class="mrel mtight"&gt;∈&lt;/span&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop"&gt;min&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;While best-case analysis might seem less practically useful than worst-case analysis, it provides valuable information in several contexts. First, it establishes a lower bound on algorithmic performance—no input can be processed faster than the best case. Second, for some algorithms, the best case occurs frequently enough in practice to be relevant. Third, understanding the best case helps us identify inputs that the algorithm handles particularly efficiently, which can inform preprocessing strategies or input transformation techniques.&lt;/p&gt;

&lt;p&gt;Consider insertion sort as an example: its best-case running time is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ(N)\Theta(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, occurring when the input array is already sorted. In this scenario, the algorithm makes exactly 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N−1N-1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 comparisons and performs no data movements beyond the overhead of the loop structure itself. This best-case behavior makes insertion sort an excellent choice for nearly-sorted data or for small arrays where the probability of encountering sorted or nearly-sorted inputs is high.&lt;/p&gt;

&lt;p&gt;Best-case analysis is often denoted using 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Ω\Omega&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Ω&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
-notation. When we write 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tbest(N)=Ω(f(N))T_{\text{best}}(N) = \Omega(f(N))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;best&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Ω&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, we indicate that even in the most favorable circumstances, the algorithm requires at least 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(N)f(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 time.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.1.3 Average-Case Analysis
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Average-case analysis&lt;/strong&gt; is perhaps the most practically relevant but also the most mathematically challenging type of analysis. It computes the expected running time of an algorithm over all possible inputs of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, weighted by the probability of encountering each input. If we assume a probability distribution 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;PP&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;P&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 over the input space 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;INI_N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, then:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tavg(N)=∑I∈INP(I)⋅T(I)=E[T(I)],
T_{\text{avg}}(N) = \sum_{I \in I_N} P(I) \cdot T(I) = \mathbb{E}[T(I)],
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;avg&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;I&lt;/span&gt;&lt;span class="mrel mtight"&gt;∈&lt;/span&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;I&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;P&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;⋅&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathbb"&gt;E&lt;/span&gt;&lt;span class="mopen"&gt;[&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mclose"&gt;)]&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;where 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;E\mathbb{E}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathbb"&gt;E&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 denotes the expectation operator.&lt;/p&gt;

&lt;p&gt;The mathematical techniques used for average-case analysis are broadly applicable to mathematical models in scientific applications ranging from genomics to statistical physics. Our prime motivation is to develop tools that enable precise statements about resource usage of important algorithms in practical applications. Average-case analysis is effective for two primary reasons.&lt;/p&gt;

&lt;p&gt;The first reason is that straightforward models of randomness are often extremely accurate. Simple random models are effective in numerous practical scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sorting is fundamental in cryptanalysis, where adversaries ensure data appears indistinguishable from random.&lt;/li&gt;
&lt;li&gt;Commercial data processing systems routinely sort massive files where keys (account numbers, identification codes) are well modeled by uniformly random numbers.&lt;/li&gt;
&lt;li&gt;Computer network implementations depend on sorts involving keys that behave like random ones.&lt;/li&gt;
&lt;li&gt;Computational biology uses sorting extensively, where deviations from randomness warrant further scientific investigation into biological and physical processes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When large datasets are created by humans, they typically involve arbitrary choices well modeled by random ones. Random models also prove effective with scientific data. We might interpret Einstein's assertion that "God does not play dice" to mean random models work precisely because significant deviations from randomness reveal something meaningful about the natural world.&lt;/p&gt;

&lt;p&gt;The second reason average-case analysis is important is that we can often inject randomness into problem instances so they appear random to both the algorithm and analyst. This approach enables efficient algorithms with predictable performance, known as &lt;strong&gt;randomized algorithms&lt;/strong&gt;. M. O. Rabin was among the first to articulate this methodology, which has been extensively developed by subsequent researchers.&lt;/p&gt;

&lt;p&gt;We typically begin by analyzing random models, starting with computing the mean—the average value of some quantity of interest for 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 instances drawn at random. Elementary probability theory offers several related approaches. We explicitly identify two:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Distributional Approach.&lt;/strong&gt; Let 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N\mathcal{N}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 be the number of possible inputs of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Nk\mathcal{N}_k&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 be the number of inputs causing the algorithm to have cost 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;kk&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, so 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N=∑kNk\mathcal{N} = \sum_k \mathcal{N}_k&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol small-op"&gt;∑&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. The probability that cost is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;kk&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Nk/N\mathcal{N}_k / \mathcal{N}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, and expected cost is:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;1N∑kkNk.
\frac{1}{\mathcal{N}} \sum_k k \mathcal{N}_k. 
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;This analysis depends on counting: How many inputs exist of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, and how many cause cost 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;kk&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
? These steps compute the probability distribution, making this the most direct approach from elementary probability theory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cumulative Approach.&lt;/strong&gt; Let 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;TN\mathcal{T}_N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 be the total (cumulated) cost on all inputs of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. (That is, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;TN=∑kkNk\mathcal{T}_N = \sum_k k \mathcal{N}_k&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol small-op"&gt;∑&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, but direct computation may proceed differently.) The average cost is simply 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;TN/N\mathcal{T}_N / \mathcal{N}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathcal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathcal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. This analysis depends on a less specific counting problem: what is the total cost across all inputs? General tools make this approach very attractive.&lt;/p&gt;

&lt;p&gt;The distributional approach provides complete information for computing standard deviation and other moments. Indirect, often simpler methods also exist for computing moments using the cumulative approach. We consider both, though we tend toward the cumulative method, which allows analyzing algorithms through combinatorial properties of basic data structures.&lt;/p&gt;

&lt;p&gt;Many algorithms solve problems by recursively solving smaller subproblems, enabling derivation of recurrence relationships that average or total cost must satisfy. Direct recurrence derivation from the algorithm is often natural.&lt;/p&gt;

&lt;p&gt;Average-case results are valuable because, where random input is reasonable, accurate analysis helps us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compare different algorithms for the same task.&lt;/li&gt;
&lt;li&gt;Predict time and space requirements for specific applications.&lt;/li&gt;
&lt;li&gt;Compare different computers running the same algorithm.&lt;/li&gt;
&lt;li&gt;Adjust algorithm parameters to optimize performance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Average-case results can be compared with empirical data to validate implementation, model, and analysis. The goal is gaining sufficient confidence to predict algorithm performance under whatever circumstances arise in particular applications. We can evaluate potential impacts of new machine architectures on important algorithms through analysis, perhaps before the architecture exists. This approach has been validated over decades: sorting algorithms first analyzed over fifty years ago remain useful for evaluating performance on contemporary computers.&lt;/p&gt;

&lt;p&gt;For average-case complexity, we often write 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tavg(N)=Θ(f(N))T_{\text{avg}}(N) = \Theta(f(N))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;avg&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 when the expected running time has tight asymptotic bounds, or 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tavg(N)=O(f(N))T_{\text{avg}}(N) = O(f(N))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;avg&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 when we have only an upper bound on the expected performance.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.1.4 Amortized Analysis
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Amortized analysis&lt;/strong&gt; differs fundamentally from the previous three cases. Rather than analyzing a single operation on a single input, amortized analysis considers the average cost per operation over a sequence of operations. The key insight is that while individual operations might occasionally be expensive, the total cost averaged over many operations remains low.&lt;/p&gt;

&lt;p&gt;Formally, if we have a sequence of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;mm&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 operations on a data structure initially of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, and the total cost of all 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;mm&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 operations is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(m,N)T(m, N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, then the &lt;strong&gt;amortized cost&lt;/strong&gt; per operation is:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tamortized=T(m,N)m.
T_{\text{amortized}} = \frac{T(m, N)}{m}.
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;amortized&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;Amortized analysis is particularly useful for analyzing data structures where occasional expensive operations are compensated by many cheap operations. The classic example is dynamic array resizing: when an array becomes full, we allocate a new array of double the size and copy all elements. Although this resize operation costs 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ(N)\Theta(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 time, it happens infrequently enough that the amortized cost of insertion remains 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(1)O(1)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.&lt;/p&gt;

&lt;p&gt;There are three primary techniques for amortized analysis:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Aggregate Method.&lt;/strong&gt; We compute the total cost of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;mm&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 operations and divide by 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;mm&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. For dynamic arrays with doubling, inserting 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 elements requires copying 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;1+2+4+⋯+N1 + 2 + 4 + \cdots + N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="minner"&gt;⋯&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 elements total, which sums to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;2N−12N - 1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. Thus the amortized cost per insertion is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(2N−1)/N=O(1)(2N - 1)/N = O(1)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accounting Method.&lt;/strong&gt; We assign different charges to different operations, some more and some less than their actual cost. The accumulated credit must always be non-negative and can be used to pay for expensive operations. For dynamic arrays, we might charge 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;33&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 units per insertion: 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;11&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 for the insertion itself, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;11&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to pay for copying this element during the next resize, and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;11&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to help pay for copying an element from the previous half of the array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Potential Method.&lt;/strong&gt; We define a potential function 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Φ\Phi&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Φ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 mapping data structure states to non-negative real numbers. The amortized cost of operation 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;ii&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;ci+Φ(Di)−Φ(Di−1)c_i + \Phi(D_i) - \Phi(D_{i-1})&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;c&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Φ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;D&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Φ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;D&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, where 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;cic_i&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;c&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is the actual cost and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;DiD_i&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;D&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 represents the data structure state after operation 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;ii&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. If we choose 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Φ\Phi&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Φ&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 appropriately (increasing during cheap operations, decreasing during expensive ones), the amortized costs smooth out variations in actual costs.&lt;/p&gt;

&lt;p&gt;It is crucial to understand that amortized cost is not the same as average-case cost. Average-case analysis considers a probability distribution over inputs, while amortized analysis considers the distribution of costs over a sequence of operations on a single input. An operation might have worst-case cost 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(N)O(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, average-case cost 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(N)O(N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, yet amortized cost 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(1)O(1)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 when analyzed as part of a sequence.&lt;/p&gt;

&lt;p&gt;Amortized analysis is typically expressed using the same asymptotic notations as other complexity measures. When we write that an operation has 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(1)O(1)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 amortized cost, we mean that over any sequence of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;mm&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 operations, the total cost is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(m)O(m)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.&lt;/p&gt;
&lt;h3&gt;
  
  
  2.1.5 Relationships Among Cases
&lt;/h3&gt;

&lt;p&gt;These four types of analysis are interconnected. For any algorithm and input size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, we have:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tbest(N)≤Tavg(N)≤Tworst(N).
T_{\text{best}}(N) \leq T_{\text{avg}}(N) \leq T_{\text{worst}}(N). 
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;best&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≤&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;avg&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≤&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;worst&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;This inequality follows from the definition of minimum, average, and maximum. The average must fall between the extremes. However, the gaps between these values can vary dramatically depending on the algorithm. For some algorithms (like mergesort), all three are asymptotically equal: 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Tbest(N)=Tavg(N)=Tworst(N)=Θ(Nlog⁡N)T_{\text{best}}(N) = T_{\text{avg}}(N) = T_{\text{worst}}(N) = \Theta(N \log N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;best&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;avg&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord text mtight"&gt;&lt;span class="mord mtight"&gt;worst&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. For others (like quicksort), the best and average cases are 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ(Nlog⁡N)\Theta(N \log N)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;lo&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 while the worst case is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ(N2)\Theta(N^2)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.&lt;/p&gt;

&lt;p&gt;Amortized analysis occupies a different conceptual space since it analyzes sequences rather than single operations. Nevertheless, for a sequence of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;mm&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;m&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 operations, we can define worst-case, average-case, and best-case amortized costs by considering different sequences or different probability distributions over sequences.&lt;/p&gt;

&lt;p&gt;Understanding all four types of analysis provides comprehensive insight into algorithm behavior. Worst-case analysis guarantees performance bounds for critical systems. Best-case analysis identifies optimal scenarios and can guide preprocessing strategies. Average-case analysis predicts typical performance when inputs follow reasonable probability distributions. Amortized analysis reveals hidden efficiency in data structures where occasional expensive operations are offset by frequent cheap ones. Together, these tools enable us to select, optimize, and reason about algorithms with precision and confidence.&lt;/p&gt;
&lt;h2&gt;
  
  
  3 Time Complexity of Algorithms
&lt;/h2&gt;

&lt;p&gt;Understanding how an algorithm's running time grows as input size increases is fundamental to algorithm analysis. This growth rate, independent of machine-specific constants, allows us to classify and compare algorithms systematically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Definition (Informal).&lt;/strong&gt; A function 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;f(n)f(n)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 such that the running time 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(n)T(n)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 of a given algorithm is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Θ(f(n))\Theta(f(n))&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;Θ&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;f&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mclose"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 measures the &lt;strong&gt;time complexity&lt;/strong&gt; of the algorithm.&lt;/p&gt;

&lt;p&gt;An algorithm is called &lt;strong&gt;polynomial time&lt;/strong&gt; if its running time 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(n)T(n)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(nk)O(n^k)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 where 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;kk&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is some fixed positive integer. A computational problem is considered &lt;strong&gt;intractable&lt;/strong&gt; if and only if no deterministic algorithm with polynomial time complexity exists for it. But many problems are classed as intractable only because a polynomial solution is unknown, and it is a very challenging task to find such a solution for one of them.&lt;/p&gt;

&lt;p&gt;Table 3.1 shows how the running time 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(n)T(n)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 of algorithms having different time complexities changes as input size grows. Each row assumes that 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(8)=1T(8) = 1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 as a baseline, then shows how 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(n)T(n)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 grows for larger inputs. The dramatic differences in growth rates—particularly the explosive growth of exponential algorithms—illustrate why algorithmic efficiency matters profoundly in practice.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table 3.1:&lt;/strong&gt; Relative growth of running time 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(n)T(n)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 when the input size increases from 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n=8n = 8&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n=1024n = 1024&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1024&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, provided that 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(8)=1T(8) = 1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time Complexity&lt;/th&gt;
&lt;th&gt;Notation&lt;/th&gt;
&lt;th&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n=8n = 8&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/th&gt;
&lt;th&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n=32n = 32&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;32&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/th&gt;
&lt;th&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n=128n = 128&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;128&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/th&gt;
&lt;th&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n=1024n = 1024&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1024&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/th&gt;
&lt;th&gt;Function&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Constant&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;11&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;11&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Logarithmic&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;lg⁡n\lg n&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1.67&lt;/td&gt;
&lt;td&gt;2.33&lt;/td&gt;
&lt;td&gt;3.33&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;lg⁡n/3\lg n / 3&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mord"&gt;/3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Log-squared&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;lg⁡2n\lg^2 n&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;2.78&lt;/td&gt;
&lt;td&gt;5.44&lt;/td&gt;
&lt;td&gt;11.1&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;lg⁡2n/9\lg^2 n / 9&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mord"&gt;/9&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linear&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;nn&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;128&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n/8n / 8&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mord"&gt;/8&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linearithmic&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;nlg⁡nn \lg n&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;6.67&lt;/td&gt;
&lt;td&gt;37.3&lt;/td&gt;
&lt;td&gt;427&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;nlg⁡n/24n \lg n / 24&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mord"&gt;/24&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Quadratic&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n2n^2&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;16,384&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n2/64n^2 / 64&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/64&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cubic&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n3n^3&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;4,096&lt;/td&gt;
&lt;td&gt;2,097,152&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;n3/512n^3 / 512&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mord"&gt;/512&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exponential&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;2n2^n&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;16,777,216&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;21252^{125}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;125&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;210212^{1021}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1021&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;2n−32^{n-3}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;n&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;3&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice how dramatically different algorithms scale: when input size increases from 8 to 1024 (a 128-fold increase), a linear algorithm takes 128 times longer, while a quadratic algorithm takes 16,384 times longer. An exponential algorithm becomes utterly impractical—
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;210212^{1021}&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1021&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is astronomically large, far exceeding the number of atoms in the observable universe. This table vividly demonstrates why we prefer 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(nlg⁡n)O(n \lg n)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 sorting algorithms over 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;O(n2)O(n^2)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;O&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;n&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 ones for large datasets, and why exponential-time algorithms are generally infeasible except for very small inputs.&lt;/p&gt;


&lt;h2&gt;
  
  
  3.2 Example: Analysis of Quicksort
&lt;/h2&gt;

&lt;p&gt;To illustrate the basic method just sketched, we examine next a particular algorithm of considerable importance, the &lt;strong&gt;quicksort&lt;/strong&gt; sorting method. This method was invented in 1962 by C. A. R. Hoare, whose paper is an early and outstanding example in the analysis of algorithms. The analysis is also covered in great detail in Sedgewick; we give highlights here. &lt;/p&gt;

&lt;p&gt;It is worthwhile to study this analysis in detail not just because this sorting method is widely used and the analytic results are directly relevant to practice, but also because the analysis itself is illustrative of many things that we will encounter later in the book. In particular, it turns out that the same analysis applies to the study of basic properties of tree structures, which are of broad interest and applicability. More generally, our analysis of quicksort is indicative of how we go about analyzing a broad class of recursive programs.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Quicksort Algorithm
&lt;/h3&gt;

&lt;p&gt;Program 3.3 is an implementation of quicksort in Java. It is a recursive program that sorts the numbers in an array by partitioning it into two independent (smaller) parts, then sorting those parts. Obviously, the recursion should terminate when empty subarrays are encountered, but our implementation also stops with subarrays of size 1. This detail might seem inconsequential at first blush, but, as we will see, the very nature of recursion ensures that the program will be used for a large number of small files, and substantial performance gains can be achieved with simple improvements of this sort.&lt;/p&gt;

&lt;p&gt;The partitioning process puts the element that was in the last position in the array (the &lt;strong&gt;partitioning element&lt;/strong&gt;) into its correct position, with all smaller elements before it and all larger elements after it. The program accomplishes this by maintaining two pointers: one scanning from the left, one from the right. The left pointer is incremented until an element larger than the partitioning element is found; the right pointer is decremented until an element smaller than the partitioning element is found. These two elements are exchanged, and the process continues until the pointers meet, which defines where the partitioning element is put. After partitioning, the program exchanges &lt;code&gt;a[i]&lt;/code&gt; with &lt;code&gt;a[hi]&lt;/code&gt; to put the partitioning element into position.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Program 3.3: Quicksort implementation in Java&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;quicksort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[++&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[--&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;];&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;quicksort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;quicksort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hi&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The call &lt;code&gt;quicksort(a, 0, N-1)&lt;/code&gt; will sort the array. There are several ways to implement the general recursive strategy just outlined; the implementation described above is taken from Sedgewick and Wayne. For the purposes of analysis, we will be assuming that the array &lt;code&gt;a&lt;/code&gt; contains randomly ordered, distinct numbers, but note that this code works properly for all inputs, including equal numbers. It is also possible to study this program under perhaps more realistic models allowing equal numbers, long string keys, and many other situations.&lt;/p&gt;
&lt;h3&gt;
  
  
  Analyzing Resource Requirements
&lt;/h3&gt;

&lt;p&gt;Once we have an implementation, the first step in the analysis is to estimate the resource requirements of individual instructions for this program. This depends on characteristics of a particular computer, so we sketch the details. For example, the "inner loop" instruction &lt;code&gt;while (a[++i] &amp;lt; v) ;&lt;/code&gt; might translate, on a typical computer, to assembly language instructions such as the following:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LOOP    INC  I,1        # increment i
        CMP  V,A(I)     # compare v with A(i)
        BL   LOOP       # branch if less
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To start, we might say that one iteration of this loop might require four time units (one for each memory reference). On modern computers, the precise costs are more complicated to evaluate because of caching, pipelines, and other effects. The other instruction in the inner loop (that decrements &lt;code&gt;j&lt;/code&gt;) is similar, but involves an extra test of whether &lt;code&gt;j&lt;/code&gt; goes out of bounds. Since this extra test can be removed via sentinels, we will ignore the extra complication it presents.&lt;/p&gt;
&lt;h3&gt;
  
  
  Defining Key Variables
&lt;/h3&gt;

&lt;p&gt;The next step in the analysis is to assign variable names to the frequency of execution of the instructions in the program. Normally there are only a few true variables involved: the frequencies of execution of all the instructions can be expressed in terms of these few. Also, it is desirable to relate the variables to the algorithm itself, not any particular program. For quicksort, three natural quantities are involved:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;AA&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 – the number of partitioning stages&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;BB&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;B&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 – the number of exchanges&lt;/li&gt;
&lt;li&gt;
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;CC&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 – the number of compares&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On a typical computer, the total running time of quicksort might be expressed with a formula, such as:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;T(N)=4C+11B+35A.
T(N) = 4C + 11B + 35A.
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;T&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;4&lt;/span&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;11&lt;/span&gt;&lt;span class="mord mathnormal"&gt;B&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;35&lt;/span&gt;&lt;span class="mord mathnormal"&gt;A&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;



&lt;p&gt;The exact values of these coefficients depend on the machine language program produced by the compiler as well as the properties of the machine being used; the values given above are typical. Such expressions are quite useful in comparing different algorithms implemented on the same machine. Indeed, the reason that quicksort is of practical interest even though mergesort is "optimal" is that the cost per compare (the coefficient of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;CC&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
) is likely to be significantly lower for quicksort than for mergesort, which leads to significantly shorter running times in typical practical applications.&lt;/p&gt;
&lt;h3&gt;
  
  
  Average-Case Analysis
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Theorem (Quicksort analysis).&lt;/strong&gt; Quicksort uses, on the average, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(N−1)/2(N - 1)/2&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;/2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 partitioning stages, 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;2(N+1)(HN+1−3/2)≈2Nln⁡N−1.846N2(N + 1)(H_{N+1} - 3/2) \approx 2N\ln N - 1.846N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;H&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="mbin mtight"&gt;+&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;3/2&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≈&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;ln&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1.846&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 compares, and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(N+1)(HN+1−3)/3+1≈0.333Nln⁡N−0.865N(N + 1)(H_{N+1} - 3)/3 + 1 \approx 0.333N\ln N - 0.865N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;H&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="mbin mtight"&gt;+&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;/3&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≈&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0.333&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;ln&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0.865&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 exchanges to sort an array of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 randomly ordered distinct elements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Proof.&lt;/strong&gt; The exact answers here are expressed in terms of the &lt;strong&gt;harmonic numbers&lt;/strong&gt; 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;HN=∑1≤k≤N1/kH_N = \sum_{1 \leq k \leq N} 1/k&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;H&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;&lt;span class="mop op-symbol small-op"&gt;∑&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mrel mtight"&gt;≤&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;span class="mrel mtight"&gt;≤&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
, the first of many well-known "special" number sequences that we will encounter in the analysis of algorithms. As with mergesort, the analysis of quicksort involves defining and solving recurrence relations that mirror directly the recursive nature of the algorithm. But, in this case, the recurrences must be based on probabilistic statements about the inputs.&lt;/p&gt;

&lt;p&gt;If 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;CNC_N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 is the average number of compares to sort 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 elements, we have 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;C0=C1=0C_0 = C_1 = 0&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;CN=N+1+1N∑1≤j≤N(Cj−1+CN−j),for N&amp;gt;1.
C_N = N + 1 + \frac{1}{N} \sum_{1 \leq j \leq N} (C_{j-1} + C_{N-j}), \quad \text{for } N &amp;gt; 1. 
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mrel mtight"&gt;≤&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;j&lt;/span&gt;&lt;span class="mrel mtight"&gt;≤&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;j&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;for &lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;To get the total average number of compares, we add the number of compares for the first partitioning stage (
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N+1N + 1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
) to the number of compares used for the subarrays after partitioning. When the partitioning element is the 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;jj&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
th largest (which occurs with probability 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;1/N1/N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1/&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 for each 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;1≤j≤N1 \leq j \leq N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≤&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;j&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≤&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
), the subarrays after partitioning are of size 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;j−1j - 1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;j&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N−jN - j&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
.&lt;/p&gt;

&lt;p&gt;Now the analysis has been reduced to a mathematical problem that does not depend on properties of the program or the algorithm. This recurrence relation is somewhat more complicated than (1.1) because the right-hand side depends directly on the history of all the previous values, not just a few. Still, is not difficult to solve: first change 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;jj&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;j&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N−j+1N - j + 1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;j&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 in the second part of the sum to get&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;CN=N+1+2N∑1≤j≤NCj−1,for N&amp;gt;0.
C_N = N + 1 + \frac{2}{N} \sum_{1 \leq j \leq N} C_{j-1}, \quad \text{for } N &amp;gt; 0.
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;span class="mrel mtight"&gt;≤&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;j&lt;/span&gt;&lt;span class="mrel mtight"&gt;≤&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;j&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;for &lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;Then multiply by 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 and subtract the same formula for 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N−1N - 1&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 to eliminate the sum:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NCN−(N−1)CN−1=2N+2CN−1,for N&amp;gt;1.
NC_N - (N - 1)C_{N-1} = 2N + 2C_{N-1}, \quad \text{for } N &amp;gt; 1.
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;−&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;for &lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;Now rearrange terms to get a simple recurrence:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NCN=(N+1)CN−1+2N,for N&amp;gt;1.
NC_N = (N + 1)C_{N-1} + 2N, \quad \text{for } N &amp;gt; 1.
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;for &lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;This can be solved by dividing both sides by 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;N(N+1)N(N + 1)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
:&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;CNN+1=CN−1N+2N+1,for N&amp;gt;1.
\frac{C_N}{N + 1} = \frac{C_{N-1}}{N} + \frac{2}{N + 1}, \quad \text{for } N &amp;gt; 1.
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="mbin mtight"&gt;−&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mpunct"&gt;,&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord text"&gt;&lt;span class="mord"&gt;for &lt;/span&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;Iterating, we are left with the sum&lt;/p&gt;


&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;CNN+1=C12+2∑3≤k≤N+11k
\frac{C_N}{N + 1} = \frac{C_1}{2} + 2\sum_{3 \leq k \leq N+1} \frac{1}{k}
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mtight"&gt;3&lt;/span&gt;&lt;span class="mrel mtight"&gt;≤&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;k&lt;/span&gt;&lt;span class="mrel mtight"&gt;≤&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="mbin mtight"&gt;+&lt;/span&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;k&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;p&gt;which completes the proof, since 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;C1=0C_1 = 0&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;C&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
. As implemented earlier, every element is used for partitioning exactly once, so the number of stages is always 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;NN&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
; the average number of exchanges can be found from these results by first calculating the average number of exchanges on the first partitioning stage. The stated approximations follow from the well-known approximation to the harmonic number 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;HN≈ln⁡N+0.57721…H_N \approx \ln N + 0.57721\ldots&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;H&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;≈&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mop"&gt;ln&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mbin"&gt;+&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0.57721&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="minner"&gt;…&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 (the Euler-Mascheroni constant). We consider such approximations below and in detail in Chapter 4. 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;□\square&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord amsrm"&gt;□&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;This analysis reveals that quicksort performs approximately 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;2Nln⁡N2N \ln N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;ln&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 compares on average, which is only about 39% more than the theoretical minimum of 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Nlg⁡NN \lg N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;l&lt;span&gt;g&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 compares required by any comparison-based sorting algorithm. Combined with its low overhead per comparison (the small coefficient in equation), quicksort achieves excellent practical performance despite lacking the worst-case guarantee of mergesort.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concluding Remarks
&lt;/h2&gt;

&lt;p&gt;The analysis of quicksort presented in Section 3.2, with its intricate recurrence relations and harmonic number summations, exemplifies both the power and the challenge of rigorous algorithm analysis. We have witnessed how a deceptively simple recursive partitioning strategy unfolds into a sophisticated mathematical framework requiring techniques from probability theory, combinatorics, and asymptotic analysis. The journey from Hoare's elegant 1962 invention to the precise statement that quicksort performs approximately 
&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;2Nln⁡N2N \ln N&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;2&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop"&gt;ln&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;N&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
 comparisons on average illustrates the depth of insight that careful analysis can provide—and hints at the vastness of what remains unexplored.&lt;br&gt;
Indeed, this article has necessarily omitted far more than it has covered. We have barely scratched the surface of space complexity analysis, have not touched upon the rich theory of lower bounds for computational problems, and have left entirely unexamined vast algorithmic paradigms such as dynamic programming, greedy algorithms, graph algorithms, and the profound connections between data structures and their associated operations. The analysis techniques we have introduced—loop invariants, recurrence solving, probabilistic averaging—extend to contexts we have not mentioned: parallel algorithms, randomized algorithms, online algorithms, approximation algorithms, and the intricate dance between theory and practice in algorithm engineering.&lt;br&gt;
For those whose curiosity has been awakened rather than sated, the references cited throughout this text offer doorways to deeper understanding. Knuth's magisterial The Art of Computer Programming remains the gold standard for mathematical rigor in algorithm analysis. Sedgewick and Wayne provide accessible yet thorough treatments of practical algorithms and their implementations. Cormen, Leiserson, Rivest, and Stein's Introduction to Algorithms covers an encyclopedic range of topics with consistent clarity. Each of these works, and the many others cited in our references, represents decades of accumulated wisdom from the computer science community.&lt;br&gt;
The beauty of algorithm analysis lies precisely in this: there is always more to discover. A simple question—"How fast does this algorithm run?"—can lead to elegant mathematical theorems, surprising connections between seemingly unrelated problems, and practical insights that transform how we build software systems. Every algorithm analyzed rigorously is a small victory for human understanding over computational complexity.&lt;br&gt;
If this exploration has resonated with you, if you find yourself curious about the mathematics underlying the software that increasingly shapes our world, then this article has achieved its purpose. The journey from informal algorithmic thinking to precise mathematical reasoning is challenging, occasionally frustrating, but ultimately deeply rewarding. We invite you to continue this journey, to explore the references, to implement and analyze algorithms yourself, and to join the ongoing conversation about computational complexity that has animated computer science for over half a century.&lt;br&gt;
Your thoughts, questions, critiques, and insights are valuable. If this article has sparked ideas or raised questions, consider sharing them in the comments below. Whether you found the material illuminating or perplexing, too elementary or too advanced, your feedback helps shape future discussions. And if you believe others might benefit from this introduction to algorithm analysis, a like or share helps this work reach fellow travelers on the path of computational understanding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Preview (PagePlace)&lt;/strong&gt; — &lt;em&gt;Preview of book (ISBN: 9780133373479).&lt;/em&gt;&lt;br&gt;
&lt;a href="https://api.pageplace.de/preview/DT0400.9780133373479_A23602754/preview-9780133373479_A23602754.pdf" rel="noopener noreferrer"&gt;https://api.pageplace.de/preview/DT0400.9780133373479_A23602754/preview-9780133373479_A23602754.pdf&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Logic Symbols — List&lt;/strong&gt; — &lt;em&gt;Logic symbols and notation (reference sheet / cheat-sheet).&lt;/em&gt;&lt;br&gt;
&lt;a href="https://www.basicknowledge101.com/pdf/km/logic_symbols%20list.pdf" rel="noopener noreferrer"&gt;https://www.basicknowledge101.com/pdf/km/logic_symbols%20list.pdf&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Soltys, M. &amp;amp; Kulinicz, ? — &lt;em&gt;An Introduction to the Analysis of Algorithms&lt;/em&gt; (World Scientific, 2018)&lt;/strong&gt; — &lt;em&gt;PDF copy.&lt;/em&gt;&lt;br&gt;
&lt;a href="https://lib.zu.edu.pk/ebookdata/Engineering/Data%20Science/An%20introduction%20to%20the%20analysis%20of%20algorithms-World%20Scientific%20Publishing%20Co%20Pte%20Ltd%20BY%20Soltys-Kulinicz,%20Michael%20-%20%282018%29.pdf" rel="noopener noreferrer"&gt;https://lib.zu.edu.pk/ebookdata/Engineering/Data%20Science/An%20introduction%20to%20the%20analysis%20of%20algorithms-World%20Scientific%20Publishing%20Co%20Pte%20Ltd%20BY%20Soltys-Kulinicz,%20Michael%20-%20(2018).pdf&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Introduction to the Analysis of Algorithms (PDF)&lt;/strong&gt; — &lt;em&gt;Lecture notes / textbook (site: erode-sengunthar.ac.in).&lt;/em&gt;&lt;br&gt;
&lt;a href="https://erode-sengunthar.ac.in/wp-content/uploads/2023/07/Introduction-to-Analysis-of-Algorithms-2.pdf" rel="noopener noreferrer"&gt;https://erode-sengunthar.ac.in/wp-content/uploads/2023/07/Introduction-to-Analysis-of-Algorithms-2.pdf&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Structures / Algorithms (DGW2)&lt;/strong&gt; — &lt;em&gt;Textbook PDF from University of Auckland (cs.auckland).&lt;/em&gt;&lt;br&gt;
&lt;a href="https://www.cs.auckland.ac.nz/textbookCS220/ebook/DGW2.pdf" rel="noopener noreferrer"&gt;https://www.cs.auckland.ac.nz/textbookCS220/ebook/DGW2.pdf&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>programming</category>
      <category>tutorial</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>My personal website</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Tue, 10 Jun 2025 23:47:23 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/my-personal-website-3lg1</link>
      <guid>https://forem.com/m__mdy__m/my-personal-website-3lg1</guid>
      <description>&lt;p&gt;I’ve finally put together a &lt;strong&gt;minimal, terminal-inspired website&lt;/strong&gt; for myself at &lt;a href="https://m-mdy-m.github.io/" rel="noopener noreferrer"&gt;m-mdy-m.github.io&lt;/a&gt; (code lives here ► &lt;a href="https://github.com/m-mdy-m/m-mdy-m.github.io" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;). It’s built with GitHub Pages so it’s free, dead simple to deploy, and completely CLI-themed—think of it as a little shell where you can &lt;code&gt;ls&lt;/code&gt; my posts, projects, and articles.&lt;/p&gt;

&lt;p&gt;So far, I’ve published &lt;strong&gt;a handful of what I hope are useful articles&lt;/strong&gt; (everything from “What Is an Algorithm?” to deep dives on data structures), and you’ll find them under &lt;strong&gt;Articles&lt;/strong&gt;. Under &lt;strong&gt;Projects&lt;/strong&gt;, it currently shows only &lt;strong&gt;ARLIZ&lt;/strong&gt;—my ongoing book on arrays, logic, identity, and zero (a journey through DS&amp;amp;A from first principles to real-world code). But there’s plenty more on the way: &lt;strong&gt;GLAND&lt;/strong&gt;, &lt;strong&gt;agas&lt;/strong&gt;, &lt;strong&gt;nexa&lt;/strong&gt;, &lt;strong&gt;techshelf&lt;/strong&gt;, &lt;strong&gt;qiks&lt;/strong&gt;, &lt;strong&gt;TideityIQ&lt;/strong&gt;, and a slew of half-baked experiments, demos, and micro-projects to fill in the gaps.&lt;/p&gt;

&lt;p&gt;Everything’s laid out in plain text—no distractions, no flashy themes—just a prompt, a blinking cursor, and your curiosity. If you see any typos, missing links, or broken demos, feel free to open an &lt;strong&gt;Issue&lt;/strong&gt; or send a &lt;strong&gt;PR&lt;/strong&gt;—I’d love your help squashing bugs, improving content, or even adding entirely new sections.&lt;/p&gt;

&lt;p&gt;If you enjoyed poking around, please ⭐ the repo and drop your thoughts, suggestions, or criticisms in the Issues. Your feedback helps me make this little CLI shell even better. Thanks for stopping by!&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>discuss</category>
      <category>career</category>
      <category>programming</category>
    </item>
    <item>
      <title>Protocol Agnostic Framework</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Wed, 09 Apr 2025 20:55:17 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/protocol-agnostic-framework-9p2</link>
      <guid>https://forem.com/m__mdy__m/protocol-agnostic-framework-9p2</guid>
      <description>&lt;p&gt;Hey everyone!&lt;/p&gt;

&lt;p&gt;After a lot of &lt;strong&gt;R&amp;amp;D&lt;/strong&gt;, multiple design iterations, and endless hours of experimentation, I’m super excited to share some big updates on &lt;strong&gt;Gland&lt;/strong&gt;. Over the past few weeks, I’ve been hard at work refactoring and rethinking the core of the framework—and now I can confidently say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Gland is now fully protocol-agnostic and 100% event-driven.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Inspired by architectures like &lt;strong&gt;NestJS&lt;/strong&gt; and &lt;strong&gt;Angular&lt;/strong&gt;, Gland takes the idea of dependency injection and modularity one step further—with a twist that makes it different from anything else out there.&lt;/p&gt;

&lt;p&gt;In my &lt;a href="https://dev.to/m__mdy__m/new-web-framework-42f5"&gt;previous post&lt;/a&gt;, I introduced Gland and talked about its event-driven nature. Back then, Gland was already lightweight, minimal, and built with almost no external dependencies. But even so, it still had a tight coupling to HTTP.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;That changes now.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  So, what’s new?
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Protocol Agnosticism&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;One of the biggest milestones in Gland’s journey so far: &lt;strong&gt;The framework is no longer tied to HTTP at all.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
The &lt;code&gt;@glandjs/core&lt;/code&gt; package has been completely redesigned to be &lt;strong&gt;totally independent of any transport layer or protocol&lt;/strong&gt;—whether it’s HTTP, WebSocket, RPC, MQTT, or something else.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Introducing Broker Adapters&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;I’ve introduced a new concept called &lt;strong&gt;Broker Adapters&lt;/strong&gt;—they serve as bridges between your protocol of choice and Gland’s event system. Want to use HTTP? Create an adapter for it. Looking to work with WebSocket, Fastify, or MQTT? Just build an adapter for your needs.&lt;/p&gt;

&lt;p&gt;Right now, there’s a working adapter for &lt;strong&gt;Express&lt;/strong&gt; via &lt;a href="https://github.com/glandjs/http/tree/main/packages/express" rel="noopener noreferrer"&gt;&lt;code&gt;@glandjs/express&lt;/code&gt;&lt;/a&gt;. Up next is support for &lt;strong&gt;Fastify&lt;/strong&gt;, followed by additional protocols like &lt;strong&gt;WebSocket&lt;/strong&gt; and &lt;strong&gt;MQTT&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;HTTP Abstraction Layer&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The old &lt;code&gt;@glandjs/http&lt;/code&gt; package has been &lt;strong&gt;split and abstracted&lt;/strong&gt;. It no longer implements a specific HTTP server—&lt;strong&gt;instead, it provides the decorators and structure&lt;/strong&gt; (such as &lt;code&gt;@Get()&lt;/code&gt;, &lt;code&gt;@Post()&lt;/code&gt;, etc.). The actual server implementation now lives in protocol-specific adapters like Express or Fastify.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Still 100% Event-Driven (EDS)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Gland remains completely based on an &lt;strong&gt;Event-Driven System (EDS)&lt;/strong&gt;. That means everything—from controller calls to the internal logic—is handled through &lt;strong&gt;events&lt;/strong&gt;, not direct method calls. This architecture makes the framework immensely modular, scalable, and flexible.&lt;/p&gt;

&lt;p&gt;Here’s an updated and working example using Express:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@glandjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@glandjs/http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;GlandFactory&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@glandjs/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ExpressBroker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ExpressContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@glandjs/express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ExpressContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;controllers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UserController&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;GlandFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connectTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ExpressBroker&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// completely type-safety.&lt;/span&gt;
  &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup, you can use Gland with &lt;strong&gt;any protocol&lt;/strong&gt; simply by plugging in the appropriate adapter. Express works today, Fastify is coming next, then WebSocket, and beyond—whatever you need!&lt;/p&gt;

&lt;h3&gt;
  
  
  What does this mean for Gland’s future?
&lt;/h3&gt;

&lt;p&gt;Gland is now about &lt;strong&gt;80% feature-complete&lt;/strong&gt; in terms of its architectural vision. The remaining work focuses on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Polishing the APIs
&lt;/li&gt;
&lt;li&gt;Stabilizing the event system
&lt;/li&gt;
&lt;li&gt;Finalizing integrations with more protocols
&lt;/li&gt;
&lt;li&gt;Building comprehensive documentation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The core pillars—&lt;strong&gt;DI&lt;/strong&gt;, &lt;strong&gt;modular architecture&lt;/strong&gt;, &lt;strong&gt;protocol independence&lt;/strong&gt;, and an &lt;strong&gt;event-based flow&lt;/strong&gt;—are now rock solid. And yes, you can totally consider using Gland in production if its approach fits your use case. The aim is to offer a minimal yet powerful toolset that evolves as you do.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s Next?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fastify support&lt;/strong&gt; – Already in the works
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WebSocket adapter&lt;/strong&gt; – Planned after Fastify
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Docs + Website&lt;/strong&gt; – Being built with Astro and GitHub Pages
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Testing and Stabilization&lt;/strong&gt; – Ensuring Gland is more battle-tested for production scenarios
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Let’s Discuss &amp;amp; Contribute!
&lt;/h3&gt;

&lt;p&gt;I’m really excited about this new phase of Gland and would love to hear your thoughts.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Do you find the protocol-agnostic approach as compelling as I do?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What potential challenges do you foresee in an event-driven system like this?&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How would you like to contribute—whether in code, documentation, or feedback?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re as passionate about modular, protocol-free, event-driven architectures as I am, please join the conversation and help shape the future of Gland.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Star the repo:&lt;/strong&gt; &lt;a href="https://github.com/glandjs/gland" rel="noopener noreferrer"&gt;github.com/glandjs/gland&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Join our Discord community:&lt;/strong&gt; &lt;a href="https://discord.gg/GtRtkrEYwR" rel="noopener noreferrer"&gt;Discord Server&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contribute docs, adapters, or examples:&lt;/strong&gt; Your ideas and PRs are always welcome!
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check out our in-progress docs:&lt;/strong&gt; &lt;a href="https://github.com/glandjs/docs" rel="noopener noreferrer"&gt;github.com/glandjs/docs&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The document is not ready yet, but I am looking for a good structure and then I will pursue creating the document and writing its content.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I’d really love to get your detailed feedback on these changes, and I’m eager to discuss how we can improve Gland together. Let’s start a conversation—what are your thoughts about this new direction? Have you encountered any interesting challenges or opportunities in event-driven design that you’d like to share?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>opensource</category>
      <category>programming</category>
      <category>typescript</category>
    </item>
    <item>
      <title>What is an algorithm really?</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Mon, 31 Mar 2025 23:34:46 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/what-is-an-algorithm-really-5agc</link>
      <guid>https://forem.com/m__mdy__m/what-is-an-algorithm-really-5agc</guid>
      <description>&lt;p&gt;You've probably seen and heard the phrase &lt;strong&gt;"What is an algorithm?"&lt;/strong&gt; countless times. Even I have attempted to explain it before. But it has always felt... incomplete to me. I wasn’t just looking for a definition—I was searching for its &lt;strong&gt;philosophy&lt;/strong&gt;. And that’s exactly why it still feels &lt;strong&gt;mysterious&lt;/strong&gt; to me.  &lt;/p&gt;

&lt;p&gt;Questions like:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why the word &lt;strong&gt;algorithm&lt;/strong&gt;?
&lt;/li&gt;
&lt;li&gt;Why is the term so deeply tied to &lt;strong&gt;computer science&lt;/strong&gt;?
&lt;/li&gt;
&lt;li&gt;Why do algorithms have to be &lt;strong&gt;step-by-step&lt;/strong&gt;?
&lt;/li&gt;
&lt;li&gt;Why must an algorithm always have an &lt;strong&gt;end&lt;/strong&gt;?
&lt;/li&gt;
&lt;li&gt;How do we decide if an algorithm is &lt;strong&gt;good&lt;/strong&gt; or &lt;strong&gt;bad&lt;/strong&gt;?
&lt;/li&gt;
&lt;li&gt;How do we find and choose the &lt;strong&gt;right algorithm&lt;/strong&gt; for a real-world problem?
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And most importantly:&lt;br&gt;&lt;br&gt;
Why are all the explanations and articles out there so &lt;strong&gt;academic&lt;/strong&gt; and never truly satisfying?  &lt;/p&gt;

&lt;p&gt;I’ve had these questions for a long time. And here, I’ll try to share everything I’ve learned so far.&lt;br&gt;&lt;br&gt;
If I get something wrong, I’d love for you to correct me!  &lt;/p&gt;

&lt;h1&gt;
  
  
  What is an algorithm really?
&lt;/h1&gt;

&lt;p&gt;It’s actually &lt;strong&gt;very simple&lt;/strong&gt;. Anything that leads us to a &lt;strong&gt;solution&lt;/strong&gt; is an algorithm.  &lt;/p&gt;

&lt;p&gt;Let’s say I want to &lt;strong&gt;drink coffee&lt;/strong&gt;. What’s my process?&lt;br&gt;&lt;br&gt;
I have to:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Stand up&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Walk to the coffee&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Grab a cup&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pour the coffee&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Drink it&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Done.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s an algorithm. I followed a set of &lt;strong&gt;steps&lt;/strong&gt; to reach my goal: drinking coffee.  &lt;/p&gt;

&lt;p&gt;But here’s the thing—&lt;strong&gt;was this an important problem?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Not really.  &lt;/p&gt;

&lt;p&gt;The point is, algorithms exist &lt;strong&gt;everywhere&lt;/strong&gt;, not just in computer science. They are the &lt;strong&gt;processes&lt;/strong&gt; we use to get things done—whether it’s making coffee, solving a math problem, or navigating through a maze of decisions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithm definition in CS
&lt;/h2&gt;

&lt;p&gt;But what exactly is the definition of an algorithm &lt;strong&gt;in computer science (CS)?&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;In CS, an &lt;strong&gt;algorithm&lt;/strong&gt; is a &lt;strong&gt;sequence of instructions&lt;/strong&gt; that a computer follows to solve a &lt;strong&gt;well-defined&lt;/strong&gt; problem. It basically tells the computer &lt;strong&gt;what to do&lt;/strong&gt; and &lt;strong&gt;how to do it&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;Algorithms can instruct a computer on how to:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Perform calculations&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Process data&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make decisions&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every program, every function, and every piece of logic in a computer ultimately boils down to &lt;strong&gt;an algorithm&lt;/strong&gt;—a structured, step-by-step approach to solving a problem efficiently.&lt;/p&gt;

&lt;h2&gt;
  
  
  The story of the algorithm
&lt;/h2&gt;

&lt;p&gt;Alright, so we’ve covered what an algorithm is in CS. But before diving deeper, let’s take a step back—why the heck is it even called an &lt;strong&gt;algorithm&lt;/strong&gt;? Why not &lt;strong&gt;Aglagorikhtam&lt;/strong&gt; or something equally ridiculous?  &lt;/p&gt;

&lt;p&gt;Well, the story of the word "algorithm" goes back &lt;strong&gt;over 1200 years&lt;/strong&gt;. And believe it or not, it comes from the &lt;strong&gt;name of a real person&lt;/strong&gt;: &lt;strong&gt;Muhammad ibn Musa al-Khwarizmi&lt;/strong&gt;—a Persian mathematician, astronomer, and geographer from the &lt;strong&gt;9th century&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;He wrote a book about &lt;strong&gt;Hindu-Arabic numerals and computational methods&lt;/strong&gt;, which was later translated into &lt;strong&gt;Latin&lt;/strong&gt;. And during that translation, his name &lt;strong&gt;"Al-Khwarizmi"&lt;/strong&gt; was adapted as &lt;strong&gt;"Algoritmi"&lt;/strong&gt; in Latin. Eventually, over time, the term evolved into &lt;strong&gt;"algorithm"&lt;/strong&gt;, and now it’s a fundamental concept in computer science.  &lt;/p&gt;

&lt;p&gt;So, essentially, &lt;strong&gt;“algorithm”&lt;/strong&gt; literally means &lt;strong&gt;“a computational method inspired by Al-Khwarizmi”&lt;/strong&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  But why didn’t we end up with a word like "Aglagorikhtam"? 🤔
&lt;/h3&gt;

&lt;p&gt;Well, first of all—because &lt;strong&gt;nobody’s name was Aglagorikhtam&lt;/strong&gt;! 😂 And second, back in the day, Latin (the scientific language of Europe at the time) had its own &lt;strong&gt;rules for adapting foreign names&lt;/strong&gt;. If things had gone differently, we might’ve ended up using something like &lt;strong&gt;“Al-Khwarizmi-Method”&lt;/strong&gt; or &lt;strong&gt;“Khwarizmi-calculus”&lt;/strong&gt; instead.  &lt;/p&gt;

&lt;p&gt;So, the next time you hear the word &lt;strong&gt;algorithm&lt;/strong&gt;, remember—you’re using a term that’s been around for centuries, &lt;strong&gt;rooted in the work of an ancient mathematician&lt;/strong&gt;. Pretty cool, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is this damn "algorithm" only heard so much in CS?
&lt;/h2&gt;

&lt;p&gt;I mean, isn't an algorithm just a &lt;strong&gt;way to reach a solution&lt;/strong&gt;? If that's the case, then every action we take in life is basically an algorithm, right?  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When I go eat food, I'm running the &lt;strong&gt;"eating algorithm."&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;When I play soccer, I'm executing the &lt;strong&gt;"soccer-playing algorithm."&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, &lt;strong&gt;why the hell&lt;/strong&gt; is it that if you ask someone to "explain the soccer-playing algorithm," instead of answering, they’ll just &lt;strong&gt;kick you in the face&lt;/strong&gt;?! Why is this concept &lt;strong&gt;so big in computer science&lt;/strong&gt;, but in real life, no one seems to care?  &lt;/p&gt;

&lt;p&gt;Well, the answer lies in the &lt;strong&gt;difference between humans and computers&lt;/strong&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Humans vs. Computers: The Algorithmic Divide&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;We, as humans, do &lt;strong&gt;a lot of things instinctively&lt;/strong&gt;. We don’t consciously think in step-by-step instructions all the time. If you’re playing soccer, you’re &lt;strong&gt;not&lt;/strong&gt; going,  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Move toward the ball
&lt;/li&gt;
&lt;li&gt;Adjust foot angle
&lt;/li&gt;
&lt;li&gt;Calculate optimal force
&lt;/li&gt;
&lt;li&gt;Execute kick
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No! Your brain &lt;strong&gt;just does it&lt;/strong&gt;, automatically, without you having to analyze every tiny step.  &lt;/p&gt;

&lt;p&gt;But computers? &lt;strong&gt;They're dumb.&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
They can’t do &lt;strong&gt;anything&lt;/strong&gt; without being explicitly told &lt;strong&gt;how&lt;/strong&gt; to do it. If you don’t give them a crystal-clear step-by-step breakdown, they’ll just sit there like an idiot, waiting for instructions.  &lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;computer science (CS)&lt;/strong&gt; comes into play. Since computers &lt;strong&gt;lack intuition&lt;/strong&gt;, we have to translate our human-level understanding into &lt;strong&gt;explicit, unambiguous steps&lt;/strong&gt; that a machine can actually follow.  &lt;/p&gt;

&lt;p&gt;For example, if you want to program a robot to play soccer, you &lt;strong&gt;can’t just tell it&lt;/strong&gt;:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Hey, go play like Messi!"  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The robot would just stand there, clueless. Instead, you have to break it down like:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Detect the ball's position using sensors
&lt;/li&gt;
&lt;li&gt;Calculate its distance from the ball
&lt;/li&gt;
&lt;li&gt;Move toward the ball while avoiding obstacles
&lt;/li&gt;
&lt;li&gt;Determine the best angle and force for a shot
&lt;/li&gt;
&lt;li&gt;Execute a kick using a precise motor function
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;See the difference?&lt;br&gt;&lt;br&gt;
A human &lt;strong&gt;just plays&lt;/strong&gt;. A computer &lt;strong&gt;needs a structured set of steps&lt;/strong&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The Reality: Algorithms Are Everywhere, But We Don't Notice&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The thing is, algorithms &lt;strong&gt;do exist in real life&lt;/strong&gt;, but we don’t think of them as "algorithms" because &lt;strong&gt;we internalize them&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
For us, "playing soccer" is an &lt;strong&gt;intuitive&lt;/strong&gt; process that happens in our brain.&lt;br&gt;&lt;br&gt;
For a computer, "playing soccer" is a &lt;strong&gt;complex&lt;/strong&gt; problem that needs an explicit solution.  &lt;/p&gt;

&lt;p&gt;That's why &lt;strong&gt;algorithms in real life are invisible&lt;/strong&gt;, but in CS, they’re &lt;strong&gt;everything&lt;/strong&gt;. In the world of programming, without an algorithm, a computer is just a useless box of silicon and wires.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does the algorithm have to end?
&lt;/h3&gt;

&lt;p&gt;If an algorithm doesn’t have an end, it means you’ve entered an &lt;strong&gt;infinite loop&lt;/strong&gt;. Imagine you’re using Google Maps for directions, and it tells you:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Turn left"
&lt;/li&gt;
&lt;li&gt;"Now turn right"
&lt;/li&gt;
&lt;li&gt;"Now turn around"
&lt;/li&gt;
&lt;li&gt;"Now turn left again"
&lt;/li&gt;
&lt;li&gt;And the cycle repeats forever!
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Result? You’ll never &lt;strong&gt;reach your destination&lt;/strong&gt;. You’re stuck in an endless loop.  &lt;/p&gt;

&lt;p&gt;The same thing happens with computers. If you design an algorithm that doesn’t end, your program might &lt;strong&gt;never stop&lt;/strong&gt;. It’ll keep consuming RAM and CPU, eventually making the system freeze or crash. That’s why &lt;strong&gt;a good algorithm must have a clear end point&lt;/strong&gt;. After a certain number of steps, it should produce the final output and terminate.  &lt;/p&gt;

&lt;p&gt;Think of it this way: when you’re writing an algorithm, you’re essentially talking to a &lt;strong&gt;three-year-old kid&lt;/strong&gt; who has no idea about the world. If you miss a single step or give vague instructions, that kid will get confused and do things wrong. The computer is &lt;strong&gt;exactly&lt;/strong&gt; like that kid, just with the ability to process information at the speed of light! So, if your algorithm doesn’t have a definite end, it will never know when to stop and just keep running, wasting resources.&lt;/p&gt;

&lt;p&gt;In the world of algorithms, &lt;strong&gt;the end is just as important as the beginning&lt;/strong&gt;. Without it, you’re just chasing your own tail.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why are some algorithms approximate, while the definition of an algorithm emphasizes exact answers? 🤔
&lt;/h1&gt;

&lt;p&gt;When we define an algorithm, we often say that "the final result should be visible and predictable," meaning the algorithm should have a defined process that leads to a result we can understand or expect. However, this doesn't necessarily mean the result must always be &lt;strong&gt;exact&lt;/strong&gt;. What matters is that the algorithm provides a &lt;strong&gt;clear and repeatable process&lt;/strong&gt; that gives us an &lt;strong&gt;acceptable solution&lt;/strong&gt;. But then, a big question arises:&lt;/p&gt;

&lt;h3&gt;
  
  
  Why are some algorithms approximate instead of exact?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Time and resource constraints&lt;/strong&gt;&lt;br&gt;
Some problems are so complex and computationally heavy that finding an exact solution within a reasonable time frame is &lt;strong&gt;impossible&lt;/strong&gt;. For instance, the &lt;strong&gt;Knapsack Problem&lt;/strong&gt; in its general form is &lt;strong&gt;NP-Complete&lt;/strong&gt;. This means that, to find the exact solution, you'd have to evaluate all possible combinations, which becomes &lt;strong&gt;exponentially slow&lt;/strong&gt; for large inputs. Instead of waiting hours or even years for a solution, we use &lt;strong&gt;approximate algorithms&lt;/strong&gt; or &lt;strong&gt;heuristics&lt;/strong&gt; that provide a "good enough" solution in a much shorter time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Nature of the problem itself&lt;/strong&gt; &lt;br&gt;
Some problems are inherently &lt;strong&gt;non-deterministic&lt;/strong&gt;, meaning they don’t always have a single, well-defined answer. Even if we write an exact algorithm for these problems, we might not always get a &lt;strong&gt;perfect solution&lt;/strong&gt;. For example, in &lt;strong&gt;image processing&lt;/strong&gt;, algorithms that detect faces can’t definitively say if the face belongs to a particular person. Instead, they provide a &lt;strong&gt;high probability&lt;/strong&gt; that the face matches a known individual. This makes the algorithm approximate, rather than exact, because there's no single, definitive answer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. "Approximate answer" is better than "no answer"&lt;/strong&gt;&lt;br&gt;
Imagine searching for something on Google, and it tells you: &lt;strong&gt;"No results found!"&lt;/strong&gt; 😐 That would be a total letdown! This is why &lt;strong&gt;search algorithms&lt;/strong&gt;, even when they can’t find an exact match, try to offer the &lt;strong&gt;best possible result&lt;/strong&gt;. In situations like this, providing an approximate answer is better than having no answer at all. The algorithm might not give you the exact thing you're looking for, but it gets you as close as possible, which is often good enough.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Randomized algorithms and machine learning&lt;/strong&gt;&lt;br&gt;
Some algorithms, like &lt;strong&gt;randomized algorithms&lt;/strong&gt; or those based on &lt;strong&gt;machine learning&lt;/strong&gt;, don’t always produce fixed or exact results. For example, a &lt;strong&gt;genetic algorithm&lt;/strong&gt; used to find the optimal path in a network might give you a slightly different solution every time it runs. While it doesn’t guarantee the &lt;strong&gt;perfect&lt;/strong&gt; answer, it’s designed to find a very good solution. In cases like these, the algorithm doesn’t aim for perfection; it seeks a &lt;strong&gt;great enough&lt;/strong&gt; answer that can be applied in a reasonable amount of time, making it useful even in complex, unpredictable environments.&lt;/p&gt;

&lt;p&gt;In essence, approximate algorithms are often a &lt;strong&gt;pragmatic approach&lt;/strong&gt;. They acknowledge that, for many real-world problems, finding a perfect solution isn't feasible, so they aim for the &lt;strong&gt;best possible solution&lt;/strong&gt; within the constraints of time, resources, and uncertainty.&lt;/p&gt;

&lt;h1&gt;
  
  
  Designing and Understanding Algorithms
&lt;/h1&gt;

&lt;p&gt;Designing an algorithm is like putting together a puzzle—it’s all about breaking down a problem into manageable pieces and figuring out a clear, step-by-step plan to get from the problem to a solution. Here’s how you can do it:&lt;/p&gt;




&lt;h3&gt;
  
  
  1. &lt;strong&gt;Deeply Understand the Problem&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Before you write even a single line of code, you need to fully understand what problem you’re trying to solve.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read the problem carefully.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Identify exactly what you’re looking for.
&lt;/li&gt;
&lt;li&gt;Ask yourself: What is the problem? What do I want as an outcome?
&lt;/li&gt;
&lt;li&gt;For example, if you’re trying to find the largest number in a list, you need to know what your input is (a list of integers) and what the expected output should be (the maximum integer in that list).
Think of this step as reading the blueprint before you start building a house.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. &lt;strong&gt;Define the Inputs and Outputs&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Once you understand the problem, clearly define:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Inputs:&lt;/strong&gt; What data will you receive? (e.g., an array of integers)
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Outputs:&lt;/strong&gt; What should your algorithm produce? (e.g., the largest number in the array)
This step helps avoid any confusion later on—like knowing exactly which ingredients you need before you start cooking.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3. &lt;strong&gt;Identify Constraints and Edge Cases&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Every problem comes with limitations and special conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Constraints:&lt;/strong&gt; These might include time limits, memory usage, or specific conditions that the input must meet.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Cases:&lt;/strong&gt; Consider unusual situations like an empty list, a list with only one element, all elements being the same, or even negative numbers.
Handling these cases is like checking all the “what if” scenarios before launching a new product—ensuring your solution works under all circumstances.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  4. &lt;strong&gt;Choose an Appropriate Algorithmic Technique&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;There are many techniques to choose from, and the right one depends on the problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Greedy Algorithms:&lt;/strong&gt; Best for optimization problems where making the locally optimal choice at each step leads to a global solution (e.g., coin change problem).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Divide and Conquer:&lt;/strong&gt; Useful when you can break the problem into smaller subproblems, solve each one, and then combine the results (e.g., Merge Sort).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Programming:&lt;/strong&gt; Ideal for problems with overlapping subproblems where you can store intermediate results (e.g., Knapsack Problem, Fibonacci sequence).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backtracking:&lt;/strong&gt; Great for problems where you need to explore all possible solutions, like generating permutations or solving puzzles.
Choosing the right technique is like picking the right tool for a job—each tool is best suited for certain types of problems.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  5. &lt;strong&gt;Develop the Algorithm Step-by-Step&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now that you know what to do, outline the steps of your algorithm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Break the problem down:&lt;/strong&gt; Divide it into smaller, manageable parts that are easier to solve.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write down each step:&lt;/strong&gt; Clearly describe what happens first, what comes next, and how each step uses the result of the previous one.
For example, to find the maximum number in a list:

&lt;ol&gt;
&lt;li&gt;If the list is empty, return a specific value (like &lt;code&gt;None&lt;/code&gt; or an error).&lt;/li&gt;
&lt;li&gt;Assume the first element is the largest.&lt;/li&gt;
&lt;li&gt;For every other element, if the current element is larger than your assumed maximum, update the maximum.&lt;/li&gt;
&lt;li&gt;Return the maximum after processing all elements.
This is like creating a detailed recipe that even someone with no cooking experience could follow to make a perfect dish.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  6. &lt;strong&gt;Write Pseudocode or a Flowchart&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Translate your step-by-step plan into pseudocode or a flowchart:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pseudocode:&lt;/strong&gt; A language-agnostic way of outlining your algorithm that anyone with basic programming knowledge can understand.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flowcharts:&lt;/strong&gt; Visual diagrams that show the process flow and decision points.
This step helps you ensure that your logic is sound before you get bogged down in coding syntax.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  7. &lt;strong&gt;Analyze the Algorithm&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Evaluate your algorithm for efficiency:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity:&lt;/strong&gt; How does the number of operations scale with input size? (e.g., O(n), O(n log n), etc.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity:&lt;/strong&gt; How much memory does your algorithm require?
For example, a simple algorithm to find the maximum number in a list runs in O(n) time and uses O(1) extra space.
This analysis is like stress-testing your solution to make sure it can handle the expected workload.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  8. &lt;strong&gt;Test the Algorithm&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Run your algorithm through a variety of tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Normal Cases:&lt;/strong&gt; Use typical inputs, like a list [3, 1, 7, 4] where the output should be 7.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Edge Cases:&lt;/strong&gt; Test with empty lists, single-element lists, lists with all identical elements, or lists containing negative numbers.
Testing is like taking your car for a test drive on different roads before you decide it’s ready for everyday use.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  9. &lt;strong&gt;Optimize if Necessary&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Based on your tests and analysis:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Refine your algorithm&lt;/strong&gt; to improve efficiency if it’s too slow or uses too much memory for large inputs.&lt;/li&gt;
&lt;li&gt;For simple problems (like finding the maximum number), your initial approach might be optimal, but more complex problems might require rethinking your approach.
This is like tuning up a machine to ensure it runs at its best possible performance.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  10. &lt;strong&gt;Document Your Algorithm&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Finally, document your algorithm:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explain the purpose:&lt;/strong&gt; What problem does it solve?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;List the inputs and outputs:&lt;/strong&gt; Make it clear for anyone else (or future you) who reads your code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Describe each step:&lt;/strong&gt; Provide comments or a separate document that explains the logic behind each step.
Documentation ensures that your algorithm can be understood, maintained, and improved upon in the future—just as a well-written manual helps someone use a new gadget correctly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Remember, designing an algorithm is like solving a puzzle. The clearer your strategy and instructions, the smoother the process—whether you’re guiding a computer or explaining your method to someone else.&lt;/p&gt;

&lt;h1&gt;
  
  
  Brain Algorithms: A Masterpiece of Divine Engineering
&lt;/h1&gt;

&lt;p&gt;Now, let’s step away from the dry and formal world of computer science and look into the most incredible processor in existence: the human brain.&lt;/p&gt;

&lt;p&gt;Have you ever really noticed how your brain makes the best possible decision in the blink of an eye? Imagine you need to descend a flight of stairs. If you're feeling playful, you might decide to jump over the last three steps. But how does that jump actually happen?&lt;/p&gt;

&lt;p&gt;In the span of a few milliseconds, your brain is busy analyzing dozens of variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;The height you plan to jump.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The speed and angle of your leap.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The strength of your leg muscles and the amount of knee flexion.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The positioning of your arms to maintain balance.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The likelihood of injury based on the landing surface.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without you even noticing, billions of neurons perform these complex calculations almost instantaneously. They then dispatch precise signals through your nervous system, instructing your muscles to execute the safest and most efficient movement possible. This entire process is a real-time decision-making algorithm—one that has been fine-tuned over millennia.&lt;/p&gt;

&lt;p&gt;It’s a stunning example of biological engineering, where every tiny detail is orchestrated perfectly to ensure your survival and well-being. The brain’s algorithms are not written in code, but they work with a level of precision and speed that even the fastest computer programs struggle to match.&lt;/p&gt;

&lt;p&gt;So, next time you effortlessly navigate a set of stairs or make a split-second decision, remember: you’re witnessing the extraordinary work of your brain’s built-in algorithms—truly a masterpiece of divine engineering.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Ultimately, algorithms are not just about solving problems, but about finding the &lt;strong&gt;best possible solution&lt;/strong&gt; given the constraints of time, resources, and real-world needs. Choosing the right algorithm can be the difference between &lt;strong&gt;a fast and smooth program&lt;/strong&gt; and &lt;strong&gt;a processing nightmare&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And if there's one thing we should always remember, it's this: &lt;strong&gt;Everything in life is, ultimately, an algorithm.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>learning</category>
    </item>
    <item>
      <title>new web framework</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Fri, 28 Feb 2025 23:03:03 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/new-web-framework-42f5</link>
      <guid>https://forem.com/m__mdy__m/new-web-framework-42f5</guid>
      <description>&lt;p&gt;Hey everyone!  &lt;/p&gt;

&lt;p&gt;I’m still working hard on &lt;strong&gt;Gland&lt;/strong&gt;, and after a lot of &lt;strong&gt;refactoring&lt;/strong&gt; and &lt;strong&gt;R&amp;amp;D&lt;/strong&gt;, I’m excited to share some of the new developments. Gland is getting closer to its final syntax, and I’ve decided to make it &lt;strong&gt;fully event-driven&lt;/strong&gt; (EDS-based), inspired by frameworks like &lt;strong&gt;NestJS&lt;/strong&gt; and &lt;strong&gt;Angular&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It means:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DI system&lt;/strong&gt;: Similar to NestJS, for easy dependency injection.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Controller, Import, Export&lt;/strong&gt;: Just like NestJS, but with a twist—&lt;strong&gt;Channels&lt;/strong&gt; replace traditional providers.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Channels instead of Providers&lt;/strong&gt;: Channels handle logic, making your app more modular and scalable.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pure and lightweight&lt;/strong&gt;: Built from the ground up with &lt;strong&gt;minimal dependencies&lt;/strong&gt;, no unnecessary packages—everything is kept lean and efficient.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why EDS? (Event-Driven System)
&lt;/h3&gt;

&lt;p&gt;Instead of returning data directly from controllers, &lt;strong&gt;everything in Gland is driven by events&lt;/strong&gt;. This makes the app &lt;strong&gt;more modular, scalable, and flexible&lt;/strong&gt;. Events trigger actions and responses, making the whole flow smoother and more intuitive.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;read:server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;read:server:error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;ctx.emit('read:server', ctx)&lt;/code&gt; sends an event called &lt;code&gt;users:read:server&lt;/code&gt;, and a Channel listens for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Channel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserChannel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;On&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;read:server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the entire data flow is &lt;strong&gt;event-driven&lt;/strong&gt;  &lt;/p&gt;

&lt;h3&gt;
  
  
  What’s Next?
&lt;/h3&gt;

&lt;p&gt;Right now, &lt;strong&gt;Gland is still under development&lt;/strong&gt;, and the ideas are evolving. The core concepts are being implemented, and I'm exploring new ways to make event-driven architecture more intuitive for web development.&lt;/p&gt;

&lt;h3&gt;
  
  
  Contribute or Share Your Ideas!
&lt;/h3&gt;

&lt;p&gt;Gland is under active development, and the idea is still evolving. If you support the project, feel free to star it, open issues, submit pull requests, or share your ideas on &lt;a href="https://github.com/medishen/gland" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. Whether you spot a bug or have a new feature in mind, your feedback is incredibly valuable.&lt;/p&gt;

&lt;p&gt;I’d love to hear your thoughts—does this event-driven approach feel more flexible and modular compared to the traditional patterns? Or do you see potential challenges? Let’s start a conversation!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is Algorithm</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Thu, 31 Oct 2024 19:44:23 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/what-is-algorithm-o51</link>
      <guid>https://forem.com/m__mdy__m/what-is-algorithm-o51</guid>
      <description>&lt;h2&gt;
  
  
  INTRODUCTION
&lt;/h2&gt;

&lt;p&gt;The word “Algorithm” is derived from the name of the Persian scholar &lt;strong&gt;&lt;a href="https://en.wikipedia.org/wiki/Al-Khwarizmi" rel="noopener noreferrer"&gt;Abdullah Jafar Muhammad ibn Musa Al-Khwarizmi&lt;/a&gt;&lt;/strong&gt; , a mathematician and astronomer from the ninth century. His work laid the foundation for algebra and the development of algorithmic processes in mathematics. He is often referred to as the "father of algebra.". Al-Khwarizmi's contribution to the definition of an algorithm is profound:&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;Algorithm&lt;/strong&gt; is a well-defined computational procedure, composed of a finite set of steps, that takes one or more inputs and produces an output. These steps form a systematic method for solving a problem or performing a calculation, which can be executed manually or by a machine (e.g., a computer).&lt;/p&gt;

&lt;p&gt;The modern definition of an algorithm retains this concept while also emphasizing computational efficiency and precision. An algorithm is &lt;strong&gt;finite&lt;/strong&gt; and &lt;strong&gt;deterministic&lt;/strong&gt;, meaning it must have a definite end and the outcome must be predictable from the inputs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Definition of an Algorithm (by Al-Khwarizmi):
&lt;/h3&gt;

&lt;p&gt;An algorithm is a systematic procedure for solving a mathematical problem in a finite number of steps, which includes well-defined instructions to achieve a specific outcome. &lt;strong&gt;An Algorithm&lt;/strong&gt; is a precise rule or set of rules to solve a problem, especially by a computer, in a step-by-step manner. This concept is an evolution of Al-Khwarizmi's ideas, where algorithms were first applied to manual arithmetic and later formalized in various fields of computer science and mathematics.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modern Formal Definition:
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;Algorithm&lt;/strong&gt; can be defined as a structured procedure that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Takes inputs&lt;/strong&gt;: Typically, an algorithm requires one or more inputs to start the computation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performs computations&lt;/strong&gt;: It carries out a sequence of operations or instructions on the inputs, following a defined set of rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Produces output&lt;/strong&gt;: It generates a result or output based on the computations made on the input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminates&lt;/strong&gt;: The process must end after a finite number of steps.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An algorithm is used to solve problems by breaking them down into simple, executable steps that can be repeated for different inputs. The concept of an &lt;strong&gt;algorithm&lt;/strong&gt; has evolved over time, becoming an essential foundation in computer science. In this context, we define an algorithm as a set of programs that express or implement that algorithm. This definition may seem abstract at first, but it provides a way to group similar programs that perform the same tasks. An algorithm is essentially a computational procedure that, given certain inputs, produces an expected output. Each step of the algorithm is well-defined, meaning that the instructions are clear and unambiguous. While there are many ways to express an algorithm (such as in programming languages), the idea behind it remains consistent. Even if different programmers write different implementations of the same algorithm, the core logic stays the same.&lt;/p&gt;

&lt;p&gt;For example, the &lt;strong&gt;Mergesort algorithm&lt;/strong&gt; can be implemented in many different ways, but as long as the process of recursively dividing and merging the list is followed, all implementations would still belong to the category of the Mergesort algorithm.&lt;/p&gt;

&lt;h3&gt;
  
  
  Programs vs. Algorithms
&lt;/h3&gt;

&lt;p&gt;One of the key points here is distinguishing between &lt;strong&gt;programs&lt;/strong&gt; and &lt;strong&gt;algorithms&lt;/strong&gt;. A program is a specific implementation of an algorithm in a particular programming language or system. When multiple programmers write different code to solve the same problem using the same algorithm, these programs are distinct from one another. However, they all express the same &lt;strong&gt;algorithm&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can think of algorithms as abstract concepts or blueprints for solving a problem, while programs are concrete implementations of that blueprint in a specific language or environment. To understand this further, consider the following example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sorting Algorithms:&lt;/strong&gt; The concept of sorting data can be implemented through different algorithms such as &lt;strong&gt;Mergesort&lt;/strong&gt; or &lt;strong&gt;Quicksort&lt;/strong&gt;. However, each of these algorithms can also have multiple implementations or programs written by different programmers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Mergesort:&lt;/strong&gt; Imagine two programmers implement Mergesort using different programming languages (one in Python and another in Java). Both programs would be different, but they are considered part of the same &lt;strong&gt;Mergesort algorithm&lt;/strong&gt; because they use the same underlying steps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Quicksort:&lt;/strong&gt; Similarly, two different implementations of the Quicksort algorithm could exist, and even though the code may differ, both would still represent the same Quicksort algorithm.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Equivalence of Programs
&lt;/h3&gt;

&lt;p&gt;The set of all programs that implement a given algorithm can be grouped into &lt;strong&gt;equivalence classes&lt;/strong&gt;. Two programs are considered &lt;strong&gt;equivalent&lt;/strong&gt; if they are essentially implementing the same algorithm, regardless of differences in their code or language.&lt;/p&gt;

&lt;p&gt;For instance, if we take all the different programs that implement Mergesort, they belong to the same equivalence class because they perform the same sorting algorithm. This grouping allows us to define an algorithm as the set of all programs that belong to its equivalence class.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="http://www.cs.man.ac.uk/~david/categories/book/book.pdf" rel="noopener noreferrer"&gt;Category Theory and Algorithms&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;In mathematical terms, this way of grouping programs into equivalence classes introduces a structure that can be analyzed using &lt;strong&gt;category theory&lt;/strong&gt;. In this theory, the set of all programs is not considered a category, but the set of algorithms forms a category with additional structure. This structure helps in understanding the relationships between different algorithms and their implementations.&lt;/p&gt;

&lt;p&gt;The conditions that determine whether two programs are equivalent can be described as &lt;strong&gt;coherence relations&lt;/strong&gt;. These relations define the rules by which different programs are grouped into the same equivalence class, enriching the category of algorithms with extra properties.&lt;/p&gt;

&lt;h3&gt;
  
  
  Universal Properties of the Category of Algorithms
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Universal properties&lt;/strong&gt; are a key concept in category theory, and they apply to algorithms as well. These properties help us understand how algorithms relate to one another and how they can be combined or transformed. Universal properties provide a formal framework for reasoning about algorithms, especially when we need to compare them or understand their behavior in a more abstract sense.&lt;/p&gt;

&lt;h3&gt;
  
  
  Questions about the Definition of an Algorithm
&lt;/h3&gt;

&lt;p&gt;In their book &lt;em&gt;Introduction to Algorithms&lt;/em&gt;, authors Cormen, Leiserson, Rivest, and Stein define an algorithm informally as a well-defined computational procedure that takes input and produces output. However, some questions arise from this definition:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;"Informally"?&lt;/strong&gt; – Given the technical nature of the book, some might expect a more formal definition of an algorithm.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What is "well-defined"?&lt;/strong&gt; – The term "well-defined" means that the steps of the algorithm are clear and can be understood unambiguously by both humans and machines.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What is a "procedure"?&lt;/strong&gt; – The word "procedure" refers to the specific series of steps followed to solve a problem, which is essentially what an algorithm is.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Donald Knuth, a renowned computer scientist, offers a more precise perspective, stating that while algorithms are difficult to define formally, they are nevertheless &lt;strong&gt;real mathematical objects&lt;/strong&gt;. We refer to algorithms with statements like "Mergesort runs in O(n log n) time" or "There does not exist an algorithm to solve the halting problem." Algorithms are just as real as numbers or mathematical sets.&lt;/p&gt;

&lt;h3&gt;
  
  
  Algorithms as Abstract Objects
&lt;/h3&gt;

&lt;p&gt;Just like the number 42 is not tied to any particular representation (whether written as &lt;code&gt;42&lt;/code&gt;, &lt;code&gt;XLII&lt;/code&gt;, or &lt;code&gt;101010&lt;/code&gt; in binary), an algorithm exists as an abstract concept. Multiple programs can implement the same algorithm, just like multiple sets of objects can represent the number 42. In this sense, an algorithm is defined similarly to how &lt;strong&gt;Gottlob Frege&lt;/strong&gt; defined a natural number as the equivalence class of all sets of the same size.&lt;/p&gt;

&lt;p&gt;To summarize:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Algorithms&lt;/strong&gt; are abstract entities that represent a specific way to solve a problem.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Programs&lt;/strong&gt; are concrete implementations of these algorithms in a particular language or system.&lt;/li&gt;
&lt;li&gt;Different programs that implement the same algorithm are grouped into &lt;strong&gt;equivalence classes&lt;/strong&gt;, allowing us to define an algorithm as the set of all equivalent programs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Defining an Algorithm as a Set of Programs
&lt;/h3&gt;

&lt;p&gt;To explain this more clearly: an &lt;strong&gt;algorithm&lt;/strong&gt; is not simply a single program or piece of code. Instead, it’s the &lt;strong&gt;entire set of programs&lt;/strong&gt; that can be written to achieve the same task. For example, imagine a professor teaching a sorting algorithm like MergeSort. Students in the class might all go home and write different programs that implement MergeSort in various ways. Despite differences in code structure or approach, all of these programs are performing the same underlying sorting task. In this sense, all of these programs belong to the same "equivalence class," which defines the algorithm as a whole.&lt;/p&gt;

&lt;p&gt;This leads to the idea that algorithms are essentially collections of programs. Two programs belong to the same equivalence class (or algorithm) if they perform the same function. The algorithm itself is defined by this collection of equivalent programs.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. What Does It Mean for Two Programs to be "Essentially" the Same?
&lt;/h3&gt;

&lt;p&gt;When two programs are considered "essentially" the same, it means they are performing the same task but may have small differences in their implementation.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One program performs two unrelated processes, let's call them &lt;strong&gt;Process1&lt;/strong&gt; and &lt;strong&gt;Process2&lt;/strong&gt;, in a certain order. Another program might perform these processes in the reverse order. Even though the order of operations differs, the overall result is the same.&lt;/li&gt;
&lt;li&gt;One program uses a loop to repeat a certain task &lt;strong&gt;n&lt;/strong&gt; times. Another program "unwinds" the loop, meaning it explicitly writes out the steps rather than using a loop structure. Again, these two programs achieve the same result, but in slightly different ways.&lt;/li&gt;
&lt;li&gt;One program might perform two separate tasks within a single loop, while another program might split these tasks into two separate loops. Despite this structural difference, both programs are effectively doing the same thing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In these examples, even though the programs may look different at the level of code, they are performing the same underlying task or function. Therefore, they belong to the same algorithm.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Algorithms as Equivalence Classes
&lt;/h3&gt;

&lt;p&gt;The key idea is that an &lt;strong&gt;algorithm&lt;/strong&gt; is the collection of all programs that can achieve the same result. These programs are grouped into what are called &lt;strong&gt;equivalence classes&lt;/strong&gt;. The notion of an equivalence class is a common concept in mathematics: two things are considered equivalent if they share some essential property. In this case, two programs are considered equivalent if they implement the same algorithm.&lt;/p&gt;

&lt;p&gt;For example, all the programs that implement MergeSort are considered part of the same equivalence class. Similarly, all the programs that implement QuickSort belong to a different equivalence class. In the language of the text, the "set of programs" is partitioned into these equivalence classes, where each class represents a distinct algorithm.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Subjectivity of Equivalence
&lt;/h3&gt;

&lt;p&gt;One important point is that the decision of whether two programs are "essentially the same" is somewhat subjective. Different people or contexts might have different criteria for considering two programs equivalent. That the relations used to decide this equivalence are not set in stone; others might come up with different or additional ways of defining equivalence between programs. Despite this subjectivity, there are some standard relations that most people would agree on. These relations correspond to what are called &lt;strong&gt;categorical coherence rules&lt;/strong&gt; in mathematics, and when we "mod-out" (apply the equivalence relation), the set of programs becomes a more structured object, called a &lt;strong&gt;category&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Moving from Programs to Algorithms to Computable Functions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Programs&lt;/strong&gt;: These are the concrete implementations that software engineers write. They are individual pieces of code that perform tasks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Algorithms&lt;/strong&gt;: These are equivalence classes of programs. An algorithm is a collection of programs that perform the same task, even if they differ in implementation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Computable Functions&lt;/strong&gt;: At the highest level, we have functions. A computable function is something that can be calculated by a program or algorithm. For example, the function "sort" takes a list of numbers and returns the list in sorted order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Different algorithms can compute the same function. For example, MergeSort and QuickSort are two different algorithms, but both compute the same &lt;strong&gt;function&lt;/strong&gt;: sorting a list of numbers.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Mapping Between Levels
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;There is a mapping (or function) from &lt;strong&gt;programs to algorithms&lt;/strong&gt;. This mapping takes each program and assigns it to the equivalence class (algorithm) that it belongs to.&lt;/li&gt;
&lt;li&gt;There is also a mapping from &lt;strong&gt;algorithms to computable functions&lt;/strong&gt;. This takes each algorithm and assigns it to the computable function that it implements.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Visual Representation:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Programs  →  Algorithms  →  Computable Functions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each level represents a higher level of abstraction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Programs&lt;/strong&gt; are specific code implementations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Algorithms&lt;/strong&gt; are groups of programs that do the same task.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Computable Functions&lt;/strong&gt; are abstract mathematical functions that can be computed by various algorithms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The idea here is that programs can be grouped together if they are “essentially the same,” forming an equivalence class. Two programs are considered to be in the same equivalence class if they perform the same essential task, even if the details of how they achieve it differ. For example, one program might execute certain processes in a different order, or it might loop through a task in a slightly different manner. Despite these differences, both programs can be seen as implementations of the same algorithm. An algorithm, in this sense, is the sum of all the programs that implement it.&lt;/p&gt;

&lt;p&gt;This leads to a concept where all programs are divided into subsets (equivalence classes), and each subset represents one algorithm. This gives us a set of equivalence classes called "Algorithms." Formally, we can define a function φ that maps a program to its corresponding algorithm, meaning φ takes a program and assigns it to its equivalence class (its algorithm). This algorithm is the essential idea behind all the programs in that class. Additionally, there could be another function ψ that, when applied to an algorithm, produces a specific program implementing it.&lt;/p&gt;

&lt;p&gt;It's important to recognize that there are different ways to compare programs for "sameness." Some methods are stricter, while others are more lenient. In the strictest interpretation, every program would be considered its own unique algorithm, meaning every program is distinct. In the loosest interpretation, two programs would be considered the same if they produce the same result or perform the same function, leading to a view where every program that computes the same result is just an expression of the same computable function.&lt;/p&gt;

&lt;p&gt;In the middle of these two extremes lies the approach taken in this discussion, where programs that are "essentially" the same are grouped together into equivalence classes, but these groups still distinguish between algorithms that compute the same result in fundamentally different ways (like different methods for sorting data). Other equivalence relations can exist that further fine-tune how programs are grouped into algorithms, and different relations will lead to different structures.&lt;/p&gt;

&lt;p&gt;The notion of algorithms forming a set is not just abstract. It forms a mathematical structure called a category, which has some specific properties. For instance, the category of algorithms in this case has a Cartesian product structure, which means that the category supports the idea of combining algorithms, and it has a special way of handling natural numbers, meaning it can express recursive algorithms.&lt;/p&gt;

&lt;p&gt;Although related categories have been studied in various forms, the connection between algorithms and this specific categorical structure is relatively new. Just as rational numbers are defined as equivalence classes of integer pairs, algorithms can be thought of as equivalence classes of programs. When we write an algorithm down, we’re really just writing one of its programs, which is why algorithms are often presented in pseudo-code: it’s a way to avoid being tied to any specific programming language. Pseudo-code captures the essential steps of an algorithm without specifying exactly how the algorithm should be implemented.&lt;/p&gt;

&lt;p&gt;A nice analogy here is to think about how rational numbers, such as 3/5, are equivalence classes. Just as there are many ways to express the same rational number (like 3/5, 6/10, or 3000/5000), there are many ways to express an algorithm through different programs. While we often prefer a "canonical" representation of a rational number, like 3/5, we might wish for a similarly preferred or canonical representation of an algorithm. This concept, though, is speculative and explored further in later parts of the paper.&lt;/p&gt;

&lt;p&gt;The next question is: which programming language should be used to express algorithms? Instead of picking a specific programming language, using the language of descriptions for &lt;strong&gt;primitive recursive functions&lt;/strong&gt;. This language is simple, elegant, and familiar to many readers. It focuses on three operations: &lt;strong&gt;Composition&lt;/strong&gt; (sequential processing), &lt;strong&gt;Bracket&lt;/strong&gt; (parallel processing), and &lt;strong&gt;Recursion&lt;/strong&gt; (looping). Primitive recursive functions are an important subset of computable functions, and their descriptions can be seen as programs that calculate functions. Although this framework is limited to primitive recursive functions for now, it still provides interesting insights and results. There is also ongoing work to expand this to cover all recursive functions, which would give a more comprehensive framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Properties of an Algorithm:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Input&lt;/strong&gt;: An algorithm must accept a finite number of inputs. These inputs represent the data to be processed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output&lt;/strong&gt;: It must produce at least one output or result, which is the solution or a decision made by following the steps of the algorithm.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Definiteness&lt;/strong&gt;: Each instruction in the algorithm must be clear, unambiguous, and precisely defined. This ensures that every step is understood without confusion.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Finiteness&lt;/strong&gt;: The algorithm must terminate after a finite number of steps. It cannot continue indefinitely.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Effectiveness&lt;/strong&gt;: Each step of the algorithm must be basic enough to be carried out manually or executed by a machine.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Criteria for Evaluating an Algorithm:
&lt;/h3&gt;

&lt;p&gt;When analyzing or designing an algorithm, the following factors are considered:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Data Structures&lt;/strong&gt;: What data structures (lists, queues, stacks, heaps, trees, etc.) should be used to implement the algorithm?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Correctness&lt;/strong&gt;: Is the algorithm correct? Does it always produce the correct output, or only in some cases?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency&lt;/strong&gt;: How efficient is the algorithm? Efficiency is measured in terms of time complexity and space complexity, and it usually depends on the size of the input.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complexity&lt;/strong&gt;: Does an efficient algorithm exist for the problem? This leads to the famous &lt;strong&gt;P vs NP&lt;/strong&gt; problem, which is a fundamental question in theoretical computer science regarding the existence of polynomial-time algorithms for NP problems.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In conclusion, algorithms are foundational concepts in both mathematics and computer science, originating from the work of Al-Khwarizmi, a scholar whose contributions laid the groundwork for modern computational thinking. An algorithm is a systematic, well-defined sequence of steps used to solve problems or perform computations, with key characteristics like finiteness, determinism, and clarity of execution. As abstract entities, algorithms transcend the specific programs that implement them, representing the core logic of problem-solving across various languages and platforms.&lt;/p&gt;

&lt;p&gt;By viewing algorithms as equivalence classes of programs, we can appreciate how different implementations of the same algorithm belong to the same conceptual framework, even if they vary in code structure or language. This abstraction helps us understand how diverse programs can achieve the same task and how algorithms relate to broader mathematical concepts, such as computable functions.&lt;/p&gt;

&lt;p&gt;Furthermore, the application of category theory provides a deeper understanding of the relationships between algorithms, showing how they can be grouped and compared based on coherence and equivalence. In essence, algorithms are the bridge between abstract mathematical functions and concrete computational procedures, enabling efficient and precise problem-solving in both theoretical and practical contexts.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>algorithms</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What are Syntax and Semantics</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Tue, 01 Oct 2024 22:54:33 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/what-are-syntax-and-semantics-1p3e</link>
      <guid>https://forem.com/m__mdy__m/what-are-syntax-and-semantics-1p3e</guid>
      <description>&lt;p&gt;If you're learning a language, you've probably heard the word "syntax" and deal with it all the time. (Fucking Syntax Error).&lt;/p&gt;

&lt;p&gt;It was a few nights ago that I was thinking to myself, I never followed programming paradigms and techniques seriously, and today I started to learn from the smallest topics that I always hear (even if I already remember). I created this &lt;a href="https://github.com/m-mdy-m/SPathways" rel="noopener noreferrer"&gt;repository&lt;/a&gt;. The learning path I took is characteristic (in the LEARNING_LIST.md file). let's pass, In contrast to the natural languages, with which we communicate our thoughts and feelings, programming languages can be viewed as artificial languages defined by men and women initially for the purpose of communicating with computers but, as importantly, for communicating algorithms among people.&lt;br&gt;
For example, language definitions consist of three components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;: the syntax of a programming language is the set of rules that define the combinations of symbols that are considered to be correctly structured programs in that language. The syntax of a language defines its surface form. Text-based programming languages are based on sequences of characters . The lexical grammar of a textual language specifies how characters must be chunked into tokens   Syntax refers to the ways symbols may be combined to create well-formed sentences (or programs) in the language. Syntax defines the formal relations between the constituents of a language, thereby providing a structural description of the various expressions that make up legal strings in the language. Syntax deals solely with the form and structure of symbols in a language without any consideration given to their meaning, syntax is the concept that concerns itself only whether or not the sentence is valid for the grammar of the language&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Semantics&lt;/strong&gt;: Semantics is about whether or not the sentence has a valid meaning. Semantics reveals the meaning of syntactically valid strings in a language. For natural languages, this means correlating sentences and phrases with the objects, thoughts, and feelings of our experiences. For programming languages, semantics describes the behavior that a computer follows when executing a program in the language. We might disclose this behavior by describing the relationship between the input and output of a program or by a step-by-step explanation of how a program will execute on a real or an abstract machine. Semantics is the general term for the study of meaning. In computer science, the subject of programming language semantics seeks to give precise mathematical meaning to programs. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Low-Level Semantics:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In natural languages, a sentence can be grammatically correct but semantically meaningless. For example, the sentence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The man bought the infinity from the store."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is grammatically correct but makes no sense in the real world. Similarly, in programming languages, a statement can be syntactically correct but semantically incorrect because it violates the language’s rules or its intended meaning.&lt;/p&gt;

&lt;p&gt;At the &lt;strong&gt;low-level semantics&lt;/strong&gt; of programming, we're concerned with whether a statement that has correct syntax also makes sense according to the language's type system or other rules. Even though the syntax might be valid, the semantic meaning of the operation could be invalid. The type system in statically-typed languages, like Java, helps enforce these rules before runtime, but dynamically-typed languages like JavaScript don't always enforce these rules until runtime.&lt;/p&gt;
&lt;h4&gt;
  
  
  Example in JavaScript:
&lt;/h4&gt;

&lt;p&gt;In JavaScript, which is loosely typed, you might not have the protection that comes with a stricter type system, and the language will allow certain operations that don't make sense in a semantically meaningful way. Consider the following JavaScript code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// No error, but semantically this doesn't make sense.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the syntax is perfectly valid, and JavaScript allows the assignment, but semantically it's odd. You're trying to assign a number (&lt;code&gt;42&lt;/code&gt;) to a variable that is presumably intended to hold a string (&lt;code&gt;name&lt;/code&gt;). There’s no type-checking in JavaScript to stop you from making this mistake, but it’s a &lt;strong&gt;low-level semantic error&lt;/strong&gt; because it's inconsistent with the developer's intent. &lt;br&gt;
If we implement it. It is executed. without any error!&lt;/p&gt;

&lt;p&gt;In stricter languages, like TypeScript or Java, this would trigger a type error immediately during compilation.&lt;/p&gt;
&lt;h4&gt;
  
  
  Example in TypeScript:
&lt;/h4&gt;

&lt;p&gt;TypeScript, a superset of JavaScript, introduces type-checking to prevent these low-level semantic issues:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// Error: Type 'number' is not assignable to type 'string'.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In TypeScript, the compiler detects the semantic error because &lt;code&gt;42&lt;/code&gt; is a number and cannot be assigned to a variable declared as a string. This enforcement of types protects the developer from unintended mistakes.&lt;/p&gt;

&lt;p&gt;In JavaScript, to avoid these low-level semantic issues, developers often use runtime checks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Expected a string!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though JavaScript doesn't enforce types, adding manual checks can help avoid errors where incorrect types lead to problems in the program.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;High-Level Semantics:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At a higher level, semantics is concerned with what your program is supposed to achieve. It's not just about whether the program has valid syntax or whether the types align correctly—it’s about whether the program behaves in the way the developer intended, or solves the problem it was designed to address.&lt;/p&gt;

&lt;p&gt;For example, let's say you're building a simple stock trading system. The high-level semantics of your code are about making sure that the system trades stocks in the right way, with the correct business logic. Even if the code doesn't produce type errors or syntax errors, it could still fail to meet the intended functionality.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example in JavaScript:
&lt;/h4&gt;

&lt;p&gt;Let’s look at a JavaScript example of high-level semantics using a simplified stock trading scenario:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;openTrade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;EURUSD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;buy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;closeTrade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;profit&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Closing trade for &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; with profit of &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Trade for &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; is not ready to close.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Check if a trade is open for EURUSD and close it if the profit target is reached.&lt;/span&gt;
&lt;span class="nf"&gt;closeTrade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;openTrade&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax is correct, and the program runs without errors. However, imagine we now introduce a mistake at a higher level, such as accidentally entering two trades for the same symbol, which violates the system's business rules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;openTrades&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;EURUSD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;buy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;EURUSD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sell&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Check all trades and close any that hit their profit target.&lt;/span&gt;
&lt;span class="nx"&gt;openTrades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trade&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;closeTrade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, both trades are processed independently, but the system ends up with two trades on the same symbol, one in the buy direction and one in the sell direction. This breaks the high-level business rule that we should only have one open trade per symbol at any given time.&lt;/p&gt;

&lt;p&gt;Although the code executes without syntax or type errors, it is &lt;strong&gt;semantically incorrect&lt;/strong&gt; at a high level. The system logic should have ensured only one trade per symbol could be active at a time. This error would likely lead to unintended consequences in a real-world trading system, such as financial loss.&lt;/p&gt;

&lt;p&gt;To fix this high-level semantic issue, you'd need to adjust the logic to ensure that only one trade is open for a particular symbol:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;openTrades&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;EURUSD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;buy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;openNewTrade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTrade&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Ensure no other trades are open for the same symbol.&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;existingTrade&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;openTrades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;trade&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;trade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;symbol&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;newTrade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;existingTrade&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;openTrades&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newTrade&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Opened new trade for &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newTrade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Cannot open a new trade for &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;newTrade&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, trade already exists.`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;openNewTrade&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;EURUSD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sell&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the logic ensures that a new trade can't be opened if there’s already an active trade for the same symbol. This is a &lt;strong&gt;high-level semantic fix&lt;/strong&gt; because it addresses the core business logic that the program is meant to follow, rather than a syntax or type issue.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pragmatics&lt;/strong&gt;: Pragmatics alludes to those aspects of language that involve the users of the language, namely psychological and sociological phenomena such as utility, scope of application, and effects on the users. For programming languages, pragmatics includes issues such as ease of implementation, efficiency in application, and programming methodology.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This was just basic information about each. For more information, you can read these items:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://homepage.divms.uiowa.edu/~slonnegr/plf/Book/" rel="noopener noreferrer"&gt;Source-1&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://cdn.preterhuman.net/texts/science_and_technology/artificial_intelligence/Formal%20Syntax%20and%20Semantics%20of%20Programming%20Languages%20-%20Kenneth%20Slonneger.pdf" rel="noopener noreferrer"&gt;Source-2&lt;/a&gt; - | &lt;strong&gt;is a great resource&lt;/strong&gt; |&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.cs.montana.edu/revelle/csci305/slides/syntax_semantics.pdf" rel="noopener noreferrer"&gt;Source-3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.usna.edu/Users/cs/wcbrown/courses/F19SI413/lec/l07/lec.html" rel="noopener noreferrer"&gt;Source-4&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/17930267/what-is-the-difference-between-syntax-and-semantics-in-programming-languages" rel="noopener noreferrer"&gt;Source-5&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Syntax_%28programming_languages%29" rel="noopener noreferrer"&gt;wikipedia-Syntax&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Semantics_%28computer_science%29" rel="noopener noreferrer"&gt;wikipedia-Semantics&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>discuss</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The OSI Model: A Comprehensive Overview</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Thu, 25 Jul 2024 20:42:45 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/the-osi-model-a-comprehensive-overview-52j5</link>
      <guid>https://forem.com/m__mdy__m/the-osi-model-a-comprehensive-overview-52j5</guid>
      <description>&lt;p&gt;The Open Systems Interconnection (OSI) model, developed by the International Organization for Standardization (ISO), is a foundational framework that facilitates understanding and designing complex communication systems. It is organized into seven distinct layers, each responsible for specific functions in the process of data transmission and reception between networked systems.&lt;/p&gt;

&lt;h4&gt;
  
  
  Overview of the OSI Layers
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Physical Layer (Layer 1)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PDU&lt;/strong&gt;: Bits&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function&lt;/strong&gt;: Responsible for the transmission and reception of raw data bits over a physical medium. It defines the hardware elements involved, such as cables, switches, and network interface cards (NICs). This layer handles the electrical, optical, or radio signals and provides specifications for the physical characteristics like voltage levels, transmission rates, and connector types.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Link Layer (Layer 2)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PDU&lt;/strong&gt;: Frame&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function&lt;/strong&gt;: Provides node-to-node data transfer and handles error detection and correction from the physical layer. It includes protocols for establishing and terminating connections between devices, as well as for controlling the flow of data. The IEEE 802 standard divides this layer into two sublayers:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MAC (Medium Access Control)&lt;/strong&gt;: Manages protocol access to the physical network medium.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LLC (Logical Link Control)&lt;/strong&gt;: Manages frame synchronization, error checking, and flow control.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Network Layer (Layer 3)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PDU&lt;/strong&gt;: Packet&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function&lt;/strong&gt;: Manages routing of data between devices across multiple networks. It handles addressing, packet forwarding, and routing through intermediate routers. This layer may fragment data into smaller packets if necessary and can reassemble them at the destination.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Transport Layer (Layer 4)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PDU&lt;/strong&gt;: Segment/Datagram&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function&lt;/strong&gt;: Ensures reliable data transfer between host systems. It manages error correction, data flow control, and retransmission of lost data. This layer can be connection-oriented (like TCP) or connectionless (like UDP), ensuring end-to-end communication and data integrity.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Session Layer (Layer 5)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PDU&lt;/strong&gt;: Data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function&lt;/strong&gt;: Manages sessions or connections between applications. It establishes, maintains, and terminates connections, providing synchronization and dialog control. This layer handles session checkpointing and recovery, ensuring that sessions can be resumed after interruptions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Presentation Layer (Layer 6)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PDU&lt;/strong&gt;: Data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function&lt;/strong&gt;: Translates data between the application layer and the network. It handles data encoding, encryption, and compression, ensuring that data sent from the application layer of one system is readable by the application layer of another. This layer transforms data into a format that the application layer can understand, like converting character encodings or compressing data for efficient transmission.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Application Layer (Layer 7)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PDU&lt;/strong&gt;: Data&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function&lt;/strong&gt;: Closest to the end user, this layer provides network services directly to applications. It includes protocols for resource sharing, remote file access, email, and web browsing (e.g., HTTP, FTP, SMTP). It handles high-level APIs, network transparency, and resource sharing.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Detailed Functions of Each Layer
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Physical Layer (Layer 1)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Converts digital data into physical signals (electrical, optical, or radio).&lt;/li&gt;
&lt;li&gt;Defines hardware specifications like cables, connectors, voltage levels, and transmission speeds.&lt;/li&gt;
&lt;li&gt;Ensures proper media termination and handles physical transmission errors like EMI (electromagnetic interference).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Data Link Layer (Layer 2)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Ensures reliable node-to-node data transfer.&lt;/li&gt;
&lt;li&gt;Detects and corrects errors from the physical layer.&lt;/li&gt;
&lt;li&gt;Establishes and terminates connections between physically connected devices.&lt;/li&gt;
&lt;li&gt;Manages data frame synchronization and flow control.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Network Layer (Layer 3)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Routes data packets across multiple networks.&lt;/li&gt;
&lt;li&gt;Manages logical addressing and routing.&lt;/li&gt;
&lt;li&gt;Handles packet fragmentation and reassembly.&lt;/li&gt;
&lt;li&gt;May provide error reporting and message forwarding.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Transport Layer (Layer 4)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Provides reliable data transfer between hosts.&lt;/li&gt;
&lt;li&gt;Manages segmentation and reassembly of data.&lt;/li&gt;
&lt;li&gt;Ensures data integrity through error correction and retransmissions.&lt;/li&gt;
&lt;li&gt;Supports both connection-oriented (TCP) and connectionless (UDP) protocols.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Session Layer (Layer 5)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Establishes, manages, and terminates sessions between applications.&lt;/li&gt;
&lt;li&gt;Provides synchronization and checkpointing for data streams.&lt;/li&gt;
&lt;li&gt;Manages session recovery and continuity.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Presentation Layer (Layer 6)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Translates data formats between application and network.&lt;/li&gt;
&lt;li&gt;Handles data encryption and decryption.&lt;/li&gt;
&lt;li&gt;Manages data compression and decompression.&lt;/li&gt;
&lt;li&gt;Ensures compatibility between different data representations (e.g., ASCII vs. EBCDIC).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Application Layer (Layer 7)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Provides network services to end-user applications.&lt;/li&gt;
&lt;li&gt;Handles protocols for file transfer, email, web browsing, and more.&lt;/li&gt;
&lt;li&gt;Manages network transparency and resource sharing.&lt;/li&gt;
&lt;li&gt;Facilitates high-level communication tasks directly used by applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;The OSI model divides network communication into seven layers, each with specific functions to standardize and streamline the process of data exchange between systems. This structured approach ensures interoperability, reliability, and efficiency in network communication, making it easier to develop and troubleshoot networked applications and services. Understanding each layer's role and function is crucial for anyone involved in networking or system design.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;But what is PDU and SDU? :)&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Service Data Unit (SDU) and Protocol Data Unit (PDU)
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Overview
&lt;/h4&gt;

&lt;p&gt;Understanding Service Data Units (SDUs) and Protocol Data Units (PDUs) is crucial for grasping how data is transmitted across various network layers in computer networking and telecommunications. These concepts are central to the OSI (Open Systems Interconnection) model and other layered networking architectures, such as the TCP/IP model.&lt;/p&gt;

&lt;h4&gt;
  
  
  Service Data Unit (SDU)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: An SDU is the piece of data that a layer receives from the layer above it. It represents the data unit that the higher layer wants to send through the network. The SDU is provided as input to a particular layer, which then processes it to produce the corresponding Protocol Data Unit (PDU) for transmission.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Role&lt;/strong&gt;: The SDU serves as the payload that the layer will encapsulate into its PDU. For example, in the transport layer, an SDU might be a stream of bytes from an application layer, which the transport layer will package into transport PDUs for delivery.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Protocol Data Unit (PDU)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: A PDU is the data unit that a layer in the network stack uses to communicate with its peer layer on another system. A PDU contains both control information (such as headers and trailers) and user data (which was the SDU from the higher layer).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structure&lt;/strong&gt;: Each layer adds its own header (and sometimes a trailer) to the SDU to form the PDU. This process is known as encapsulation. The PDU at each layer is specific to that layer's protocol.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Layer-Specific Examples
&lt;/h4&gt;

&lt;p&gt;In the OSI model, each layer has its specific type of PDU:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Application Layer&lt;/strong&gt;: The PDU is often referred to simply as data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Presentation Layer&lt;/strong&gt;: The PDU remains data, but it may include encoded or encrypted data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session Layer&lt;/strong&gt;: The PDU is still referred to as data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transport Layer&lt;/strong&gt;: The PDU is called a segment in TCP or a datagram in UDP.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Layer&lt;/strong&gt;: The PDU is called a packet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Link Layer&lt;/strong&gt;: The PDU is called a frame.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Physical Layer&lt;/strong&gt;: The PDU is a bit or a stream of bits.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Encapsulation Process
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Application Layer&lt;/strong&gt;: Generates the application data, which becomes the SDU for the presentation layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Presentation Layer&lt;/strong&gt;: Encapsulates the SDU into its PDU, which becomes the SDU for the session layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session Layer&lt;/strong&gt;: Encapsulates the SDU into its PDU, passing it to the transport layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transport Layer&lt;/strong&gt;: Encapsulates the SDU into a transport PDU (segment/datagram), sending it to the network layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Layer&lt;/strong&gt;: Encapsulates the SDU into a network PDU (packet) and passes it to the data link layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Link Layer&lt;/strong&gt;: Encapsulates the SDU into a data link PDU (frame) and sends it to the physical layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Physical Layer&lt;/strong&gt;: Converts the frame into bits for transmission over the physical medium.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Decapsulation Process
&lt;/h4&gt;

&lt;p&gt;Upon reaching the destination, each layer reverses the encapsulation process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Physical Layer&lt;/strong&gt;: Converts the bits back into a frame and passes it to the data link layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Link Layer&lt;/strong&gt;: Removes the frame headers and trailers, extracting the packet (SDU), which is then passed to the network layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Layer&lt;/strong&gt;: Removes the packet headers and trailers to extract the segment/datagram, passing it to the transport layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transport Layer&lt;/strong&gt;: Removes the segment/datagram headers and trailers to retrieve the original session data, passing it to the session layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session Layer&lt;/strong&gt;: Passes the data to the presentation layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Presentation Layer&lt;/strong&gt;: Decodes or decrypts the data as needed and passes it to the application layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application Layer&lt;/strong&gt;: Receives the original application data.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Importance of SDU and PDU
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Integrity&lt;/strong&gt;: Encapsulation ensures that each layer can process its part of the data correctly without interfering with other layers. This helps maintain the integrity of the data as it travels through the network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interoperability&lt;/strong&gt;: By defining clear boundaries and responsibilities, different network protocols and technologies can work together seamlessly. This interoperability is essential for the functioning of diverse network systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling&lt;/strong&gt;: Each layer can implement its own error detection and correction mechanisms, providing robustness in data transmission. This layered error handling ensures that errors can be detected and corrected at multiple points in the communication process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Additional Insights
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Modularity&lt;/strong&gt;: The concept of SDUs and PDUs allows for modular design in networking protocols. Each layer operates independently, providing services to the layer above and using the services of the layer below. This modularity simplifies the design and implementation of network protocols.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: The layered approach and the use of SDUs and PDUs enable networks to scale efficiently. As networks grow and new technologies are introduced, the independence of each layer ensures that changes in one layer do not necessarily impact the others.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In summary, SDUs and PDUs are fundamental concepts in networking that facilitate the structured and efficient transmission of data across layered architectures. They ensure that data is properly encapsulated, transmitted, and decapsulated, maintaining integrity and interoperability throughout the communication process. Understanding these concepts is essential for anyone involved in the design, implementation, or management of networked systems.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;How the OSI Model Works&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Each layer in the OSI model serves a specific role and communicates with the layers directly above and below it. This modular approach allows for flexibility and standardization in network communications. When data is transmitted from one system to another, it passes through all seven layers, from the application layer down to the physical layer on the sender's side, and then from the physical layer up to the application layer on the receiver's side.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Comparison with the Internet Protocol Suite&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The Internet protocol suite, also known as the TCP/IP model, was developed around the same time as the OSI model. Although it is less structured, it focuses primarily on the software layers of communication and has been the foundation for the development of the Internet. The TCP/IP model has four layers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Link Layer&lt;/strong&gt;: Corresponds to the OSI's Physical and Data Link layers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internet Layer&lt;/strong&gt;: Corresponds to the OSI's Network layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transport Layer&lt;/strong&gt;: Same as the OSI's Transport layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application Layer&lt;/strong&gt;: Encompasses the OSI's Session, Presentation, and Application layers.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Significance of the OSI Model&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The OSI model has been instrumental in standardizing networking concepts and practices. It provides a clear framework for understanding how different network protocols interact and ensures transparent communication between systems through the equivalent exchange of protocol data units (PDUs) in peer-to-peer networking. This model is widely accepted and utilized by both professionals and non-professionals in the field of information technology.&lt;/p&gt;

&lt;h3&gt;
  
  
  The History and Significance of the OSI Model
&lt;/h3&gt;

&lt;p&gt;The OSI (Open Systems Interconnection) model is a crucial conceptual framework in the world of computer networking, developed by the International Organization for Standardization (ISO) during the late 1970s and early 1980s. This model was created to address the emerging and diverse networking methods and to provide a universal set of standards for network communication.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Origins and Early Development&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In the 1970s, networking was dominated by government-sponsored projects and proprietary vendor solutions. Notable examples included:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;NPL Network (UK)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ARPANET (US)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CYCLADES (France)&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During this period, public data networks began to emerge, using standards like &lt;strong&gt;X.25&lt;/strong&gt;. However, there was a clear need for higher-level protocols to ensure diverse systems could interconnect effectively.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Key Milestones&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1973-1975&lt;/strong&gt;: The UK's Experimental Packet Switched System highlighted the need for standardized higher-level protocols.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1977&lt;/strong&gt;: The UK proposed the formation of an international standards committee at an ISO meeting in Sydney, Australia.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1978&lt;/strong&gt;: French software engineer Hubert Zimmermann first defined the OSI model in Washington, D.C.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1980&lt;/strong&gt;: The ISO published a draft standard of the OSI model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1983&lt;/strong&gt;: Documents from the CCITT and ISO were merged, leading to the unified OSI Reference Model.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Formal Adoption and Standardization&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In 1984, the OSI model was officially published by both the ISO (as ISO 7498) and the CCITT (now ITU-T) as X.200. This formal adoption represented a significant step towards standardizing network communication protocols and ensuring interoperability between different systems and vendors.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Components of the OSI Model&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The OSI model consists of two major components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Basic Reference Model&lt;/strong&gt;: The well-known seven-layer model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Specific Protocols&lt;/strong&gt;: A set of protocols designed to implement the functionalities of each layer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The seven-layer model includes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Physical Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data Link Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Network Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transport Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Session Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Presentation Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Application Layer&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Competing Priorities and Technological Challenges&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Developing the OSI model involved balancing various interests and keeping pace with rapid technological advancements. The goal was to create a framework that new systems could converge to, rather than standardizing procedures after their implementation.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;The Protocol Wars&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;During the late 1980s and early 1990s, there was significant debate over whether the OSI model or the Internet Protocol Suite (TCP/IP) would dominate the future of networking. While OSI aimed to standardize networking protocols, TCP/IP quickly gained widespread use, particularly in multi-vendor environments and internetworking.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Modern Relevance and Legacy&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Although the OSI protocols did not become as popular as those in the Internet Protocol Suite, the OSI model remains an invaluable reference for teaching and documenting networking concepts. It continues to be relevant in discussions about cloud computing and modern network architectures, though some engineers argue for a simplified approach better suited to today's protocols.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Role and Process of Communication Protocols in the OSI Model
&lt;/h3&gt;

&lt;p&gt;Communication protocols are essential for enabling interaction between entities in different hosts across a network. The OSI model abstracts this communication process through a layered approach, where each layer provides specific functionalities to the layer above it. Here’s a detailed look at how this works and the associated standards:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Communication Between Layers&lt;/strong&gt;
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Entities and Protocols&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each layer in the OSI model corresponds to specific entities in communicating devices (known as layer N peers).&lt;/li&gt;
&lt;li&gt;These entities interact using protocol data units (PDUs), which contain the actual data (called the service data unit or SDU) along with protocol-related headers or footers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Data Processing Flow&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Composition&lt;/strong&gt;: Data is composed into a PDU at the topmost layer (layer N) of the transmitting device.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transformation&lt;/strong&gt;: The PDU is passed down to layer N−1, where it becomes an SDU. Here, it is augmented with headers, footers, or both, forming a new PDU for layer N−1.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repetition&lt;/strong&gt;: This process continues, with each layer adding its own headers and footers, until the data reaches the lowest layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transmission&lt;/strong&gt;: The fully processed data is then transmitted over the network to the receiving device.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reception&lt;/strong&gt;: At the receiving end, the data ascends through the layers, with each layer stripping off its respective headers and footers, eventually delivering the SDU to the topmost layer.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This hierarchical encapsulation and decapsulation ensure that each layer handles specific tasks and communicates effectively with its peer layer on the receiving end.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Standards Documents for the OSI Model&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;The OSI model is comprehensively defined in the ISO/IEC 7498 series, which is divided into several parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ISO/IEC 7498-1: The Basic Model&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provides the fundamental framework of the seven-layer model.&lt;/li&gt;
&lt;li&gt;Also published as ITU-T Recommendation X.200.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ISO/IEC 7498-2: Security Architecture&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Defines the security features and mechanisms that can be applied across the OSI layers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ISO/IEC 7498-3: Naming and Addressing&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Describes how entities are named and addressed within the OSI model.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ISO/IEC 7498-4: Management Framework&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Outlines the management processes for the OSI model, including monitoring and control functions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Summary of the Communication Process&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Top Layer&lt;/strong&gt;: Data is prepared and encapsulated into a PDU.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Intermediate Layers&lt;/strong&gt;: Each layer adds its own specific headers/footers, transforming the SDU into a PDU for the next lower layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bottom Layer&lt;/strong&gt;: The fully encapsulated data is transmitted over the network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Receiving Device&lt;/strong&gt;: The data is passed upwards through the layers, with each layer removing its own headers/footers, until the original data is reconstructed at the topmost layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This structured approach ensures that data is transmitted accurately and efficiently, with each layer performing its designated role and facilitating seamless communication between networked devices.&lt;/p&gt;

&lt;h3&gt;
  
  
  Visual Representation of Data Processing in the OSI Model
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Transmitting Device:
   Layer N (PDU) -&amp;gt; Layer N-1 (SDU -&amp;gt; PDU) -&amp;gt; Layer N-2 (SDU -&amp;gt; PDU) -&amp;gt; ... -&amp;gt; Layer 1 (Physical Transmission)

Receiving Device:
   Layer 1 (Physical Reception) -&amp;gt; Layer 2 (PDU -&amp;gt; SDU) -&amp;gt; Layer 3 (PDU -&amp;gt; SDU) -&amp;gt; ... -&amp;gt; Layer N (Original Data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This model ensures a clear and organized process for handling data transmission and reception, highlighting the critical role of each layer in maintaining the integrity and efficiency of network communications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cross-Layer Functions in the OSI Model
&lt;/h3&gt;

&lt;p&gt;Cross-layer functions are essential services in network design that transcend individual layers of the OSI model. These functions are integral to the overall performance, security, and management of the network, impacting multiple layers simultaneously. Unlike the strictly defined roles of each layer in the OSI model, cross-layer functions provide holistic benefits that enhance the communication system's efficiency and security.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Cross-Layer Functions
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Security Services&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Security services aim to protect data's confidentiality, integrity, and availability (CIA triad). These services are defined in the ITU-T X.800 recommendation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Impact&lt;/strong&gt;: Security protocols and mechanisms, such as encryption, authentication, and integrity checks, operate across multiple layers to ensure secure communication. For example, while encryption might occur at the presentation layer, authentication might be handled at the application layer, and integrity checks can be performed at both the transport and data link layers.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Management Functions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: Management functions enable the configuration, monitoring, and termination of communications between network entities.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protocols&lt;/strong&gt;: Common Management Information Protocol (CMIP) and its corresponding service, Common Management Information Service (CMIS), are used to manage network resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Impact&lt;/strong&gt;: These protocols interact with every layer of the OSI model to manage network instances effectively. For instance, network administrators can monitor traffic at the network layer, configure hardware at the physical layer, and manage sessions at the session layer.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Multiprotocol Label Switching (MPLS)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: MPLS is a data-carrying technique for high-performance telecommunications networks that operates at an intermediate layer often referred to as "Layer 2.5."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Impact&lt;/strong&gt;: MPLS enhances the data-carrying capabilities of the network by providing efficient routing and switching for various types of traffic, including IP packets, ATM cells, and Ethernet frames. It helps in unifying circuit-based and packet-switching clients, improving the overall efficiency and speed of data transmission.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Cross MAC and PHY Scheduling in Wireless Networks&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Definition&lt;/strong&gt;: This involves coordination between the MAC (Medium Access Control) layer and the PHY (Physical) layer to optimize packet transmission based on channel conditions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Impact&lt;/strong&gt;: By leveraging real-time channel state information from the PHY layer, the MAC layer can schedule transmissions during favorable conditions, significantly enhancing network throughput and reducing energy consumption. This dynamic adjustment is crucial for maintaining high performance in wireless networks, where channel conditions can vary rapidly.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Importance of Cross-Layer Functions
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Efficiency&lt;/strong&gt;: Cross-layer optimization enables better resource utilization and improved performance by allowing layers to share information and adjust their operations dynamically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Implementing security measures across multiple layers ensures robust protection against various types of cyber threats, enhancing the overall security posture of the network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Management&lt;/strong&gt;: Comprehensive management functions facilitate seamless configuration, monitoring, and troubleshooting of network components, leading to more efficient and reliable network operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Programming Interfaces and OSI Model
&lt;/h3&gt;

&lt;p&gt;The OSI Reference Model and its associated protocol specifications do not define specific programming interfaces. Instead, they provide abstract service descriptions, allowing implementation details to be specific to different systems. This means that while the OSI model outlines how communication should occur between layers and peers, it leaves the actual software interface implementation to the developers.&lt;/p&gt;

&lt;h4&gt;
  
  
  Examples of Programming Interfaces
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NDIS (Network Driver Interface Specification)&lt;/strong&gt;: An interface between the data link layer (Layer 2) and the network layer (Layer 3).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ODI (Open Data-Link Interface)&lt;/strong&gt;: Similar to NDIS, providing a standard for network driver interfaces.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Comparison to Other Networking Suites
&lt;/h3&gt;

&lt;p&gt;The following table presents a rough comparison between the OSI layers, the original OSI protocols, and approximate modern matches. This comparison highlights how networking protocols have evolved and how different suites map onto the OSI model.&lt;/p&gt;

&lt;h4&gt;
  
  
  Layer Comparisons
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Layer&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;OSI Protocols&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;TCP/IP Protocols&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Signaling Systems &amp;amp; Modern Examples&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;7. Application&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;FTAM, X.400, X.500, DAP&lt;/td&gt;
&lt;td&gt;HTTP, HTTPS, FTP, SMTP&lt;/td&gt;
&lt;td&gt;HTTP-based protocols, Web Browser, REST API, Git, DNS over HTTP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;6. Presentation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;ISO/IEC 8823, X.226&lt;/td&gt;
&lt;td&gt;MIME, SSL/TLS, XDR&lt;/td&gt;
&lt;td&gt;XML, JSON, MIME, gzip, brotli&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;5. Session&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;ISO/IEC 8327, X.225&lt;/td&gt;
&lt;td&gt;Sockets (TCP/RTP/PPTP)&lt;/td&gt;
&lt;td&gt;HTTP headers, cookies, WebSocket, Named pipes, RPC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;4. Transport&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;ISO/IEC 8073 (TP0-TP4)&lt;/td&gt;
&lt;td&gt;TCP, UDP, SCTP, QUIC, DCCP&lt;/td&gt;
&lt;td&gt;HTTP/2, WebSocket, DTLS, Port number specifications&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;3. Network&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;ISO/IEC 8208, X.25, CLNP, IS-IS&lt;/td&gt;
&lt;td&gt;IP, IPsec, ICMP, IGMP, OSPF, RIP&lt;/td&gt;
&lt;td&gt;IPX, RRC/BMC, IBM NCP, IP addresses in URLs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;2. Data Link&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;ISO/IEC 7666, X.25 (LAPB), LLC&lt;/td&gt;
&lt;td&gt;PPP, SLIP, Ethernet framing&lt;/td&gt;
&lt;td&gt;IEEE 802.3, ATM, HDLC, Frame Relay, IEEE 802.11 MAC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;1. Physical&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;X.25, EIA/TIA-232, G.703&lt;/td&gt;
&lt;td&gt;TCP/IP is medium-agnostic&lt;/td&gt;
&lt;td&gt;RS-232, RJ45, IEEE 802.3 PHY, Bluetooth, DSL, IEEE 802.11 PHY&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Comparison with TCP/IP Model
&lt;/h3&gt;

&lt;p&gt;The TCP/IP model, which underpins the modern Internet, does not strictly adhere to the OSI model's hierarchical layering. Instead, it recognizes four broad layers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Application Layer&lt;/strong&gt;: Maps to the OSI application, presentation, and most of the session layers. Examples include HTTP, FTP, and SMTP.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transport Layer&lt;/strong&gt;: Maps to the OSI transport layer and parts of the session layer, providing end-to-end communication services. Examples include TCP and UDP.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internet Layer&lt;/strong&gt;: Corresponds to the OSI network layer, handling packet routing and addressing. Examples include IP, ICMP, and IGMP.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Link Layer&lt;/strong&gt;: Encompasses the OSI data link layer and aspects of the physical layer, dealing with hardware addressing and media access control. Examples include Ethernet and Wi-Fi.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Criticisms and Adoption
&lt;/h3&gt;

&lt;p&gt;The OSI protocol suite, despite its comprehensive design, was often considered too complex and inefficient, making it difficult to implement and less practical than the TCP/IP model. The OSI model’s approach required a complete overhaul of existing protocols, which was not feasible for many organizations with significant investments in other networking technologies.&lt;/p&gt;

&lt;p&gt;In contrast, the TCP/IP model's simplicity, flexibility, and pragmatic approach have made it the standard for modern networking. It allows for the independent implementation of protocols, making it more adaptable and widely adopted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The OSI model provides a detailed framework for understanding networking concepts and protocol functions. However, in practice, the TCP/IP suite has become the standard due to its simplicity and effectiveness. The comparison between the OSI and TCP/IP models helps in understanding the evolution of networking protocols and the practical considerations in their implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Embark on an Enchanting Journey through Algorithms, Data Structures, and Network Programming
&lt;/h2&gt;

&lt;p&gt;Welcome to a realm where algorithms dance, data structures weave intricate tales of efficiency, and network programming unlocks the secrets of connectivity. Step into my GitHub repositories, &lt;strong&gt;&lt;a href="https://github.com/m-mdy-m/algorithms-data-structures" rel="noopener noreferrer"&gt;Algorithms &amp;amp; Data Structures&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="https://github.com/medishen/CodeNet" rel="noopener noreferrer"&gt;CodeNet&lt;/a&gt;&lt;/strong&gt;, where a treasure trove of knowledge awaits your discovery.&lt;/p&gt;

&lt;h3&gt;
  
  
  Experience, Experiment, and Excel:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Uncover:&lt;/strong&gt; Dive deep into a diverse collection of algorithms and data structures. Each exploration offers a chance to sharpen your skills, reinforce your understanding, and master the art of problem-solving.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connect:&lt;/strong&gt; Explore the fundamentals and advanced concepts of network programming. Learn how data travels across networks, how protocols function, and how to build efficient, robust networked applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous Evolution:&lt;/strong&gt; These repositories are living testaments to growth. Some sections are works in progress as I journey through the realms of learning (expected completion: 2-3 years), with fresh content constantly enriching their tapestry.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Join Our Vibrant Learning Community:
&lt;/h3&gt;

&lt;p&gt;In our quest for knowledge, collaboration and interaction are the bedrock of progress. Whether you seek guidance, have ideas for enhancement, or crave discourse on algorithms, data structures, network programming, and performance optimization, your involvement is invaluable!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Engage with Us:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Twitter:&lt;/strong&gt; Follow for insights and updates &lt;a href="https://twitter.com/m__mdy__m" rel="noopener noreferrer"&gt;@m_&lt;em&gt;mdy&lt;/em&gt;_m&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Telegram:&lt;/strong&gt; Join our real-time discussions: &lt;a href="https://t.me/medishn" rel="noopener noreferrer"&gt;Channel Link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Discord:&lt;/strong&gt; Connect with us on Discord: &lt;a href="https://discord.com/invite/DK2Y5DWd" rel="noopener noreferrer"&gt;Invite Link&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub:&lt;/strong&gt; Explore and contribute on &lt;a href="https://github.com/m-mdy-m" rel="noopener noreferrer"&gt;m-mdy-m&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;Together, let's cultivate a vibrant community where knowledge flows freely, ideas flourish, and we collectively elevate our mastery of algorithms, data structures, and network programming.&lt;/p&gt;

</description>
      <category>networking</category>
      <category>network</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>What is Associative Arrays in Computer Science</title>
      <dc:creator>Genix</dc:creator>
      <pubDate>Sun, 30 Jun 2024 09:40:57 +0000</pubDate>
      <link>https://forem.com/m__mdy__m/what-is-associative-arrays-in-computer-science-1dhe</link>
      <guid>https://forem.com/m__mdy__m/what-is-associative-arrays-in-computer-science-1dhe</guid>
      <description>&lt;p&gt;In computer science, an associative array, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In mathematical terms, an associative array is a function with a finite domain. It supports 'lookup', 'remove', and 'insert' operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Dictionary Problem
&lt;/h2&gt;

&lt;p&gt;The dictionary problem is the classic problem of designing efficient data structures that implement associative arrays. The two major solutions to the dictionary problem are hash tables and search trees. It is sometimes also possible to solve the problem using directly addressed arrays, binary search trees, or other more specialized structures.&lt;/p&gt;

&lt;p&gt;Many programming languages include associative arrays as primitive data types, while many other languages provide software libraries that support associative arrays. Content-addressable memory is a form of direct hardware-level support for associative arrays.&lt;/p&gt;

&lt;p&gt;Associative arrays have many applications, including such fundamental programming patterns as memoization and the decorator pattern.&lt;/p&gt;

&lt;p&gt;The name does not come from the associative property known in mathematics. Rather, it arises from the association of values with keys. It is not to be confused with associative processors.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Summary : The dictionary problem refers to the challenge of designing and implementing efficient data structures that support the functionality of associative arrays, which allow for the storage, retrieval, and management of key-value pairs. The goal is to develop data structures that can efficiently handle a set of operations such as insertion, deletion, and lookup.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Operations
&lt;/h2&gt;

&lt;p&gt;In an associative array, the association between a key and a value is often known as a "mapping"; the same word may also be used to refer to the process of creating a new association.&lt;/p&gt;

&lt;p&gt;The operations that are usually defined for an associative array are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Insert or put&lt;/strong&gt;: add a new (key, value) pair to the collection, mapping the key to its new value. Any existing mapping is overwritten. The arguments to this operation are the key and the value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remove or delete&lt;/strong&gt;: remove a (key, value) pair from the collection, unmapping a given key from its value. The argument to this operation is the key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lookup, find, or get&lt;/strong&gt;: find the value (if any) that is bound to a given key. The argument to this operation is the key, and the value is returned from the operation. If no value is found, some lookup functions raise an exception, while others return a default value (such as zero, null, or a specific value passed to the constructor).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Associative arrays may also include other operations such as determining the number of mappings or constructing an iterator to loop over all the mappings. For such operations, the order in which the mappings are returned is usually implementation-defined.&lt;/p&gt;

&lt;p&gt;A multimap generalizes an associative array by allowing multiple values to be associated with a single key. A bidirectional map is a related abstract data type in which the mappings operate in both directions: each value must be associated with a unique key, and a second lookup operation takes a value as an argument and looks up the key associated with that value.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Operations
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Insert or Put&lt;/strong&gt;: Adds a new (key, value) pair to the collection, or updates the value if the key already exists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remove or Delete&lt;/strong&gt;: Removes the (key, value) pair associated with a given key from the collection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lookup, Find, or Get&lt;/strong&gt;: Retrieves the value associated with a given key. If the key is not found, an exception might be raised or a default value returned.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Additional Operations
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;ContainsKey&lt;/strong&gt;: Checks if a specific key exists in the associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ContainsValue&lt;/strong&gt;: Checks if a specific value exists in the associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Size or Count&lt;/strong&gt;: Returns the number of (key, value) pairs in the associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IsEmpty&lt;/strong&gt;: Checks if the associative array is empty.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clear&lt;/strong&gt;: Removes all (key, value) pairs from the associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keys&lt;/strong&gt;: Returns an iterable collection of all keys in the associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Values&lt;/strong&gt;: Returns an iterable collection of all values in the associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Entries&lt;/strong&gt;: Returns an iterable collection of all (key, value) pairs in the associative array.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Properties and Characteristics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Uniqueness&lt;/strong&gt;: Keys are unique, meaning no two pairs can have the same key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mutable&lt;/strong&gt;: The contents (keys and values) of associative arrays can be modified.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dynamic Sizing&lt;/strong&gt;: The size of an associative array can change dynamically as pairs are added or removed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementations
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Hash Tables&lt;/strong&gt;: The most common implementation, using a hash function to map keys to indices in an array.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Separate Chaining&lt;/strong&gt;: Uses linked lists or other structures to handle hash collisions by storing multiple elements at each index.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open Addressing&lt;/strong&gt;: Resolves collisions by probing, or searching through alternative locations in the array.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Self-Balancing Binary Search Trees&lt;/strong&gt;: Such as AVL trees or red-black trees, which maintain sorted order of keys.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strengths&lt;/strong&gt;: Provide O(log n) time complexity for lookup, insertion, and deletion.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Weaknesses&lt;/strong&gt;: Generally slower in average-case performance compared to hash tables.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Trie (Prefix Tree)&lt;/strong&gt;: A specialized tree used for storing associative arrays with string keys.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strengths&lt;/strong&gt;: Efficient for common prefixes and can handle dynamic sets of strings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Weaknesses&lt;/strong&gt;: Can consume a lot of memory for large datasets.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Direct Addressing&lt;/strong&gt;: Uses an array where the position corresponds directly to the key.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strengths&lt;/strong&gt;: Provides O(1) time complexity for all operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Weaknesses&lt;/strong&gt;: Impractical for large key spaces due to high memory usage.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Specialized Variants
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Multimap&lt;/strong&gt;: Allows multiple values to be associated with a single key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bidirectional Map&lt;/strong&gt;: Supports reverse lookups from values to keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ordered Dictionary&lt;/strong&gt;: Maintains a specific order of elements, either by insertion order or sorted order of keys.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Caching&lt;/strong&gt;: Storing results of expensive function calls or database queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration Management&lt;/strong&gt;: Storing configuration settings for software applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database Indexing&lt;/strong&gt;: Facilitating quick lookups in database systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memoization&lt;/strong&gt;: Storing previously computed values to avoid redundant calculations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Symbol Tables&lt;/strong&gt;: Used in compilers and interpreters to store information about variables, functions, and other entities.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Language Support
&lt;/h3&gt;

&lt;p&gt;Many programming languages provide built-in support for associative arrays, often with specific syntax and optimized implementations. Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Python&lt;/strong&gt;: &lt;code&gt;dict&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt;: &lt;code&gt;Object&lt;/code&gt; and &lt;code&gt;Map&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Java&lt;/strong&gt;: &lt;code&gt;HashMap&lt;/code&gt; and &lt;code&gt;TreeMap&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C++&lt;/strong&gt;: &lt;code&gt;std::map&lt;/code&gt; and &lt;code&gt;std::unordered_map&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C#&lt;/strong&gt;: &lt;code&gt;Dictionary&lt;/code&gt; and &lt;code&gt;SortedDictionary&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ruby&lt;/strong&gt;: &lt;code&gt;Hash&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PHP&lt;/strong&gt;: Associative arrays are a core part of the language's array type&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rust&lt;/strong&gt; : &lt;code&gt;HashMap&lt;/code&gt; and &lt;code&gt;BTreeMap&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Performance Considerations
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;: Varies based on the underlying data structure.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hash Tables&lt;/strong&gt;: O(1) average-case time complexity for insertion, deletion, and lookup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Self-Balancing Trees&lt;/strong&gt;: O(log n) time complexity for insertion, deletion, and lookup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Direct Addressing&lt;/strong&gt;: O(1) time complexity, but with potentially high space complexity.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;: Depends on the efficiency of the data structure in managing memory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Hash Tables&lt;/strong&gt;: Generally efficient, but can suffer from memory overhead due to load factors and collision handling mechanisms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trees&lt;/strong&gt;: More predictable memory usage but can be less space-efficient for sparse data sets.ssociative arrays in multi-threaded environments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ConcurrentHashMap&lt;/strong&gt; in Java&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Thread-safe dictionaries&lt;/strong&gt; in Python with locks or &lt;code&gt;collections.defaultdict&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Persistent Data Structures&lt;/strong&gt;: Associative arrays that maintain their previous versions after updates, useful in functional programming.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clojure&lt;/strong&gt;: Persistent hash maps and vectors&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Serialization and Deserialization&lt;/strong&gt;: Converting associative arrays to a format suitable for storage or transmission, and reconstructing them back.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;JSON&lt;/strong&gt;: Common format for serializing associative arrays.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protocol Buffers, Thrift&lt;/strong&gt;: Binary serialization formats for efficient data interchange.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Properties
&lt;/h2&gt;

&lt;p&gt;The operations of the associative array should satisfy various properties to ensure consistent and predictable behavior. These properties define how insertion, lookup, and removal operations interact with each other.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;lookup(k, insert(j, v, D)) = if k == j then v else lookup(k, D)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lookup(k, new()) = fail&lt;/code&gt;, where fail is an exception or default value&lt;/li&gt;
&lt;li&gt;&lt;code&gt;remove(k, insert(j, v, D)) = if k == j then remove(k, D) else insert(j, v, remove(k, D))&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;remove(k, new()) = new()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Definitions
&lt;/h3&gt;

&lt;p&gt;Let's define the terms used in the properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;k&lt;/strong&gt; and &lt;strong&gt;j&lt;/strong&gt;: Keys in the associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;v&lt;/strong&gt;: Value associated with a key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;D&lt;/strong&gt;: An associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;new()&lt;/strong&gt;: A function that creates a new, empty associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;lookup(k, D)&lt;/strong&gt;: A function that retrieves the value associated with the key &lt;strong&gt;k&lt;/strong&gt; in the associative array &lt;strong&gt;D&lt;/strong&gt;. If the key &lt;strong&gt;k&lt;/strong&gt; is not present in &lt;strong&gt;D&lt;/strong&gt;, it returns an exception or a default value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;insert(j, v, D)&lt;/strong&gt;: A function that inserts the key-value pair (&lt;strong&gt;j&lt;/strong&gt;, &lt;strong&gt;v&lt;/strong&gt;) into the associative array &lt;strong&gt;D&lt;/strong&gt;. If &lt;strong&gt;j&lt;/strong&gt; already exists in &lt;strong&gt;D&lt;/strong&gt;, it updates the value associated with &lt;strong&gt;j&lt;/strong&gt; to &lt;strong&gt;v&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;remove(k, D)&lt;/strong&gt;: A function that removes the key &lt;strong&gt;k&lt;/strong&gt; and its associated value from the associative array &lt;strong&gt;D&lt;/strong&gt;. If &lt;strong&gt;k&lt;/strong&gt; is not present in &lt;strong&gt;D&lt;/strong&gt;, the operation has no effect.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Properties
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lookup After Insertion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lookup(k, insert(j, v, D)) = if k == j then v else lookup(k, D)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This property states that if you look up a key &lt;strong&gt;k&lt;/strong&gt; after inserting a key-value pair (&lt;strong&gt;j&lt;/strong&gt;, &lt;strong&gt;v&lt;/strong&gt;) into the associative array &lt;strong&gt;D&lt;/strong&gt;, the result depends on whether &lt;strong&gt;k&lt;/strong&gt; is equal to &lt;strong&gt;j&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;strong&gt;k&lt;/strong&gt; equals &lt;strong&gt;j&lt;/strong&gt;, the lookup should return the value &lt;strong&gt;v&lt;/strong&gt; that was just inserted.&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;k&lt;/strong&gt; does not equal &lt;strong&gt;j&lt;/strong&gt;, the lookup should return the value associated with &lt;strong&gt;k&lt;/strong&gt; in the original associative array &lt;strong&gt;D&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Lookup in a New Associative Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;lookup(k, new()) = fail&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This property states that if you look up any key &lt;strong&gt;k&lt;/strong&gt; in a newly created empty associative array, the operation should fail. The failure can be represented as an exception or a default value, indicating that the key &lt;strong&gt;k&lt;/strong&gt; is not present in the associative array.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Remove After Insertion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;remove(k, insert(j, v, D)) = if k == j then remove(k, D) else insert(j, v, remove(k, D))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This property describes the effect of removing a key &lt;strong&gt;k&lt;/strong&gt; after inserting a key-value pair (&lt;strong&gt;j&lt;/strong&gt;, &lt;strong&gt;v&lt;/strong&gt;) into the associative array &lt;strong&gt;D&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;strong&gt;k&lt;/strong&gt; equals &lt;strong&gt;j&lt;/strong&gt;, the removal operation should result in the associative array &lt;strong&gt;D&lt;/strong&gt; without the key &lt;strong&gt;k&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;k&lt;/strong&gt; does not equal &lt;strong&gt;j&lt;/strong&gt;, the removal operation should first remove the key &lt;strong&gt;k&lt;/strong&gt; from the original associative array &lt;strong&gt;D&lt;/strong&gt; and then insert the key-value pair (&lt;strong&gt;j&lt;/strong&gt;, &lt;strong&gt;v&lt;/strong&gt;). This ensures that the inserted pair (&lt;strong&gt;j&lt;/strong&gt;, &lt;strong&gt;v&lt;/strong&gt;) remains in the associative array, except for the key &lt;strong&gt;k&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Remove in a New Associative Array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;remove(k, new()) = new()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This property states that removing any key &lt;strong&gt;k&lt;/strong&gt; from a newly created empty associative array should result in another new empty associative array. Since the associative array is already empty, the removal operation has no effect.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lookup&lt;/strong&gt; after an &lt;strong&gt;insertion&lt;/strong&gt; either returns the newly inserted value or the value from the original associative array.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lookup&lt;/strong&gt; in a new empty associative array fails, indicating the absence of any keys.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remove&lt;/strong&gt; after an &lt;strong&gt;insertion&lt;/strong&gt; either results in the original associative array without the key if the removed key is the same as the inserted key, or maintains the insertion while removing the specified key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remove&lt;/strong&gt; in a new empty associative array has no effect, leaving the array empty.
## Example&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider a library's system for tracking which books are checked out by which patrons. Each book can be checked out by only one patron at a time, but a patron can check out multiple books. This can be efficiently represented using an associative array, where the keys are the book titles and the values are the patrons who have checked them out. &lt;/p&gt;

&lt;p&gt;Let's use a Python or JSON-like notation to represent this associative array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Pride and Prejudice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Wuthering Heights"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Great Expectations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lookup Operation
&lt;/h3&gt;

&lt;p&gt;If we perform a lookup operation for the key "Great Expectations", we retrieve the value associated with it, which is "John". This means John has checked out the book "Great Expectations":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;patron&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;library&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Great Expectations&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;patron&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Output: "John"
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deletion Operation
&lt;/h3&gt;

&lt;p&gt;If John returns "Great Expectations", we need to remove this key-value pair from the associative array. The deletion operation updates the data structure to reflect that "Great Expectations" is no longer checked out by anyone:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;del&lt;/span&gt; &lt;span class="n"&gt;library&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Great Expectations&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The updated associative array is now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Pride and Prejudice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Wuthering Heights"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Insertion Operation
&lt;/h3&gt;

&lt;p&gt;If a new patron, Pat, checks out a new book, "The Brothers Karamazov", we perform an insertion operation to add this new key-value pair to the associative array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;library&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;The Brothers Karamazov&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Pat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The associative array now reflects the new state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Pride and Prejudice"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"Wuthering Heights"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Alice"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"The Brothers Karamazov"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Pat"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Comprehensive Explanation
&lt;/h3&gt;

&lt;p&gt;In this example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Initial State&lt;/strong&gt;: The associative array initially has three books checked out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Pride and Prejudice" and "Wuthering Heights" are checked out by Alice.&lt;/li&gt;
&lt;li&gt;"Great Expectations" is checked out by John.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lookup Operation&lt;/strong&gt;: Performing &lt;code&gt;lookup("Great Expectations")&lt;/code&gt; returns "John", indicating that John has checked out "Great Expectations".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deletion Operation&lt;/strong&gt;: When John returns "Great Expectations", we remove this entry from the associative array using &lt;code&gt;delete("Great Expectations")&lt;/code&gt;. The updated array no longer includes "Great Expectations".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insertion Operation&lt;/strong&gt;: When Pat checks out "The Brothers Karamazov", we add this new entry to the associative array using &lt;code&gt;insert("The Brothers Karamazov", "Pat")&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By using an associative array, the library system efficiently manages the check-out status of books, allowing for quick lookups, insertions, and deletions. This method ensures that each book is accurately tracked, and patrons' interactions with the library's inventory are easily managed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Implementing a dictionary (associative array) can vary based on the size and characteristics of the dataset. Here are two straightforward implementation techniques: using an association list and using direct addressing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Association List
&lt;/h3&gt;

&lt;p&gt;An association list is a simple implementation suitable for dictionaries with a small number of mappings. Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Structure&lt;/strong&gt;: The dictionary is represented as a linked list, where each node contains a key-value pair (mapping).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insertion&lt;/strong&gt;: To insert a new key-value pair, you add a new node with the given key and value at the beginning (or end) of the list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lookup&lt;/strong&gt;: To find the value associated with a key, you start at the beginning of the list and traverse each node, checking the key in each node until you find the desired key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deletion&lt;/strong&gt;: To remove a key-value pair, you traverse the list to find the node with the key, then update the pointers to exclude that node from the list.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple to implement.&lt;/li&gt;
&lt;li&gt;Suitable for small dictionaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lookup, insertion, and deletion operations have a linear time complexity (O(n)), where n is the number of mappings.&lt;/li&gt;
&lt;li&gt;Not efficient for large dictionaries due to the linear search time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Direct Addressing
&lt;/h3&gt;

&lt;p&gt;Direct addressing is a technique used when keys are restricted to a narrow range. This method uses an array to store values, where the position in the array directly corresponds to a key. Here’s how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Structure&lt;/strong&gt;: An array is created with a size equal to the range of possible keys. Each position in the array represents a key, and the value at that position is the value associated with that key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insertion&lt;/strong&gt;: To insert a key-value pair, you place the value in the array position corresponding to the key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Lookup&lt;/strong&gt;: To find the value associated with a key, you simply access the array position corresponding to the key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deletion&lt;/strong&gt;: To remove a key-value pair, you set the array position corresponding to the key to a special sentinel value indicating the absence of a value.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lookup, insertion, and deletion operations have constant time complexity (O(1)).&lt;/li&gt;
&lt;li&gt;Extremely fast due to direct access.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Requires a large amount of memory if the range of possible keys is large.&lt;/li&gt;
&lt;li&gt;Impractical if the key space is very sparse or large because of the excessive memory usage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Comparison
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Association List&lt;/strong&gt;: Best for small dictionaries where the overhead of managing a list is acceptable. It's simple and has small constant factors in its running time, but operations can become slow as the dictionary grows.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Direct Addressing&lt;/strong&gt;: Best for scenarios where keys fall within a small, contiguous range, allowing for constant time operations. However, it becomes impractical if the key range is vast, as it requires a large array that could waste a lot of memory.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example Scenario
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Association List&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Insertion&lt;/strong&gt;: Add a node with the key-value pair to the start of the linked list.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lookup&lt;/strong&gt;: Traverse the list from the beginning, checking each node’s key until the target key is found.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deletion&lt;/strong&gt;: Traverse the list to find the node with the target key, then update the list pointers to remove that node.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Direct Addressing&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Insertion&lt;/strong&gt;: Store the value at the array index corresponding to the key.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lookup&lt;/strong&gt;: Directly access the array at the index of the key to retrieve the value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deletion&lt;/strong&gt;: Set the array element at the index of the key to a sentinel value indicating no value.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Hash Table Implementations
&lt;/h3&gt;

&lt;p&gt;The most frequently used general-purpose implementation of an associative array is with a hash table: an array combined with a hash function that separates each key into a separate "bucket" of the array. The basic idea behind a hash table is that accessing an element of an array via its index is a simple, constant-time operation. Therefore, the average overhead of an operation for a hash table is only the computation of the key's hash, combined with accessing the corresponding bucket within the array. As such, hash tables usually perform in O(1) time, and usually outperform alternative implementations.&lt;/p&gt;

&lt;p&gt;Hash tables must be able to handle collisions: the mapping by the hash function of two different keys to the same bucket of the array. The two most widespread approaches to this problem are separate chaining and open addressing. In separate chaining, the array does not store the value itself but stores a pointer to another container, usually an association list, that stores all the values matching the hash. By contrast, in open addressing, if a hash collision is found, the table seeks an empty spot in an array to store the value in a deterministic manner, usually by looking at the next immediate position in the array.&lt;/p&gt;

&lt;p&gt;Open addressing has a lower cache miss ratio than separate chaining when the table is mostly empty. However, as the table becomes filled with more elements, open addressing's performance degrades exponentially. Additionally, separate chaining uses less memory in most cases, unless the entries are very small (less than four times the size of a pointer).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core Idea:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine a large filing cabinet with numerous drawers labeled with numbers (buckets). A hash function acts like a super-fast sorting mechanism. It takes a key (like a person's name) and converts it into a unique number (drawer number) within a predefined range. This number is used to directly access the corresponding bucket in the cabinet (hash table array). Ideally, each key maps to a unique bucket, allowing for quick retrieval of its associated value stored in that bucket.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation Steps:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define the Hash Table:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An array to store key-value pairs (buckets).&lt;/li&gt;
&lt;li&gt;A hash function that takes a key and returns a bucket index within the array's range.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Adding a Key-Value Pair:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calculate the hash index for the key using the hash function.&lt;/li&gt;
&lt;li&gt;Check the bucket at that index:

&lt;ul&gt;
&lt;li&gt;If empty, store the key-value pair directly in that bucket.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collision Resolution:&lt;/strong&gt; If the bucket is already occupied (collision!), a collision resolution strategy comes into play (explained later).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Finding a Value:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calculate the hash index for the key.&lt;/li&gt;
&lt;li&gt;Look at the bucket at that index.

&lt;ul&gt;
&lt;li&gt;If it contains the exact key-value pair, return the value.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collision Resolution:&lt;/strong&gt; If there's a collision, use the chosen strategy to find the key-value pair within the bucket or nearby locations (explained later).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Removing a Key-Value Pair:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Calculate the hash index for the key.&lt;/li&gt;
&lt;li&gt;Find the key-value pair using the collision resolution strategy (similar to finding).&lt;/li&gt;
&lt;li&gt;Once located, remove the pair from the bucket.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Collision Resolution Techniques:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Separate Chaining:&lt;/strong&gt; Each bucket stores a linked list or another data structure to hold all key-value pairs that hashed to the same index. This offers flexibility but can increase memory usage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Open Addressing:&lt;/strong&gt; If the bucket is full, the table probes for the next empty slot in the array following a predefined pattern (linear probing, quadratic probing, etc.). This is faster in terms of space but might lead to clustering of elements and slower access as the table fills up.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Choosing the Right Method:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Separate chaining is generally preferred for its memory efficiency and simpler implementation, especially for scenarios with a high fill factor (many elements in the table). Open addressing might be considered if memory is a tight constraint and the fill factor is expected to be low.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hash Function Design:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A good hash function should distribute keys uniformly across the buckets to minimize collisions. Common techniques involve bitwise operations and mathematical manipulations on the key's data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tree Implementations
&lt;/h3&gt;

&lt;p&gt;Self-balancing binary search trees are another common approach to implementing an associative array, such as an AVL tree or a red–black tree.&lt;/p&gt;

&lt;p&gt;Compared to hash tables, these structures have both strengths and weaknesses. The worst-case performance of self-balancing binary search trees is significantly better than that of a hash table, with a time complexity in big O notation of O(log n). This is in contrast to hash tables, whose worst-case performance involves all elements sharing a single bucket, resulting in O(n) time complexity. In addition, and like all binary search trees, self-balancing binary search trees keep their elements in order. Thus, traversing its elements follows a least-to-greatest pattern, whereas traversing a hash table can result in elements being in seemingly random order. Because they are in order, tree-based maps can also satisfy range queries (find all values between two bounds) whereas a hashmap can only find exact values. However, hash tables have a much better average-case time complexity than self-balancing binary search trees of O(1), and their worst-case performance is highly unlikely when a good hash function is used.&lt;/p&gt;

&lt;p&gt;A self-balancing binary search tree can be used to implement the buckets for a hash table that uses separate chaining. This allows for average-case constant lookup but assures a worst-case performance of O(log n). However, this introduces extra complexity into the implementation and may cause even worse performance for smaller hash tables, where the time spent inserting into and balancing the tree is greater than the time needed to perform a linear search on all elements of a linked list or similar data structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strengths of Trees:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Worst-Case Efficiency:&lt;/strong&gt; Tree operations like finding, adding, or removing elements have a worst-case time complexity of O(log n), where n is the number of elements. This is significantly better than the worst-case O(n) for hash tables that suffer from collisions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ordered Elements:&lt;/strong&gt; Unlike hash tables, trees inherently keep elements in a specific order (usually ascending or descending). This allows for efficient retrieval of elements within a specific range (range queries) – a feature not readily available with hash tables.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementation (Conceptual):&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Structure:&lt;/strong&gt; Imagine a binary tree where each node holds a key-value pair.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ordering:&lt;/strong&gt; Keys are compared during insertion to maintain the order property (e.g., left subtree contains keys less than the current node's key, right subtree contains greater keys).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Self-Balancing:&lt;/strong&gt; Specific balancing rules (AVL or red-black) ensure the tree remains roughly balanced, meaning the height (number of levels) doesn't grow excessively with each insertion. This is crucial for maintaining efficient operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Comparison with Hash Tables:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Average Case:&lt;/strong&gt; Hash tables generally excel in average-case performance, with O(1) time complexity for most operations due to their direct bucket access using a hash function. This can be faster than tree operations, especially for smaller dictionaries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Worst Case:&lt;/strong&gt; However, hash tables can suffer from collisions where multiple keys map to the same bucket, leading to a worst-case O(n) lookup time if the collision resolution strategy involves iterating through all elements in the bucket. Trees guarantee a worst-case of O(log n) even with unbalanced data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hybrid Approach:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Separate Chaining with Trees:&lt;/strong&gt; A self-balancing tree can be used as the underlying data structure for each bucket in a hash table that employs separate chaining. This approach offers the average-case efficiency of O(1) from hash tables with the worst-case guarantee of O(log n) from trees.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Trade-offs:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Complexity:&lt;/strong&gt; This hybrid approach adds complexity to the implementation compared to a basic hash table with linked lists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overhead for Smaller Tables:&lt;/strong&gt; For small dictionaries, the overhead of maintaining the tree's balance in the hybrid approach might outweigh the benefits. A simple linked list within each bucket might be more efficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Choosing the Right Approach:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The choice between trees and hash tables depends on your priorities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;strong&gt;worst-case performance&lt;/strong&gt; is critical and you can tolerate a slightly slower average-case lookup, a self-balancing tree might be ideal.&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;average-case speed&lt;/strong&gt; is paramount, and the worst-case scenario is less concerning with a good hash function, a hash table is generally preferred.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Other Trees
&lt;/h3&gt;

&lt;p&gt;Associative arrays may also be stored in unbalanced binary search trees or in data structures specialized to a particular type of keys such as radix trees, tries, Judy arrays, or van Emde Boas trees, though the relative performance of these implementations varies. For instance, Judy trees have been found to perform less efficiently than hash tables, while carefully selected hash tables generally perform more efficiently than adaptive radix trees, with potentially greater restrictions on the data types they can handle. The advantages of these alternative structures come from their ability to handle additional associative array operations, such as finding the mapping whose key is the closest to a queried key when the query is absent in the set of mappings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Underlying Data Structure&lt;/th&gt;
&lt;th&gt;Lookup or Removal&lt;/th&gt;
&lt;th&gt;Insertion&lt;/th&gt;
&lt;th&gt;Ordered&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;td&gt;Worst Case&lt;/td&gt;
&lt;td&gt;Average&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hash table&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Self-balancing binary search tree&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unbalanced binary search tree&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(log n)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sequential container of key–value pairs (e.g. association list)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(n)&lt;/td&gt;
&lt;td&gt;O(1)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;O(1)       | No    |&lt;/p&gt;

&lt;h2&gt;
  
  
  Ordered Dictionary
&lt;/h2&gt;

&lt;p&gt;While dictionaries typically focus on efficient key-value retrieval, some use cases require a specific order for iterating through the elements. This is where ordered dictionaries come in. There are two main interpretations of "ordered":&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Order based on Sorting Keys:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In this approach, the order of elements during iteration is determined by sorting the keys within the dictionary. This is similar to how elements in a tree-based implementation (e.g., C++ &lt;code&gt;&amp;lt;map&amp;gt;&lt;/code&gt;) are presented.&lt;/li&gt;
&lt;li&gt;Whenever the dictionary is modified (adding, removing, or updating elements), the keys are potentially re-sorted to maintain the desired order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Order based on Insertion:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is the more prevalent approach. The order of elements reflects the sequence in which they were added to the dictionary. This means the first element inserted is retrieved first during iteration, followed by the second, and so on.&lt;/li&gt;
&lt;li&gt;This insertion order is preserved even if elements are subsequently updated or removed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Implementation Techniques:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are three common ways to implement ordered dictionaries:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Association List:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;This is a simple approach for small dictionaries. It uses a linked list where each node holds a key-value pair. New elements are added at the end, maintaining insertion order.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Doubly Linked List on Top of a Regular Dictionary:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;This technique combines a standard dictionary (like a hash table) for efficient key-based lookups with a doubly linked list. The linked list connects all the key-value pairs in the order they were inserted, regardless of their position in the underlying dictionary.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Dense Insertion-Ordered Array:&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;This approach abandons the sparse (potentially unevenly filled) array structure of a traditional hash table. Instead, it uses a dense array where elements are stored consecutively based on insertion order. This offers efficient iteration but might require resizing the array as elements are added.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Choosing the Right Approach:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The best implementation for an ordered dictionary depends on factors like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Expected size:&lt;/strong&gt; Association lists are suitable for small dictionaries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access patterns:&lt;/strong&gt; If frequent key-based lookups are needed, a hybrid approach with a dictionary and linked list might be beneficial.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory usage:&lt;/strong&gt; Dense arrays can be less memory-efficient compared to other methods, especially for frequently updated dictionaries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Language Support
&lt;/h2&gt;

&lt;p&gt;Associative arrays can be implemented in any programming language as a package, and many language systems provide them as part of their standard library. In some languages, they are not only built into the standard system, but have special syntax, often using array-like subscripting.&lt;/p&gt;

&lt;p&gt;Built-in syntactic support for associative arrays was introduced in 1969 by SNOBOL4, under the name "table". TMG offered tables with string keys and integer values. MUMPS made multi-dimensional associative arrays, optionally persistent, its key data structure. SETL supported them as one possible implementation of sets and maps. Most modern scripting languages, starting with AWK and including Rexx, Perl, PHP, Tcl, JavaScript, Maple, Python, Ruby, Wolfram Language, Go, and Lua, support associative arrays as a primary container type. In many more languages, they are available as library functions without special syntax.&lt;/p&gt;

&lt;p&gt;In Smalltalk, Objective-C, .NET, Python, REALbasic, Swift, VBA and Delphi they are called dictionaries; in Perl, Ruby and Seed7 they are called hashes; in C++, C#, Java, Go, Clojure, Scala, OCaml, Haskell they are called maps (see map (C++), unordered_map (C++), and Map); in Common Lisp and Windows PowerShell, they are called hash tables (since both typically use this implementation); in Maple and Lua, they are called tables. In PHP, all arrays can be associative, except that the keys are limited to integers and strings. In JavaScript (see also JSON), all objects behave as associative arrays with string-valued keys, while the Map and WeakMap types take arbitrary objects as keys. In Lua, they are used as the primitive building block for all data structures. In Visual FoxPro, they are called Collections. The D language also supports associative arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  Permanent Storage
&lt;/h2&gt;

&lt;p&gt;Many programs using associative arrays will need to store that data in a more permanent form, such as a computer file. A common solution to this problem is a generalized concept known as archiving or serialization, which produces a text or binary representation of the original objects that can be written directly to a file. This is most commonly implemented in the underlying object model, like .Net or Cocoa, which includes standard functions that convert the internal data into text. The program can create a complete text representation of any group of objects by calling these methods, which are almost always already implemented in the base associative array class.&lt;/p&gt;

&lt;p&gt;For programs that use very large data sets, this sort of individual file storage is not appropriate, and a database management system (DB) is required. Some DB systems natively store associative arrays by serializing the data and then storing that serialized data and the key. Individual arrays can then be loaded or saved from the database using the key to refer to them. These key–value stores have been used for many years and have a history as long as that of the more common relational database (RDBs), but a lack of standardization, among other reasons, limited their use to certain niche roles. RDBs were used for these roles in most cases, although saving objects to a RDB can be complicated, a problem known as object-relational impedance mismatch.&lt;/p&gt;

&lt;p&gt;After approximately 2010, the need for high-performance databases suitable for cloud computing and more closely matching the internal structure of the programs using them led to a renaissance in the key–value store market. These systems can store and retrieve associative arrays in a native fashion, which can greatly improve performance in common web-related workflows.&lt;/p&gt;

&lt;h2&gt;
  
  
  History and Evolution of Associative Arrays
&lt;/h2&gt;

&lt;p&gt;The concept of associative arrays dates back to the early days of computing. Early examples include the use of symbol tables in compilers and interpreters, which are used to store information about program variables and other entities.&lt;/p&gt;

&lt;h3&gt;
  
  
  Early Implementations
&lt;/h3&gt;

&lt;p&gt;The first formal implementations of associative arrays were in high-level programming languages in the 1960s. SNOBOL4, developed in 1969, was one of the first languages to provide built-in associative arrays under the name "tables." Similarly, TMG offered tables with string keys and integer values, and MUMPS introduced multi-dimensional associative arrays.&lt;/p&gt;

&lt;h3&gt;
  
  
  Growth in Popularity
&lt;/h3&gt;

&lt;p&gt;As programming languages evolved, associative arrays became a common feature. The development of scripting languages in the 1980s and 1990s, such as AWK, Perl, and TCL, heavily utilized associative arrays due to their flexibility and ease of use in handling dynamic and unstructured data. &lt;/p&gt;

&lt;p&gt;The 2000s saw associative arrays becoming a fundamental part of many programming languages, including Python, Ruby, PHP, and JavaScript. These languages provided built-in support for associative arrays, often with optimized implementations that offered efficient performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modern Implementations
&lt;/h3&gt;

&lt;p&gt;In modern programming, associative arrays are ubiquitous. They are often referred to as dictionaries, maps, or hashes, depending on the language. Modern languages and frameworks have optimized their associative array implementations to handle large datasets efficiently and provide robust methods for serialization, iteration, and other operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Associative Arrays in Databases
&lt;/h3&gt;

&lt;p&gt;The rise of NoSQL databases has further cemented the importance of associative arrays. NoSQL databases like MongoDB, Redis, and DynamoDB use key-value pairs as their primary data model. This allows for highly scalable and flexible data storage solutions, catering to modern applications that require real-time processing and horizontal scalability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Usage and Techniques
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Complex Keys and Values
&lt;/h3&gt;

&lt;p&gt;While traditional associative arrays use simple data types for keys (like integers or strings), more advanced implementations support complex keys. For example, in Python, the &lt;code&gt;frozenset&lt;/code&gt; type can be used as a dictionary key, allowing for the creation of complex key structures.&lt;/p&gt;

&lt;p&gt;Values in associative arrays can also be complex. They can be other associative arrays, leading to nested structures. This is particularly useful in representing JSON data structures or hierarchical data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory Efficiency
&lt;/h3&gt;

&lt;p&gt;Efficient memory usage is critical in associative arrays, especially when dealing with large datasets. Techniques like open addressing and separate chaining in hash tables help manage memory effectively. Additionally, some languages and libraries implement memory pooling and garbage collection optimizations to minimize the overhead associated with associative arrays.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thread Safety and Concurrency
&lt;/h3&gt;

&lt;p&gt;In multi-threaded applications, associative arrays can become a bottleneck if not managed properly. Concurrent implementations of associative arrays, like &lt;code&gt;ConcurrentHashMap&lt;/code&gt; in Java, provide thread-safe operations. These implementations use techniques such as lock striping and non-blocking algorithms to ensure that multiple threads can access and modify the associative array concurrently without significant performance degradation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Persistent Associative Arrays
&lt;/h3&gt;

&lt;p&gt;Persistent data structures retain their previous version after modifications. Persistent associative arrays are particularly useful in functional programming and versioned data storage systems. Languages like Clojure offer persistent hash maps, enabling efficient immutability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use in Algorithms and Data Structures
&lt;/h3&gt;

&lt;p&gt;Associative arrays are fundamental in various algorithms and data structures. They are used in caching (e.g., memoization), implementing sets, graphs (as adjacency lists), and in many more complex data structures like priority queues and indexed priority search queues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Web Development
&lt;/h3&gt;

&lt;p&gt;Associative arrays are extensively used in web development. In JavaScript, objects are the primary way to represent and manipulate structured data, both on the client side and server side. They are used to handle JSON data, which is the de facto standard for data interchange in web applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuration Management
&lt;/h3&gt;

&lt;p&gt;In many software systems, configuration settings are managed using associative arrays. These settings are often stored in configuration files (e.g., JSON, YAML) and loaded into associative arrays for easy access and manipulation within the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data Analysis
&lt;/h3&gt;

&lt;p&gt;In data analysis and scientific computing, associative arrays (often implemented as dictionaries or data frames) are used to store and manipulate large datasets. Libraries like Pandas in Python provide powerful tools for data manipulation, where the underlying data structure is often a dictionary of arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  Associative Arrays in Distributed Systems
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Distributed Hash Tables (DHTs)
&lt;/h3&gt;

&lt;p&gt;Distributed Hash Tables (DHTs) are a class of decentralized distributed systems that provide a lookup service similar to a hash table. Key-value pairs are stored in a distributed manner across multiple nodes. DHTs are designed to scale to large numbers of nodes and handle node arrivals, departures, and failures gracefully.&lt;/p&gt;

&lt;h4&gt;
  
  
  Key Concepts:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Chord&lt;/strong&gt;: One of the most well-known DHT implementations, which uses consistent hashing to distribute keys evenly across nodes and provides efficient lookup with logarithmic complexity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Kademlia&lt;/strong&gt;: Another popular DHT implementation that uses XOR metric for distance measurement between keys, enabling efficient node lookup and self-organization.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Key-Value Stores
&lt;/h3&gt;

&lt;p&gt;Key-Value Stores are a type of NoSQL database that uses associative arrays as their primary data model. They are optimized for fast read and write operations and are often used in distributed systems to store large volumes of data.&lt;/p&gt;

&lt;h4&gt;
  
  
  Examples:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Amazon DynamoDB&lt;/strong&gt;: A fully managed key-value and document database that delivers single-digit millisecond performance at any scale. DynamoDB is designed to run high-performance, internet-scale applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Redis&lt;/strong&gt;: An open-source, in-memory key-value store that supports various data structures such as strings, hashes, lists, sets, and sorted sets. It is often used for caching, session management, and real-time analytics.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Consistency Models
&lt;/h3&gt;

&lt;p&gt;In distributed systems, maintaining consistency of associative arrays across multiple nodes is a significant challenge. Different consistency models are used to balance the trade-offs between performance, availability, and data consistency.&lt;/p&gt;

&lt;h4&gt;
  
  
  Types of Consistency Models:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strong Consistency&lt;/strong&gt;: Guarantees that all nodes see the same data at the same time. Operations appear instantaneous and globally synchronized.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eventual Consistency&lt;/strong&gt;: Guarantees that, given enough time, all nodes will converge to the same state. This model is often used in systems where high availability is more critical than immediate consistency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Causal Consistency&lt;/strong&gt;: Ensures that operations that are causally related are seen by all nodes in the same order, while concurrent operations may be seen in different orders.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Distributed Caching
&lt;/h3&gt;

&lt;p&gt;Distributed caching involves storing key-value pairs in memory across multiple nodes to improve the performance of read-heavy applications. Associative arrays play a crucial role in caching mechanisms by providing fast access to frequently used data.&lt;/p&gt;

&lt;h4&gt;
  
  
  Technologies:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memcached&lt;/strong&gt;: A high-performance, distributed memory caching system that is used to speed up dynamic web applications by alleviating database load.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hazelcast&lt;/strong&gt;: An in-memory data grid that offers distributed caching, data partitioning, and replication for high scalability and resilience.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conflict Resolution
&lt;/h3&gt;

&lt;p&gt;In distributed associative arrays, conflicts can occur when multiple nodes attempt to update the same key simultaneously. Various strategies are employed to resolve these conflicts:&lt;/p&gt;

&lt;h4&gt;
  
  
  Techniques:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Last-Write-Wins (LWW)&lt;/strong&gt;: A simple conflict resolution strategy where the most recent write operation is retained.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vector Clocks&lt;/strong&gt;: A more sophisticated technique that tracks the history of changes to detect and resolve conflicts in a causally consistent manner.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Operational Transformation&lt;/strong&gt;: Used in collaborative environments, it allows concurrent edits to be merged in a consistent manner.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Performance and Scalability
&lt;/h3&gt;

&lt;p&gt;Performance and scalability are critical concerns in distributed associative arrays. Techniques such as sharding (partitioning data across multiple nodes), replication (duplicating data across nodes for redundancy), and load balancing (distributing workload evenly across nodes) are employed to ensure efficient and reliable operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Content Delivery Networks (CDNs)&lt;/strong&gt;: Use distributed caching to store web content close to users, reducing latency and improving load times.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Big Data Analytics&lt;/strong&gt;: Key-value stores are used to store and retrieve large datasets efficiently, enabling real-time data processing and analysis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internet of Things (IoT)&lt;/strong&gt;: Associative arrays are used to manage device metadata, configurations, and state information in distributed IoT platforms.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Challenges and Future Directions
&lt;/h3&gt;

&lt;p&gt;The future of associative arrays in distributed systems includes addressing challenges such as improving consistency models, optimizing performance for large-scale deployments, and developing more robust conflict resolution techniques. Advancements in areas like edge computing, machine learning, and blockchain also present new opportunities for leveraging associative arrays in innovative ways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Associative arrays are a fundamental and versatile data structure in computer science, enabling efficient storage and retrieval of key-value pairs. Their wide adoption across programming languages and applications highlights their importance. From simple implementations in early programming languages to sophisticated, concurrent, and persistent versions in modern software, associative arrays continue to be a cornerstone of data manipulation and storage solutions. Understanding their implementation, performance characteristics, and use cases is crucial for any software developer or computer scientist.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/m-mdy-m/algorithms-data-structures/blob/main/10.Data_Structures/Array/Associative_Arrays/example/associative.rs" rel="noopener noreferrer"&gt;Rust&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/m-mdy-m/algorithms-data-structures/blob/main/10.Data_Structures/Array/Associative_Arrays/example/associative.ts" rel="noopener noreferrer"&gt;Ts&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/m-mdy-m/algorithms-data-structures/blob/main/10.Data_Structures/Array/Associative_Arrays/example/associative.js" rel="noopener noreferrer"&gt;Js&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/m-mdy-m/algorithms-data-structures/blob/main/10.Data_Structures/Array/Associative_Arrays/example/associative.java" rel="noopener noreferrer"&gt;Java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/m-mdy-m/algorithms-data-structures/blob/main/10.Data_Structures/Array/Associative_Arrays/example/associative.py" rel="noopener noreferrer"&gt;Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/m-mdy-m/algorithms-data-structures/blob/main/10.Data_Structures/Array/Associative_Arrays/example/associative.go" rel="noopener noreferrer"&gt;Golang&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Deepen Your Algorithmic Journey: A World of Discovery Awaits
&lt;/h2&gt;

&lt;p&gt;Excited to delve deeper into the world of non-linear array addressing and beyond? My GitHub repository, &lt;strong&gt;&lt;a href="https://github.com/m-mdy-m/algorithms-data-structures" rel="noopener noreferrer"&gt;Algorithms &amp;amp; Data Structures&lt;/a&gt;&lt;/strong&gt;, offers a treasure trove of algorithms and data structures for you to explore.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Experiment, Practice, and Master:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dive into:&lt;/strong&gt; A diverse collection of algorithms and data structures awaits your exploration, providing ample opportunity to practice, solidify your knowledge, and refine your understanding.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Continuous Growth:&lt;/strong&gt; While some sections are actively under development as part of my ongoing learning journey (estimated completion: 2-3 years), the repository is constantly expanding with new content.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Let's Build a Community of Learners:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The quest for knowledge doesn't end with exploration! I actively encourage feedback and collaboration. Encountered a challenge? Have a suggestion for improvement? Eager to discuss algorithms and performance optimization? Reach out and let's connect!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Join the Conversation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Twitter:&lt;/strong&gt; &lt;a href="https://twitter.com/m__mdy__m" rel="noopener noreferrer"&gt;@m_&lt;em&gt;mdy&lt;/em&gt;_m&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Telegram:&lt;/strong&gt; &lt;strong&gt;Join my channel here: &lt;a href="https://t.me/medishn" rel="noopener noreferrer"&gt;https://t.me/medishn&lt;/a&gt;&lt;/strong&gt; (Note: This is the preferred channel for the most up-to-date discussions)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/m-mdy-m" rel="noopener noreferrer"&gt;m-mdy-m&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Together, let's build a vibrant learning community where we can share knowledge and push the boundaries of our understanding.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
