Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Searching Algorithms

 There are two (2) types of Search Algorithms in c++ namely;


 Linear Search.

&

 Binary Search.

LINEAR SEARCH ALGORITHM

There are two conditions that satisfies the linear search algorithm, these are;
1. It is either you have found what you are looking for or.
2. You have exhausted the chances of the search.
Steps to follow in this algorithm
 Start from the left most element of the array one by one, compare the values
that you are searching for with the element of the array.
 If the value you are looking for matches with the element store the position of
the element.
 Otherwise continue searching until either you find the value or you have
exhausted all the elements.

T= 1 5 7 10 18 4 21 11 0 8
Source code
While (i<=5) {
If (value==T[i]) {
Posi=i;
}
i=i+1;
}
If you want to stop the search then the following code will do.

While (i<=5) {
If ((value==T[i]) && (Posi==-1)) {
Posi=i;
}
i=i+1;
}
BINARY SEARCH ALGORITHM

 For the Binary search algorithm to work properly first sort the array in
order.
 That is Ascending and Descending order.
 Identify the middle most position.
 Check if the value you are looking for is at the middle.

Formula to use for the binary search

Min, Max and Middle

Posi= 0 1 2 3 4 5 6 7 8 9

T= 1 3 7 13 14 21 31 49 200 2509
min + 𝑚𝑎𝑥
Middle=
2

0+4
Middle=
2
4
Middle=
2
Middle= 4
min + 𝑚𝑎𝑥
(2.) Middle=
2

0+3
Middle=
2

3
Middle=
2

Middle= 1
min + 𝑚𝑎𝑥
(3.) Middle=
2

2+3
Middle=
2

5
Middle=
2

Middle= 2

NB: there is a Modulus (%) operation that is taking place on the above calculations.
The Modulus operator (%) is an operator that gives the remainder of an integer division

e.g.

1. 7%3 =1 (because 3 can go into 7, 2 times leaving a remainder of 1).


2. -10%8 = -2
3. -10%-8 =-2
 This operator can be used only with integers.
 Can be used to extract digits from numbers.
6543%10 =3
6789%10 =9
 Integer Division gives a zero (quotient) when the numerator is smaller than the
denominator
3
e.g. =0.
4
 Modulus division gives us the gives us the numerator (same number) if the numerator is
smaller than the denominator.
e.g. 3%4 =3. (As the remainder and zero as the integer.)

Source code

While ((min<max) && (found==false)) {


min + 𝑚𝑎𝑥
Middle= ;
2

If(T[mid]==Val) {

found==true;

else {

if(Val>T[mid]) {

min=mid+1;

} else {

Max=mid-1;

}
Sorting Algorithms
 There are basically about four (4) Sorting Algorithms in programming.
1. Selection Sort
 With selection sort, you need to identify the smallest element

Position= 0 1 2 3 4
5 0 -2 7 2
T=

 Arrange the smallest element in the first position and proceed to another
smallest element and store it in the second place/position and so on and so
forth.
(1.) Min= T [0]  Min=5
Is 0<5? => Yes
Then, Min=> T [1] 0
Posi =1

(2.) T [2] < Min?


Is -2<0 => Yes
Min= T [2]
Posi= T [2]

(3.) T [3] < Min?


7<-2? => No
(4.) T [4] < Min?
(5.) 2 < -2? => No

The algorithm first looks for the smallest or largest element in the array and swaps it with the
element in the first position. Then it looks for the second smallest or largest element then swaps
it with the element in the second position. This will keep on doing so until the entire array is
sorted. It is called the selection sort because it repeatedly select the next smallest element and
swaps it into the right place.

Swapping of element

Temp = T [0];

T [0] = T [pos];

T [pos] = Temp;

So, to summarize this information, here are the steps and a piece of code on how to go about
the selection sort algorithm.
Steps of the algorithm

1. Start from the first element, search for the smallest element in the array and swap it
with element in the first position.
2. Move on to the second element or position and look for the smallest element present n
the sub-array starting from index/position [1] until the last index. Once identified swap
the smallest element in the sub-array with the element in the second position of the
entire array.
3. Repeat this process until the entire array is completely sorted.
Swapping Source Code
int T[5];
int i=0;
While (i<4) {
Min = T[i];
For (int j=0; j<=4; j++) {
If (T[j] <Min) {
Min= T[j];
Posi=j
}
}
Temp = T[i];
T[i] =T [Posi];
T [Posi] = Temp;
i=i+1;
}

2. Bubble Sort
 The Bubble sort compares the elements one by one and sorts them based on
their values after the comparison.
 The bubble sort starts by comparing the first element of the array with the
second element. If the second element is greater than the first element it
swaps them and moves on to compare the third and fourth elements.
 If we have N elements in the array, this process must repeat “N-1”times
 It is called the bubble sort because with every complete iteration with
largest element int the given array moves the bubbles upwards towards the
last position

5 0 -2 7 2

So, 7 swaps with -2


5 0 7 -2 2

-2 with 2.

5 0 7 2 -2

Then 0 with 7

5 7 0 2 -2

2 with 0

5 7 2 0 -2

And finally 5 with 7

7 5 2 0 -2

Until the whole array is sorted (in this case it is descending order)

Steps of the algorithm

1. Starting with the first element compare the current with the next element in
the array.
2. If the current element is greater than the next swap them and move on to
the next elements.
3. If the current element is less than the next element move on to the next
element and repeat the two previous steps.
Swapping Source Code
While (i < 4) {
For (j=0; j<4; j++) {
If (T [j]> T [j+1]) {
Temp = T [j];
T [j] = T [j+1];
T [j+1] =Temp;
}
}
}
3. Insertion Sort
 The Insertion sort tests the elements with each other one by one and sorts
them.
 The insertion sort results in an ascending order way of arrangement of
elements.
 If the element in the first position is smaller move to the next element and
test it with the current one. If not smaller swap the elements. Repeat this
process until the array is sorted.

We arrange in ascending order


0 4 -1 7 8 9

Test o and -1 to see if zero is greater than -1


4 0 -1 7 8 9

Since -1 is less than zero we swap them


-1 4 0 7 8 9

Another example (detailed)


4 0 -1 17 28 9

T [1] < T [0]?


0 < 4 yes, we swap.

4 moves to position T [1]


0 4 -1 17 28 9

Move to the next elements


T [2] < T [0]?
-1 <0 yes, we swap.

0 swaps with -1
-1 4 0 17 28 9

So, this process will continue to do the swapping until the entire array
is sorted and arranged in ascending order.
Steps of the algorithm

1. Start with the first element test it with the next element and see is any is less than the
other and swap them.
2. It doesn’t matter the position of the element but keep on testing and swapping them
3. Repeat these two steps until the whole array is arranged in ascending order and sorted.

Swapping Source Code

For (int i=1; i<6; i++) {

If (array [j] < array [i]) {

Int temp = array [i];

Array [i] = array [j];

Array [j] = temp

You might also like