ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-112Algorithm Efficiency S…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-112

    Programming Fundamentals

    Progress0 / 39 done
    Topics39
    1Introduction to Problem Solving, Algorithms, Programming, and C Language2Problem Solving, a brief review of Von-Neumann Architecture3The C Programming Language, Pseudo-code, Concept of Variable4Data types in Pseudo-code, The C Standard Library and Open Source5Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6Concept of Integer division, Flowchart and its notations7Typical C Program Development Environment, Role of Compiler and Linker8Test Driving C Application
    9A Simple C Program: Printing Text, Adding Two Integer10Memory Concepts, Arithmetic in C, Operators
    11Equality and Relational Operators
    12The if, if...else, while Nested Control Statements
    13for, switch, do...while, break, continue, Logical Operators
    14Modularizing Program in C, Math Library Functions15Function Definitions and Prototypes, Function-Call Stack and Stack Frames16Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration
    18Defining Arrays, Character Arrays, Static and Automatic Local Arrays19Passing Arrays to Function, Sorting and Searching Arrays20Multidimensional and Variable Length Arrays
    21Pointer Definitions and Initialization, Pointer Operators22Passing Arguments to Function by Reference, Using the const and sizeof Operator23Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24Function Pointers
    25Strings and Characters, Character Handling Library26String Functions, Library Functions
    27Streams, Formatted Output with printf, Formatted Input with scanf
    28Defining Structures, Accessing Structure Member, Structures and Functions29typedef, Unions
    30Bitwise Operators, Bit Fields, Enumeration Constants
    31Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File
    32#include, #define, Conditional Compilation, #error and #pragma33# and ## Operators, Predefined Symbolic Constants, Assertions
    34Variable Length Argument List, Using Command Line Arguments35Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36Suffixes for Integer and Floating-Point Literals, Signal Handling
    37calloc and realloc, goto
    38Self-Referential Structures, Linked Lists39Efficiency of Algorithms, Selection and Insertion Sort
    6 min read
    Topic 39 of 39
    Topic Study Note Illustration
    39

    Efficiency of Algorithms, Selection and Insertion Sort

    In computer science, algorithm efficiency is a key factor in determining how well an algorithm performs, particularly with large datasets. The efficiency of an algorithm can be...

    Efficiency of Algorithms, Selection and Insertion Sort

    In computer science, algorithm efficiency is a key factor in determining how well an algorithm performs, particularly with large datasets. The efficiency of an algorithm can be measured in terms of its time complexity and space complexity. Time complexity tells us how the runtime of an algorithm increases as the input size grows, while space complexity refers to the amount of memory an algorithm requires.

    When it comes to sorting algorithms, Selection Sort and Insertion Sort are among the simplest sorting algorithms. Both have their advantages and disadvantages, particularly when considering efficiency.


    1. Efficiency of Algorithms

    The efficiency of an algorithm is typically evaluated using Big O notation, which classifies algorithms based on their worst-case, best-case, and average-case performance in terms of input size (n).

    • Time Complexity: Refers to how the time taken by an algorithm increases as the size of the input grows. It's usually expressed as a function of n (where n is the size of the input data).

    • Space Complexity: Refers to the amount of memory used by an algorithm as a function of the input size.

    Some common time complexities for sorting algorithms are:

    • O(1): Constant time complexity, meaning the algorithm’s runtime doesn’t change with the size of the input.
    • O(n): Linear time complexity, meaning the runtime grows linearly with the input size.
    • O(n²): Quadratic time complexity, meaning the runtime grows proportionally to the square of the input size.

    For sorting algorithms like Selection Sort and Insertion Sort, we primarily focus on time complexity.


    2. Selection Sort

    Selection Sort is an in-place comparison-based sorting algorithm. It works by repeatedly selecting the smallest (or largest, depending on the sorting order) element from the unsorted portion of the array and swapping it with the first unsorted element.

    Algorithm Explanation:

    1. Start with the first element of the array.
    2. Find the smallest element in the unsorted portion of the array.
    3. Swap this smallest element with the first unsorted element.
    4. Repeat the process for the remaining unsorted portion of the array.

    Selection Sort Pseudocode:

    for i = 0 to n-1
        min_index = i
        for j = i+1 to n
            if array[j] < array[min_index]
                min_index = j
        swap array[i] with array[min_index]
    

    Time Complexity:

    • Best, Average, and Worst Case:
      • O(n²), where n is the number of elements in the array.
      • This is because for each element, we need to scan the remaining unsorted elements to find the minimum element, resulting in a nested loop.

    Space Complexity:

    • O(1): Selection Sort is an in-place sorting algorithm, meaning it requires only a constant amount of extra memory for swapping elements.

    Why Selection Sort?

    • Advantages:
      • Simple to implement.
      • Does not require extra memory allocation (in-place sort).
    • Disadvantages:
      • Inefficient for large datasets due to its O(n²) time complexity.
      • Performs poorly on almost sorted data as well.

    3. Insertion Sort

    Insertion Sort is another in-place, comparison-based sorting algorithm. It works by building a sorted portion of the array one element at a time. It repeatedly takes the next unsorted element and inserts it into the correct position in the sorted portion of the array.

    Algorithm Explanation:

    1. Start with the second element of the array (assuming the first element is already sorted).
    2. Compare this element with the previous elements.
    3. Shift all larger elements one position to the right.
    4. Insert the element into its correct position.
    5. Repeat for the rest of the array.

    Insertion Sort Pseudocode:

    for i = 1 to n-1
        key = array[i]
        j = i - 1
        while j >= 0 and array[j] > key
            array[j + 1] = array[j]
            j = j - 1
        array[j + 1] = key
    

    Time Complexity:

    • Best Case:

      • O(n): This happens when the array is already sorted. Only a single comparison per element is needed.
    • Average and Worst Case:

      • O(n²): In the worst case, when the array is sorted in reverse order, each element must be compared with every other element before it can be inserted in its correct position.

    Space Complexity:

    • O(1): Like Selection Sort, Insertion Sort is an in-place algorithm that only requires a constant amount of extra space.

    Why Insertion Sort?

    • Advantages:

      • Efficient for small or nearly sorted arrays. It performs well if the data is almost sorted (best case O(n)).
      • Simple and intuitive to implement.
      • Adaptive: The time complexity improves when the array is already partially sorted.
    • Disadvantages:

      • Inefficient for large datasets, since its time complexity is O(n²) in the average and worst cases.
      • Performs poorly on large, random datasets.

    Comparison: Selection Sort vs. Insertion Sort

    Criterion Selection Sort Insertion Sort
    Time Complexity (Best Case) O(n²) O(n)
    Time Complexity (Worst/Average Case) O(n²) O(n²)
    Space Complexity O(1) O(1)
    Stability Unstable (does not preserve relative order of equal elements) Stable (preserves relative order of equal elements)
    Performance on Small/Almost Sorted Arrays Performs equally for all cases Performs well on small or partially sorted arrays
    Usage Used in situations where memory is highly constrained and performance is not critical Useful for small arrays or nearly sorted data

    Summary:

    • Selection Sort is easy to implement but inefficient for larger datasets, as its time complexity is always O(n²), regardless of the input data's initial order. It does not perform well on nearly sorted data and is not stable.
    • Insertion Sort is also O(n²) in the worst case, but it performs much better on nearly sorted data or smaller datasets due to its O(n) best-case performance. It is stable and adaptive but also not suitable for large datasets.

    Which to Use?

    • For small or nearly sorted datasets, Insertion Sort tends to perform better due to its efficiency in these cases.
    • Selection Sort, while simple and space-efficient, is generally slower in most practical scenarios due to its O(n²) time complexity and is less adaptive than Insertion Sort.

    For larger datasets, more efficient algorithms like Merge Sort, Quick Sort, or Heap Sort should be considered, as they offer better performance with time complexities of O(n log n).

    ← Previous topic 38
    Advance Topics: Self-Referential Structures, Linked Lists

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    1.0kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals