0% found this document useful (0 votes)
14 views10 pages

DocScanner 25-Sept-2024 7-19 PM

.. 11

Uploaded by

sigmamale2428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

DocScanner 25-Sept-2024 7-19 PM

.. 11

Uploaded by

sigmamale2428
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
f 4 i Gar Linear Search Hinear search also called sequent; search is a sequential method for findings Particular value ina list. This method chegg the search element with each clement ig sequence until the desired element is foung or the list is exhausted. In this searchi in algorithm, list need not be ordered. Procedure 1. Traverse the array using for loop 2. In every iteration, compare the targe, search key value with the current value of the list. + If the values match, display the current index and value of the array z + If the values do not match, move on to the next array element. 3, Ifno match is found, display the search element not found. To search the number 25 in the array given below, linear search will go step bj step in a sequential order starting from the first element in the given array if the search) element is found that index is returned! otherwise the search is continued till the las index of the array. In this example number 25 is found at index number 3. target = 200 Output: -1 (not found) procedure for Binary search Start with the middle element: L If the search element is equal to the middle element of the array: ie., the middle value = number of elements in array/2, then return the index of the’ middle element. + Ifnot, then compare the middle element with the search value, + ifthe search element is greater than the number in the middle index, then select the elements to the right side of the middle index, and go to Step-1. * Ifthe search element is less than the ‘Aumber in the middle index, then select theclements to the left side of the middle index, and start with Step-1. 2 When a match is found, display success Message with the index of the element Matched, ee ee 3. Ifno matchis found forallcomparisons, then display unsuccessful message. Binary Search Working principles List of elements in an array must be sorted first for Binary search. ‘The following example describes the step by step operation. of binary search. Consider the following array of elemnts, the array is being sorted so it enables to do the binary search algorithm. Let us assume that the search element is 60 and we need to search the location or index of search element 60 using binary search. First, we find index of middle element of the array by using this formula : id = bows hye! mid = low + (high - low) /2 Here itis, 0 +(9-0)/2=4 (fractional part ignored). So, 4 is the mid value of the array. Now compare the search element with the value stored at mid value location 4, The value stored at location or index 4 is 50, which is not match with search element. As the search value 60 is greater than 50. 5 8 9 v Now we change our low to mid + 1 and find the new mid value again using the formula. low = mid + 1 mid = low + (high - low) /2 Our new midis 7 now. We compare the value stored at location 7 with our target value 60. ‘The value stored at location or index 7 is not a match with search element, rather it is more than what we are looking for. So, the search element must be in the lower part from the current mid value location ‘The search element still not found. Hence, we calculated the mid again by using the formula. high = mid-1 mid = low + (high - low)/2 Now the mid value is 5. Now we compare the value stored at location 5 with our search element. We found that it is a match. [10}{20}[30)[49 SO CII AIS 7123 45 6 7 8 9 We can conclude that the seary element 60 is found at Itoation or index 5 For example if we take the search elemey, as 95, For this value this binary searg algorithm return unsuccessful result. ~S 4.5 Sorting Techniques 4.5.1 Bubble sort algorithm 4 Bubble sort is a simple sortin, algorithm. The algorithm starts at th beginning of the list of values stored in a array. It compares each pair of adjacey elements and swaps them if they are in th unsorted order. This comparison and passei to be continued until no swaps are needed which indicates that the list of values store in an array is sorted. The algorithm is: comparison sort, is named for the wa smaller elements "bubble" to the top of thi list. Although the algorithm is simple, iti too slow and less efficient when comparel to insertion sort and other sorting methods Assume list is an array of n elements The swap function swaps the values of the given array elements. Procedure 1. Start with the first element i.e. index = 0, compare the current element with the next element of the array. 2, Ifthe current element is greater than thé next element of the array, swap them. 3, If the current element is less than tht next or right side of the element, mov! to the next element, Go to Step 1 an! repeat until end of the index is reached ~ 8.5 String Operators ~~ python provide: useful to manipulate string. 5 the following operators for string operations. These operators ' e Joining of two or more strings is-called as | Concatenation, The plus (+) operator is us to concatenate strings in python. Example >>> "welcome" + "Python" ‘welcomePython' (ii) Append (+ =) dding more strings at the end of an existing string is known as append. The ope += is used to append a new string with an existing string. Example >>> strl="Welcome to " >>> strl+="Learn Python" >>> print (str1) Welcome to Learn Python (iii) Repeating (*) The multiplication operator (*).is-used-to display-a string in multiple number of times. Example >>> strl="Welcome " >>> print (str1*4) Welcome Welcome Welcome Welcome (iv) String slicing ain string. A substring can be taken from.the-original-string a d-index-or-subscript-values, Thus. [_ known as sfecing operator. ice first 4 characters from a string, you have to specif it as ot to 5. Because ly the end value as n-1. oAua039M. ({¢:] Ins) wid <<< qa ([z:91:01] ts) wunad <<< a ({e:9 1:01] Las) aud <<< uivay ({91:01J 143) wd <<< woyldd UsvAy 0} PWOITAAA,, = [qs <<< aydurexg | "| St aplays jo anyea yneyap ayy, “Surys a WON} Paaatajad s} 2aIIVIvYD ISIY ay] JAYB PILMIOJ 2AOU OF STa}IBTEY jo Jaquinu ay} 0} Sepa yoiyar ‘apyys ayy se auMsue pang v Ayoads ues nod uonesado Buroys ay} Ua Burays Sursyys way aplays (A) Explsin about constaecter and dutivcton syrap Siteble lexomple » \ Gonstevetor ss the farina femchton gt ads mebieall 4 eumld ulen on objet of 2 dey 4 Pee fo Python, teu a Sfeviel fonction called “ype” at a 4% constructor: Ht vrat begin and ered wire dovble ondascon. This constvete fonction con be diberwd with a without angoment ely ax) ay 28} ‘ c Grernot formal of os 2b puted Clonstuctor Lmckion) Ein B= Goll Tage. De hears 2 . Feel 5 Pn & ilasteae — Gnsbucto jy——= claws name y des Semple: PP Rar deb — ek — Gat, nom): trintl Gnsbivetos off clans Sample « a) SUF num = nuna —ahnstance vawiadk pain Tha? valu as ee 5 = Semple Cre Th above clans “domple” has only & constauctey with Poramte romd os num: Whin fe constuclr of sxcal, Frat le piat alabnenl, print He “Contact of clon arrle 4 Mastience Bee He pupieg vlan te canslectey an anigred * dla des vie th num = nom aad Racy ib part [Ped along with Hs given string * = el Contour of clr chargh Te. vghe “7s 200 Clays variable defined within | rom ba of ehjeds cats with a fee pated be ditey ~ bap cont of con sted de days: i | | Dstrclee a Lb | d Auctor+ leat. % poe etd aed fone Ae «fant & consticker - =O witod | | Exon: pa ifeatat, aboot We — MO pithed as Sample : ; noms dd — init — Gt, ay: Sample -noms= | suf. veg =A paist ("Te objad value a =" SA. ) i , Print ("The velea of able & = Bolen) | 4- dy — (eat): Sample. nure~ 1 pant ("Objet wih val Bde ect eee | ° joka | ste hample Gprins. =MS 5 is = Sarge Gs) = Semple GD “i $1, $3, $3 Mb Ta dh vated pls ced adorably ohen wwe ced | 7 Hh LOM of hey D| Writ a python code % Find th | ismbing ‘« : . . | def compuil _ Jeon'Cx, 4) ? rh xy t Yeoty = x ds @ ‘ . regina = guaten = 4 while (Tau) : - if (C quot % x = =0) and (quota ey 20) : dun = quake £ brauls r r num tzint (input Cet fins t pel -’) . num tnt Go put Chit Brond numb, =')) Sa Print ("TL, Aeon a , (0 wnpude = ABs Gen, nm) | Outpul ° Fata Last novla = % Ente Bcend rumba = 6 The dem t A 54 i ith the ui . ogram needs to interact with the user to acc ' , 2 rt ions. The i ccomplish the desired task; —_ _eausing mput-Output functio t() function helps to enter data at run time agin on ut function print() is used to di ee ed to display the result ofthe program on the 54d The print() function In Python, the print() function is used to display result on the screen. The syntax for follows: I Example print (“string to be displayed as output ” ) print() is a8 print (variable ) print (“String to be displayed as output ”, variable) print (“String] *, variable, “String 2”, variable, “String 3” ...... I Example >>> print (“Welcome to Python Programming”) Welcome to Python Programming >>>x=5 >>>y=6 >>>z=xt+y >>> print (z) ll >>> print (“The sum =’, 2) The sum = 11 >>> print (“The sum of”, x, “and” y, “is”, z) The sum of 5 and 6 is 11 ‘on the monitor. The print ‘The print ( ) evaluates the expression before printing it ), Commra(;)-is-used asa displays an entire statement which is specified within print ( ‘parator_in-print () to print more than one.item. 4.2 input() function In Python, input() function is used to accept data as input Put() function is, t at run time. The syntax for Variable = input (“prompt string”) Where, i >» Prompt string ii A input can be give g in the syntax is a statement or message to the user, to know wh, ; Wha \ — pe prompt sing ised, itis played onthe monitor the wer cn provide expe the _- evice. The input( ) takes whatever is typed from the keyboard and st *d ne entered data in the given variable. If prompt string is not given in input() no me: ote displayed on the screen, thus, the user will not know what is to be typed as input. “eee th prompt string Example :input() wit >>> city=input (“Enter Your City: ”) Enter Your City: Madurai >>> print (“Lam from *, city) Tam from Madurai Example atiaput( without prompt string >>> city=input() Rajarajan >>> print (“Iam from’, city) Iam from Rajarajan thus the user wil input( ) is not having any prompt string, {If the user inputs irrelevant data as given in the abor Note that in example-2, the your program more interactive, not know what is to be typed as inpul example, then the output will be unexpected. So, to make provide prompt string with input(). \ Cae faput () accepts all data as string but not as numbers. If a numerical value’ meric data type. The intl! entered, the input values should be explicitly converted into nu! data as integer data explicitly. We will learn about more sue. Example 3: x = int (input(“Enter Number 1:”)) Z int (input(“Enter Number 2:”)) print (“The sum =”, x+y) Output: Enter Number 1: 34 Enter Number 2: 56 The sum = 90

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy