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 :)
Linear searching algorithm : A linear searching has O(n) time complexity. Lets step through a process of creating an algorithm for this problem.
Steps:
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++.
int searchLinearly( 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 }
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 chose not to for simplicity sake.
Wikibooks has a book on the topic of Algorithms. |
Coming soon.