Posts tagged Algorithms

Selection Sort

1

Selection sort is a sorting algorithm

we compare the value in the first position with the value in the second position if the value in the second position is smaller then we swap the two values, otherwise we do nothing, assuring that the first position holds the smaller of both.
then we compare the value in the first position again with the value in the third position and make the same with the fourth and fifth until the last position in the array
after finishing these steps we’d have the smallest value in the first position
we will repeat these steps with the second position comparing it with the third and fourth until the end which would result that the second smallest value in the second position
we will repeat this again and again with every position until the end.
(more…)

Binary Search

1


The binary Search is a very efficient search algorithm applies only on ordered arrays

the main concept of binary search is to break the whole array into two halves and then check the value in the middle, if we’re searching for a value bigger than the value in the middle so it’d be at the right part otherwise it’d be in the left part
we will repeat this step in the resulted part by dividing it into two parts and vice versa.
we should make a stop point and it’d be in two cases

  1. if we ended with no division available in our array then it’ll we will notify that we didn’t find the value
  2. if we found the value we will return with the current index

(more…)

Linear Search

0

The linear Search is the simplest search algorithm
it’s simply checks for every element in an array one by one in order

we can implement this algorithm in a function that takes 3 arguments

  1. A pointer to the array
  2. The size of the array
  3. The value we want to search for

the return value of the function ‘d be the index (order) of the element in that array or -1 if it’s not exist
(more…)

Go to Top