ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    COMP1112
    Progress0 / 19 topics
    Topics
    1. Introduction to Problem Solving2. Von-Neumann Architecture3. Introduction to Programming4. Role of Compiler and Linker5. Introduction to Algorithms6. Basic Data Types and Variables7. Input/Output Constructs8. Arithmetic, Comparison and Logical Operators9. Conditional Statements and Execution Flow10. Repetitive Statements and Execution Flow11. Lists and Memory Organization12. Multi-dimensional Lists13. Introduction to Modular Programming14. Function Definition and Calling15. Stack Rolling and Unrolling16. Strings and String Operations17. Pointers/References18. Static and Dynamic Memory Allocation19. File I/O Operations
    COMP1112›Introduction to Algorithms
    Programming FundamentalsTopic 5 of 19

    Introduction to Algorithms

    5 minread
    793words
    Beginnerlevel

    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

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time5 min
      Word count793
      Code examples0
      DifficultyBeginner