Topic:Algorithms and Data Structures

From Wikiversity

Jump to: navigation, search

Algorithm -- briefly, is a step-by-step instruction that as a whole complete a process. Usually this process solves problems such as sorting , finding data, and many other problems. It can be thought of as a recipe, with each ingredient contributing to the whole food, thus solving your problem of hunger :)

Examples of Algorithm

Linear searching algorithm : A linear searching has O(n) time complexity. Lets step through a process of creating an algorithm for this problem.

Step # 1 : We need a way to check through every element in the data. This can be done through a loop.

          A loop (ex, for loop , while loop) enable the programmer to run it block of code multiple of time

Step # 2 : Each iteration we need to check if the value that we are searching for matches. If so then return that index.

Step # 3 : handle error, like what happens if the value we are searching for is not there ?

Since we have the step-by-step process written. We are able to effectively write an algorithm that follows the process above.

This function is written in C++.

[code].

int serachLinearly( int * Array, int MaxSize, int searchValue) {

  //Step # 1 - loop through every element if needed
  for(int i = 0; i < MaxSize; i++)
      {
          if( Array[i] == searchValue ) // Step # 2 -- search value
              return i;
      } 
      
   return -1; //Step # 3 -- handle error if value not found

} [/code].

So as you can see there is a step-by-step process in creating an algorithm. The above code could be written in template version but I choose not to for simplicity sake.



Data Structure Article coming soon.