Download as pdf
Download as pdf
You are on page 1of 5
Analgorithm isa stepwise solution toa given problem, Rules: }) Algorithms are generalized solutions to problems that can be implemented in any language. Algorithms do not follow syntax of any language. ©) Algorithms use mathematical symbols instead of language specific operators. ®) Algorithms consist of brief steps avoiding irrelevant explanations. » ‘Use remarks / comments wherever necessary to clarify the step. Namber Problems: Algorithm 1: To find the roots of a quuadratie equation Function: root Steps: Begin Inputa,b,e //a, band care coefficients Vb-dxaxe O then do steps 5 to 7 else goto step 8 5 b/Qxa) 6. Print “Equal roots”, r 7. Gotostep 14 8 9. veg Ifdise> 0 then do steps 9 to 12 else goto step 13 Set rl =(-b + ¥ disc) /(2xa) 10. Setrt ~ V disc) / (2x a) 1, Print“Distinet roots”. r1,12 12. Goto step 14 13. Ifdise<(0 then print “Imaginary roots” 14. End ‘Algorithm 2: To test a prime number Function: isPrime Scanned with CamScanner ‘Algorithm 7: To find the maximum value ina list of N integers Function: maxdrr Steps: vey om 9. 10. u Begin InputN /* ndenotes the size of list */ Repeat steps 4 to $ fori=1 tod Input A,,, _/* Read valuesin the list */ Seti=it+] LetM=A,,, //Mstores first element ofthe list Repeat steps 8 10 9 for i=2t0 N IfA,,,>Mthenset M=A,,, Se PrintM End ‘Algorithm 8: To find a value ina list using binary search Function: bsearch list is sorted in ascending order /* Aisa listofimtegers */ /* x isthe value to search */ 1/ stores the position of the search: /*F denotes the first position */ /* L denotes the last position */ Repeat steps 9 to 11 while F< L Set M=|(F+L)/2J_ //Getthe mid-tist position IFX=A,,, then found =M :Gotostep 11 // amscanner Let N = size of fist Repeat steps 5 to 13 fori =Nto2 —_ /* Select the maximum element in thelist */ 5 Letmax®A,,, ‘mas hols the greatest list value i 6 Letp=1 //pholds the position of greatest value \ 7. Repeat steps 8 to 9 for} = 201 8. ITA, > max thenmax = Ay, and p=} 9. Set}=j+1 ho. SetA,,) "Aj, I. SetA,= max //Ansert max as the last element 12. Setimi-1 3. End “Algorithm 11; To arrange alist in ascending order using insertion sort technique ‘ution: isort 1. Begin 2. Input} /* Aisalistofintegers */ 3. LetN=sizeoflist 4. Repeat steps 5 to 10 fori=2t0N 5. Lett=A,, /* select value from unordered ps Repeat steps 7108 forj=i-1tolANDt

You might also like