All pages
Powered by GitBook
1 of 3

Loading...

Loading...

Loading...

Algorithms

In the context of computer programming, an algorithm is a set of well-defined instructions or a step-by-step procedure designed to perform a specific task or solve a particular problem. You can think of it as an algorithm in computer programming is to computer programs as recipes are to cooking good food. Here's a deeper look into algorithms in computer programming including the names and quick descriptions of some of the most common, existing algorithms for common tasks.

  1. Definition: An algorithm is a sequence of computational steps that transform the input into the output.

  2. Characteristics:

    • Correctness: An algorithm should produce the correct outputs for given inputs.

    • Efficiency: It should use resources (like time and memory) optimally.

    • Deterministic: Given a particular input, the algorithm should always produce the same output.

    • Finiteness: The algorithm should have a finite number of steps.

    • Generalization: It should be applicable for a broad set of input values.

  3. Components:

    • Input: What goes into the algorithm, can be zero or more inputs.

    • Output: The result produced by the algorithm, typically at least one output.

  4. Types:

    • Sorting Algorithms Sorting algorithms are fundamental in computer science, used for arranging elements of a list or an array in a certain order, typically either ascending or descending. Here are some of the most common sorting algorithms:

      1. Bubble Sort:

In summary, algorithms are the building blocks of computer programs, providing a methodical solution process for complex problems. The study of algorithms is a critical part of computer science and software engineering. KEY TO REMEMBER! Design, plan and problem solve using an existing algorithm (or devise your own) BEFORE you start coding!

Program design

As is the case when creating any artefact and especially an IT artefact such as a computer program, a mobile application or a web page you should always start with some design of the artefact. In addition, you need to problem solve that is solve the problem at hand programmatically. In order to do that two common methods are algorithms and/or pseducode that you use to describe how to solve a problem.

Control Structures
: These include conditionals (like if-else statements) and loops (like for and while loops) that guide the flow of the algorithm.

A simple comparison-based algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

  • Best for small datasets or as a teaching tool.

  • Selection Sort:

    • This algorithm divides the input list into two parts: a sorted sublist of items which is built up from left to right and a sublist of the remaining unsorted items. It repeatedly selects the smallest (or largest, depending on the order) element from the unsorted sublist and moves it to the end of the sorted sublist.

    • Simple but not suitable for large datasets.

  • Insertion Sort:

    • Builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms like quicksort, heapsort, or merge sort.

    • Efficient for small data sets and mostly sorted arrays.

  • Merge Sort:

    • A divide and conquer algorithm that divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves.

    • Efficient for large datasets, and has a consistent running time, but requires additional memory space.

  • Quick Sort:

    • Another divide and conquer algorithm. It picks an element as a pivot and partitions the given array around the picked pivot.

    • Generally faster than merge sort in practice, though it has a worst-case time complexity.

  • Heap Sort:

    • Based on a binary heap data structure. It’s similar to selection sort where we first find the maximum element and place it at the end.

    • Efficient for large data sets but not as fast in practice as quicksort.

  • Counting Sort:

    • An integer sorting algorithm that operates by counting the number of objects that have each distinct key value.

    • Efficient for sorting a small range of integer values; not suitable for large range of values.

  • Radix Sort:

    • A non-comparative integer sorting algorithm that sorts data with integer keys by grouping the keys by individual digits that share the same significant position and value.

    • Efficient for large datasets where the range of values isn’t too large.

  • Bucket Sort:

    • Works by distributing the elements of an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm or by recursively applying the bucket sort.

    • Useful when the input is uniformly distributed over a range.

  • Shell Sort:

    • A variation of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, taking every hth element produces a sorted list.

    • More efficient than simple insertion sort.

  • Search Algorithms:

  • The main search algorithms in computer programming can be broadly classified into two categories: Linear Search Algorithms and Non-Linear Search Algorithms. Here's a brief overview of each:

    1. Linear Search Algorithms

    These algorithms work on lists or arrays and search for an element by checking each element sequentially.

    • Linear Search: The simplest search algorithm that checks every element in the list until the desired element is found or the list ends.

    • Binary Search: A more efficient algorithm than linear search, but it requires the list to be sorted. It works by repeatedly dividing in half the portion of the list that could contain the item, until you've narrowed down the possible locations to just one.

    2. Non-Linear Search Algorithms

    These are used for more complex data structures like trees and graphs.

    • Depth-First Search (DFS): Used in tree and graph structures, it explores as far as possible along each branch before backtracking.

    • Breadth-First Search (BFS): Also used in trees and graphs, it explores all of the neighbor nodes at the present depth before moving on to the nodes at the next depth level.

    • Jump Search: Similar to binary search, but in arrays, it works by creating a 'block' and trying to find the element in that block. This requires the array to be sorted.

    Specialized Search Algorithms

    • Hashing: Utilizes a hash table to search for items. It's not a search algorithm per se but a technique that enables fast data retrieval.

    • Exponential Search: Combines the ideas of binary search and jump search to find a range where the search key may exist and then performs a binary search in this range.

  • Graph Algorithms: Like Dijkstra's or A* algorithm, used for finding the shortest paths in graphs.

  • Dynamic Programming Algorithms: Used for solving complex problems by breaking them down into simpler subproblems.

  • Machine Learning Algorithms: Like neural networks, decision trees, used in AI for data analysis and pattern recognition.

  • Pseudocode

    Pseudocode in computer programming is a simplified, half-way language that helps developers and programmers to plan and communicate algorithms without the constraints of formal programming language syntax. It's not executable code but a textual representation of an algorithm, often resembling a combination of several programming languages. Pseudocode is primarily used for algorithm development and explanation.

    Key Characteristics of Pseudocode:

    • Language Agnostic: It doesn't adhere to the syntax of any specific programming language.

    • Focus on Logic: Prioritizes the logic and steps of the algorithm rather than syntax and programming conventions.

    • Readable: Designed to be easily understood by people familiar with programming concepts.

    • Structured: Often follows a structure similar to high-level programming languages (e.g., use of loops, conditionals).

    A Simple Example of Pseudocode:

    Let's consider a simple algorithm for finding the largest number in a list:

    In this pseudocode:

    • The algorithm is outlined in a step-by-step, readable manner.

    • It uses common programming constructs like a loop (For each) and a conditional statement (If).

    • It avoids specifics like variable declarations or error handling, which are typical in real code.

    Interpolation Search: A variant of binary search for uniformly distributed lists, which estimates the position of the search item based on its value.

    It's written in a way that can be easily translated into any programming language.

    Algorithm: Find the Largest Number in a List
    Input: A list of numbers
    Output: The largest number in the list
    
    Begin
        Set largest to the first number in the list
    
        For each number in the list
            If the number is greater than largest
                Set largest to this number
            End If
        End For
    
        Return largest
    End