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 PortalCOMP1112Introduction To Algori…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP1112

    Programming Fundamentals

    Progress0 / 19 done
    Topics19
    1Introduction to Problem Solving2Von-Neumann Architecture3Introduction to Programming4Role of Compiler and Linker5Introduction to Algorithms6Basic Data Types and Variables7Input/Output Constructs8Arithmetic, Comparison and Logical Operators9Conditional Statements and Execution Flow10Repetitive Statements and Execution Flow11Lists and Memory Organization12Multi-dimensional Lists13Introduction to Modular Programming14Function Definition and Calling15Stack Rolling and Unrolling16Strings and String Operations17Pointers/References18Static and Dynamic Memory Allocation19File I/O Operations
    5 min read
    Topic 5 of 19
    Topic Study Note Illustration
    05

    Introduction to Algorithms

    An algorithm is a well-defined procedure or set of rules for solving a specific problem or accomplishing a particular task. In the context of computer science and...

    Introduction to Algorithms

    An algorithm is a well-defined procedure or set of rules for solving a specific problem or accomplishing a particular task. In the context of computer science and programming, algorithms are fundamental for creating efficient and effective software. Here’s a detailed overview of what algorithms are, their characteristics, types, and importance.

    1. What is an Algorithm?

    An algorithm is essentially a step-by-step guide that outlines how to perform a task or solve a problem. It can be represented in various forms, including pseudocode, flowcharts, or actual code in a programming language.

    Key Characteristics of Algorithms:

    • Unambiguous: Each step should be clearly defined, leaving no room for interpretation.
    • Well-Defined Inputs and Outputs: An algorithm should specify what inputs it requires and what outputs it will produce.
    • Finiteness: An algorithm must terminate after a finite number of steps.
    • Effectiveness: Each step of the algorithm must be sufficiently basic that it can be performed, in principle, by a person using a pencil and paper.

    2. Importance of Algorithms

    • Efficiency: Well-designed algorithms can significantly improve the performance of programs, affecting both time complexity (how fast a solution is found) and space complexity (how much memory is used).
    • Problem Solving: Algorithms provide a systematic approach to problem-solving, enabling programmers to tackle complex issues.
    • Reusability: Once developed, algorithms can be reused in different programs or systems, promoting code efficiency.
    • Foundation of Computer Science: Understanding algorithms is essential for grasping more complex topics in computer science, such as data structures, software development, and artificial intelligence.

    3. Types of Algorithms

    Algorithms can be categorized in various ways based on their functionality and application. Here are some common types:

    • Sorting Algorithms: These algorithms arrange elements in a specified order (e.g., ascending or descending).

      • Examples: Quick Sort, Merge Sort, Bubble Sort.
    • Searching Algorithms: Used to find an element within a data structure.

      • Examples: Linear Search, Binary Search.
    • Recursive Algorithms: These solve a problem by solving smaller instances of the same problem (i.e., calling themselves).

      • Example: Factorial calculation, Fibonacci series.
    • Dynamic Programming: This approach solves complex problems by breaking them down into simpler subproblems and storing the results to avoid redundant computations.

      • Example: Knapsack problem, longest common subsequence.
    • Greedy Algorithms: These make the locally optimal choice at each stage with the hope of finding a global optimum.

      • Example: Dijkstra's algorithm for shortest paths.
    • Backtracking Algorithms: These build up a solution incrementally, abandoning paths as soon as it determines that they cannot lead to a valid solution.

      • Example: Solving puzzles like Sudoku.

    4. Representing Algorithms

    Algorithms can be represented in several forms:

    • Pseudocode: A high-level description of an algorithm using a mix of natural language and programming constructs. It’s used for designing algorithms without worrying about syntax.

      Example of a simple algorithm to find the maximum number in a list:

      FUNCTION findMax(list)
          max = list[0]
          FOR each item IN list
              IF item > max THEN
                  max = item
          RETURN max
      
    • Flowcharts: Visual representations of algorithms that use shapes to denote different types of actions or steps.

    • Programming Languages: Actual code written in languages like C++, Python, or Java that implements the algorithm.

    5. An Example: Bubble Sort Algorithm

    Sorting algorithm
    Visual ReferenceSorting Algorithm
    Wikipedia

    Description: Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

    Pseudocode:

    FUNCTION bubbleSort(arr)
        n = LENGTH(arr)
        REPEAT
            swapped = FALSE
            FOR i FROM 1 TO n - 1
                IF arr[i - 1] > arr[i] THEN
                    SWAP(arr[i - 1], arr[i])
                    swapped = TRUE
            END FOR
        UNTIL NOT swapped
    END FUNCTION
    

    C++ Implementation:

    #include <iostream>
    using namespace std;
    
    void bubbleSort(int arr[], int n) {
        bool swapped;
        do {
            swapped = false;
            for (int i = 0; i < n - 1; i++) {
                if (arr[i] > arr[i + 1]) {
                    swap(arr[i], arr[i + 1]);
                    swapped = true;
                }
            }
        } while (swapped);
    }
    
    int main() {
        int arr[] = {64, 34, 25, 12, 22, 11, 90};
        int n = sizeof(arr)/sizeof(arr[0]);
        bubbleSort(arr, n);
        cout << "Sorted array: ";
        for (int i = 0; i < n; i++)
            cout << arr[i] << " ";
        cout << endl;
        return 0;
    }
    

    Conclusion

    Algorithms are essential for effective programming and problem-solving. They provide structured methods for processing data and performing tasks efficiently. By understanding different types of algorithms and their representations, programmers can choose the best approach for their specific needs, ultimately leading to more efficient and robust software solutions.

    ← Previous topic 4
    Role of Compiler and Linker
    Next topic 6 →
    Basic Data Types and Variables

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    5mremaining
    798words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals