Search code examples
algorithmpseudocode

What's the difference between pseudo code and Algorithm?


What's the difference between pseudo code and Algorithm? Can you give me an example? I tried to search online but I'm still confused about the algorithm. Pseudo code is written in words, I get that. But I fail understand the algorithm. Is there a difference between alogrithm and code?


Solution

  • An algorithm is just a sequence of steps with no fixed representation. It can be described in a high-level description, pseudocode or code in any language.

    More generically, any program written in any language, any pseudocode or really any concrete sequence of steps can be considered an algorithm.

    There's no fixed format for pseudocode - it can look very similar to any language or combination of languages, or it can simply be a natural language description of the algorithm.


    As an example, insertion sort is an algorithm.

    A high-level description is:

    Keep a sorted list which starts off empty. For each element in the original list, insert it into the correct position in the sorted list. Return the sorted list.

    A pseudo-code representation (as per Wikipedia) is:

    for i ← 1 to length(A)
        x ← A[i]
        j ← i
        while j > 0 and A[j-1] > x
            A[j] ← A[j-1]
            j ← j - 1
        A[j] ← x
    

    And then you can also write it in Java, C++, C or just about any other language.

    These are all representations of the insertion sort algorithm.

    * - There are a few variants of insertion sort, and these can all, in their own right, be considered algorithms, and there's some ambiguity in the above high-level description (which can lead to different variants), but let's ignore these aspects for now.