3)Design an algorithm for performing selection sor
3)Design an algorithm for performing selection sor
Algorithm Design
Pseudocode:
Key Steps:
1. Outer Loop: Iterates through each position i in the array (from 0 to n-2).
2. Inner Loop: Finds the index of the minimum element in the unsorted subarray
A[i...n-1].
Explanation:
Swaps: Fixed at $ n-1 $ (linear), as one swap occurs per outer loop iteration.
Space Complexity
Selection sort uses $ O(1) $ auxiliary space, as it operates in-place with temporary
variables for swaps.
Algorithm Design
Pseudocode:
Key Steps:
1. Outer Loop: Iterates through all possible starting positions i in the text (from 0 to n
- m).
2. Inner Loop: Compares characters of the pattern with the text starting at i until a
mismatch occurs or the entire pattern matches.
3. Termination: Returns the starting index if a match is found; otherwise, returns -1.
Explanation:
Best Case: Occurs when the pattern’s first character never matches the text,
resulting in $ n - m + 1 $ quick comparisons.
Worst Case: Quadratic time $ O(mn) $ arises when partial matches occur
frequently (common in small alphabets like binary strings).
Average Case: Linear time $ O(n) $ assumes uniform character distribution and
finite alphabet size, where most mismatches occur early.
Space Complexity
The algorithm uses $ O(1) $ auxiliary space, as it only requires indices for comparison.
Clearly define the problem statement, including inputs, outputs, and constraints.
Select appropriate data structures and algorithm design techniques (e.g., divide-
and-conquer, dynamic programming).
4. Prove Correctness
Verify that the algorithm produces correct results for all valid inputs.
Use techniques like mathematical induction or testing with sample cases to ensure
correctness.
Determine the time complexity (e.g., O(n), O(n 2)) by analyzing loops, recursive
calls, etc.
+----------------------+
| Understand Problem |
| (Inputs/Outputs) |
+----------+-----------+
|
v
+----------+-----------+
| Decide Approach |
| (Data Structures, |
| Techniques) |
+----------+-----------+
|
v
+----------+-----------+
| Design Algorithm |
| (Pseudocode/Flowchart)|
+----------+-----------+
|
v
+----------+-----------+
| Prove Correctness |
| (Verification) |
+----------+-----------+
|
v
+----------+-----------+
| Analyze Complexity |
| (Time/Space) |
+----------+-----------+
|
v
+----------+-----------+
| Implement Algorithm |
| (Code Translation) |
+----------+-----------+
|
v
+----------+-----------+
| Test & Refine |
| (Optimization) |
+-----------------------+
Efficiency Metrics
Time Complexity: Measures how execution time grows with input size.
This structured process ensures that algorithms are not only functional but also optimized
for performance and scalability.
⁂
Pseudocode:
def bubble_sort(arr):
n = len(arr)
for i in range(n-1): # Outer loop for passes
swapped = False
for j in range(n-i-1): # Inner loop for comparisons
if arr[j] > arr[j+1]: # Compare adjacent elements
arr[j], arr[j+1] = arr[j+1], arr[j] # Swap if needed
swapped = True
if not swapped: # If no swaps occurred, array is sorted
break
1. Compares adjacent elements and swaps them if they are in the wrong order.
2. After each pass, the largest unsorted element "bubbles" to its correct position.
Time Complexity:
Space Complexity:
Sequential sort is typically referred to as Selection Sort, where the smallest element is
repeatedly selected and placed at its correct position.
Pseudocode:
def selection_sort(arr):
n = len(arr)
for i in range(n-1): # Outer loop for passes
min_idx = i
for j in range(i+1, n): # Inner loop to find the smallest element
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i] # Swap smallest element with current index
2. Finds the smallest element in the unsorted portion and places it in the sorted
portion.
Time Complexity:
Space Complexity:
Worst Case 2 2
O(n ) O(n )
Average Case 2 2
O(n ) O(n )
Space Complexity O(1) O(1)
1. Big-O Notation (O ):
2. Big-Omega Notation (Ω ):
3. Big-Theta Notation (Θ ):
o Represents a tight bound, meaning it describes both the upper and lower
bounds.
Efficiency classes categorize algorithms based on their growth rates, which determine
their performance as input size increases. Common classes include:
⁂
1. https://www.geeksforgeeks.org/types-of-asymptotic-notations-in-complexity-analysis-of-
algorithms/
2. https://www.engati.com/glossary/asymptotic-notation
3. https://dev.to/abhishekchandra2522k/asymptotic-notations-14nn