1/30
Module 11-14
the foundational process of arranging a collection of data elements in a specific, predictable order based on their values (either ascending or descending).
Sorting
a comparative tracking algorithm. It uses nested loops to pass through an array. During a single pass, it evaluates index A[j] and its immediate neighbor. If the value at A[j] is strictly greater than the value at A[j+1], a swap is executed using a temporary variable (temp) to prevent data loss. The outer loop decreases the boundary of evaluated elements because, after the first full pass, the absolute largest number is guaranteed to have shifted to the very end of the array.
Performance Metrics:
Best-Case: O(n) occurs when the list is completely pre-sorted. The algorithm makes one structural pass, notices zero swaps were made, and breaks early.
Worst/Average Case: O(n^2) occurs because every single item must be systematically compared with every other remaining item. It is highly inefficient for large data frameworks.
Bubble Sort (Elementary)
virtually divides the target array into two distinct areas: a “sorted” sublist on the left and an “unsorted” sublist on the right. It begins at index 2 (the second element) and stores this value in a variable called index (or the key). It then looks backward through the sorted sublist to its left. While the items on the left are larger than the current index, the algorithm continually shifts those larger elements one space to the right to make a vacancy. Once it finds the correct relative position, it “inserts” the stored value into that slot.
Performance Metrics: While it shares a theoretical worst-case time complexity of O(n^2) with Bubble Sort, it runs roughly twice as fast in practice because shifting elements in a linear block requires fewer computational overhead actions than executing continuous logical swaps.
Insertion Sort (Elementary)
operates on a “search and capture” mechanism. The algorithm loops through the array from left to right. For every position i, it assumes the current item is the minimum (min = i). It then scans the rest of the remaining unsorted items to the right to find if an even smaller value exists. If it finds a smaller element, it updates the minimum index tracking pointer (Min = j). After completing the internal scan for that pass, it performs exactly one swap to place that absolute minimum value into position A[i].
Selection Sort (Elementary)
an advanced optimization of Insertion Sort designed by Donald Shell. Insertion Sort’s primary weakness is that it only shifts elements one position at a time. This solves this by establishing a structural spacing factor called an “increment” (k). It breaks the array into interleaved segments where elements are separated by k positions. It then runs a standard Insertion Sort on each isolated segment. As the algorithm loops, the increment k is systematically reduced (divided by 2) until k=1. Because elements were sorted over large gaps early on, the final k=1 pass requires very few shifts.
Shell Sort (Efficient)
a highly optimized, recursive algorithmic paradigm built on a “Divide and Conquer” architectural layout.
Quick Sort (Efficient)
The algorithm selects an item to serve as a benchmark value, known as the “Pivot” (commonly the leftmost element, A[left]).
Pivot Selection
The array is partitioned by using two pointers (left and right) that move inward. Elements greater than the pivot are cataloged to the right side, and elements smaller than the pivot are cataloged to the left side.
Partitioning
Once the pointers meet, the pivot is placed in its permanent, correct location. The algorithm then splits the array into two new independent sublists (the left half and the right half) and calls itself (Qsort) recursively to process both sides until the sublists contain one or zero elements.
Recursive Step
is entirely built upon the distributive property of real numbers. When you multiply a group inside parentheses by a term outside, you are scaling the entire value of that expression. This means every single isolated item within the boundaries of the parentheses must be multiplied by the external factor equitably.
Polynomial Multiplication
Single term multiplication:
-5(2x²)
3(4a²b)
Distributing a Constant
A polynomial with only one single term.
(4a²)(-5a^7)
(12x²y)(12x^5y³z)
Multiplying Single Terms
Multiplication can be shown by juxtaposition (putting the term right next to the parenthesis).
x(x² +12)
3x²(x² -5)
Advanced Distribution
(x+4)(x-3)
Two Terms Times Two Terms
When polynomials get past two terms, horizontal multiplication becomes painful and risky.
(x+3) (4x² – 4x – 7)
Multiplying Larger Polynomials
There are two distinct paths to handle repetitive logic in programming:
iteration & recursion
Uses traditional loops (like for or while) where logic relies solely on variables/parameters, not the algorithm itself.
iteration
A repetitive process where an algorithm calls itself directly or indirectly to solve a problem.
Recursion
is more than a syntax choice; it is a fundamental shift in how program tasks are broken down.
Recursion
Multiplies numbers sequentially from 1 up to n.
• Formula:
{Factorial}(n) = n x (n-1) x … x 1
Iterative Approach PDF
The algorithm appears directly within its own definition.
• Formula:
{Factorial}(n) = n x {Factorial}(n-1)
Recursive Approach PDF
never a one-way street; it always involves a two-way journey.
recursion
Decomposing the big problem into smaller, identical pieces from top to bottom.
The Downward Journey
Solving the individual pieces step-by-step from bottom to top once the base limit is hit.
The Upward Journey
ANATOMY OF A SUBPROGRAM CALL
Recursion relies strictly on a standard underlying framework of function/method calls.
When a program executes a call, the current system module suspends its processing.
Control is completely handed over to the newly called subprogram.
Once the subprogram completes its execution and returns, the parent module resumes exactly where it left off.
Three Core Steps to Design a Recursive Algorithm
Determine the Base Case
Determine the General Case
Combine Both Blocks
Identify the statement or condition that solves and stops the problem directly without calling itself again. (Example: {Factorial}(0) = 1).
Determine the Base Case
Establish the rest of the algorithm, which focuses on reducing the size of the operational problem. (Example: n x {Factorial}(n-1)).
Determine the General Case
Unify the structural base case and general case logic cleanly into an integrated conditional algorithm.
Combine Both Blocks
Used primarily whenever the targeted list is not ordered (unsorted).
Recommended only for small lists or lists that are not searched frequently.
Starts at the very beginning of the list and checks element by element until the target is found or the end of the list is reached.
Sequential (Linear) Search
Relies strictly on the Divide-and-Conquer technique.
CRITICAL REQUIREMENT: The array must be sequenced/ordered (e.g., ascending order).
The Three Core Steps:
1. Divide: Split the instance into two smaller halves.
2. Recur: Solve the smaller instance recursively.
3. Conquer: Discard the unneeded half; no extra step is needed to combine solutions.
Binary Search (Divide and Conquer