Page 415 - computer science (868) class 11
P. 415

Step 9:  Divide copy by 10 and store its quotient in copy.
                 Step 10: Initialise copy = sum.

                 Step 11: If copy = 1 then go to Step 12, else go to Step 13.
                 Step 12: Display num “is a Happy number”, go to Step 14.

                 Step 13: Display num “is not a Happy number”.
                 Step 14: Stop.


                 13.5.3 Searching Algorithms
                 Problem 10: Write an algorithm to search any number from an array using the LINEAR SEARCH technique.



                        Note:  Linear search program is already covered in the Array chapter of this book.


                                                                     Target                      Unsorted List
                 Step 1:  Start.
                                                                      15             13    48    7    4    15   25   11
                 Step 2:   Initialise  loop  variable  i  to  0  and  index   1st Comparision
                         position pos to -1.
                                                                      15             13    48    7    4    15   25   11
                 Step 3:  Repeat Step 4 to Step 6 while i < n.              15≠13
                 Step 4:   If A[i] = searchvalue then go to Step 5,   2nd Comparision
                         else go to Step 6.
                                                                      15             13    48    7    4    15   25   11
                 Step 5:  Initialise pos to i, and go to Step 7.            15≠48
                                                                3rd Comparision
                 Step 6:  Increment i by 1.
                                                                      15             13    48    7    4    15   25   11
                 Step 7:   If pos != -1 then go to Step 8, else go to        15≠7
                         Step 9.                                 4th Comparison

                 Step 8:   Display  searchvalue  “found  at  index”   15             13    48    7    4    15   25   11
                         pos, go to Step 10.                                 15≠4
                                                                 5th Comparison
                 Step 9:  Display searchvalue “not found”.
                                                                      15             13    48    7    4    15   25   11
                 Step 10: Stop.
                                                                            15=15
                 Problem 11: Write an algorithm to search any number from an array using the BINARY SEARCH technique.



                        Note:  Binary search program is already covered in the Array chapter of this book.


                 This algorithm is written assuming array a[] is sorted in ascending order:

                 Step 1:  Start.
                 Step 2:  Initialise begin to 0, end to array length-1 and pos = -1.

                 Step 3:  Repeat Step 3 to Step 8 while begin <= end.
                 Step 4:  Calculate mid = (begin + end)/2.
                 Step 5:  If a[mid] = searchvalue then initialise pos = mid, go to Step 9.

                 Step 7:  If a[mid] > searchvalue then initialise end = mid - 1.




                                                                                                                       413
                                                                            Implementation of Algorithms to Solve Problems  413
   410   411   412   413   414   415   416   417   418   419   420