Data Structures DECODE
Data Structures DECODE
Max_value)then Max_value < Ali). Ifany value is larger than current ‘Max_value then set new Max_valne by } obtained larger value return Max value Mathematical Analysis Step 1 : The input size is n ice. total number of elements in array. Step 2 + The basic operation is comparison in loop for finding larg) value. ee A Guide for Engineering Studess'Data Structures 2-8 Searching and Sorting Algorithms Step 3 : The comparison is executed on each ition of the loop. As the comparison is made for each value of n there is no need to find best case, worst case and average case analysis, Step 4 : Let C(n) be the number of times the comparison is executed The algorithm makes comparison each time the loop executes. That means with each new value of i the comparison is made. Hence for i = 1 to ni —I times the comparison is made. Therefore we can formulate C(a) as - C(n) = One comparison made for each value of i Step 5 : Let us simplify the sum n=1 ca) = S11 1 Ca) = n-1€0(@) [vine the rule ) 1= ne O(n) i=1 J Thus the efficiency of above algorithm is © (n). Q.9 Write an algorithm for obtaining matrix multiplication. Also analyse it. Ans.: While performing matrix multiplication we scan row of first matrix and column of second matrix. For example : oo Por 490 01 292 1 2 1 2 3 C= x | on “10 11 12 4 4 5 2x3 | boy bay 5 6 Isx2 ‘The formula for multiplication of the above two matrices is 09 X boo + a1 X big + ag2x bag agg X boy + aX byy + ax bay 10% boo + a1 big + ay2X bay aygX boy + aX by +ayX boy ‘A Guide for Engineering SmdentsData Structures 2-9 Searching and Si 1x14+2x343x5 1x 242x443) 4x145x346x5 4x245xK44 22 28 c= [3 64 | Now the algorithm for matrix multiplication is - Algorithm Matrix_Mul (A[0..0-1, 0...n-1), B[O...n-1, 0...n—-1], //Problem Description : This algorithm performs multiplication //of two square matrices /fnput : Two matrices A and B //Output : C matrix containing multiplication of A and B for i-0 to n-1 do for j-0 to n-1 do Chi, J] 0 fork[SPPU : Dec.-06, Marts ¢| Ans, : Sorting is useful for arranging the data-in desired order. sorting the required element can be located easily. Searching technique is essential for locating the position of the requ clement from the heap of data. Important Points to Remember © Seaching is a technique used for locating the position of & sequixed elerment from heap of data. \e Sorting is useful for arranging the data in desired order. Q.14 What is searching technique ? Explain the basic charactaistics f searching algorithm. Ans. ; When we want w find out particular record efficiently from Given Sist of cements then there are various methods of searching element, These methods are called searching methods. at A Gulde for Engineering SextesData Structures 2-14 Searching and Sorting Algorih The basic characteristic of any searching algorithm is - i. It should be efficient. Less number of computations must be involved in it. iii. The space occupied by searching algorithms must be less. Q.15 Explain sequential searching with suitable example. Ans. : Sequential search is technique in which the given list of elements is scanned from the beginning. The key element is compared with every element of the list. If the match is found the searching is stopped otherwise it will be continued to the end of the list. Example Fig. Q.15.1 Represents students database for sequential search From the above Fig. Q.15.1 the array is maintained to store the students record. The record is not sorted at all. If we want to search the student's record whose roll number is 12 then with the key-roll number we will see the every record whether it is of roll number = 12. We obtain such a record at Array [4] location. Q.16 Write a C function for linear search. Discuss its time complexity. 5 [SPU : Dec-15, Marks 6] A Guide for Engineering Soadenss rVEULIVNGData Structures 2-15 Searching ‘and Sorting Ans, : search(int k) { for(i=0;i high) Tetum; mid =(low+high)/2; if: [mid}) Tetum (mid); . if(x arr [£]) fsf-a f=fta b=a b=b-a a=a-b A Guide for Engineering Students vie atin eesData Structures If Key = 20 ie. Key < ar[f] ive. 20< 70 of=f-a57-3=4 bea=3 a=b-a=2 Again we compare if (Key < ar [f]) ie. 20 < ar [4] i.e. if (20 < 40) — Yes At Present f= 4,b=3,a=2 of=f-2=4-2=2 b=a=2 a=b-a=3-2=1 Now we get f= 2,b=2,a= ath Lo] xToJoT soo] deee 8 6 os) 1 7 If Key = 60 ie, Key < arr [f] 60 < 70 wfef-a=7= 3 s14) beaa3 a=b-a=2 Again we compare if (Key > arr [f]) 60 > arr [f] ie. 40 wf=fta=4+2=6° b=b-a=3-2=1 a=a-b=2-3=-1 a b f [ [20 T 20 Tao [50 [0 [a 4 2 3 4 5 61 If (Key < arr [f]) ive, if (60 < 60) > No A Gulde for Engineering Suit?Data Structures 2-22 Searching and Sorting Algorithms If (Key < arr {f) If (key > arr (9) ie. if (20 < 20) > No If Key > arr [f]) i.e. if (60 > 60) + No ive. if (20 > 20) > No That means "Element is present — That means "Element is present at f= 2 location" at f = 6 location.” 2.8 : Sorting Methods Q.22 Write algorithm and C function for bubble sort method. Ans. : Algorithm : 1. Read the total number of elements say n 2. Store the elements in the array 3. Set thei=0. 4, Compare the adjacent elements. 5. Repeat step 4 for all n elements. 6. Increment the value of i by 1 and repeat step 4, 5 for i< n 7. Print the sorted list of elements. 8. Stop. *C! Function void bubblesort (int a[20], int n) int i, j, m,temp; for (i = O;1 A[j] then swap 4, elements. Pass | Vo 4 18 7 1 2 5 10 4 18 7 1 2 5 ha 18 7 1 is 5 4 ol 7 1 2. 5 4 10 ae 1 2 5 4 10 if ge 5 4 10 7 1 2 18 Pass Il 5 4 10 7 1 2 18 4 5 10 7 1 2 18 4 6 10 i 4 2 18 | 4 6 7 10 4 2 18 4 6 7 4 10 2 18 . 4 6 7 1 2 10 18 4 6 7 4 2 10 18 A Gulde for Engineering StudenssData Structures 2-24 Searching and Sorting Algorithms Pass Il 4 Kd 2 10 18 4 5 1 i 10 18 4 5 1 2 7 10 48 Pass IV 4 ia) 2 7 10 18 4 1 V3 7 10 48 SF 4 1 2 5 7 10 18 Pass V , TO 2 5 7 10 18 1 avi 5 7 10 18 1 2 ie 10 18 4 4 5 7 10 18 Pass Vi 1 2 4 5 This is the sorted list of elements. w 7 8 10 18 Q.24 Write a Pseudo code for selection sort algorithm, Ans, vold selection (int A/10] int 1,J,Min,temp; A Gulde for Engineering StudentsData Structures 2-25 Searching and Sorting. for (i=0;i<=n-2ji++) 41j<=n-1j++) if(A[j] [SPPU : May-10, Marks 10] Ans. : Let be the given elements. Pass 1: Consider the elements A[0] as the first element. Assume this ‘the minimum element. 0 4 2 3 4 5 t Scan the array for a finding the smallest element If smallest element is found, swap it with A[0] Ohm? i ai ae A Guide for Engineering Students:Data Structures 2-26 Searching and Sorting Algorithms Pass 2: 0 3.4 ~=65 41 2 [ere lelelel a) 4 —~_-—— Scan array for minimum element Min Pass 3: Scan array for minimum element ‘Swap Al2] and A[4] Min Pass 4: Scan'the array for finding minimum element Pass 5: o 1 2 3 4 5 Min Oo .1 02 3 4 8 a sorted list A Guide for Engineering StudentsData Structures Searching and Sorting A Q.26 What is the difference between internal sorting and sorting ? Sort he following numbers using selection sorts a ’ 25, 17, $1, 13, 2. USP (SPPU : May-18, Marks ¢) | Ans. : Difference which data resides in memory of ‘computer. . Secondary si When limited or small amount of data need to be sorted, then this technique is used, _ Examples ; Bubble sort, insertion sort, selection sort. Itis simple to implement and This is complicated and less efficient. efficient. : Step 1 : We will store the elements in an array. a Trt] 2 | — Scan array for Initially Min finding smallest element Now swap [=e Ta Te T=] these ; elements Min Smallest element [2 Tr [a TT] : ‘A Guide for Engineering StudentsData Structures 2-28 Step 2: element Swap the Be 8 NIE 728 71 «wo elements 31 | 17 | 25 ae [2 TeTe Tels] eigen Min Scan Step 4: Sorted list Se A Guide for Engineering Students Dubhlic avr -Data Structures 2-29 Searching and Sorting Algorithy, Algorithm 1, Set Oth element as Min element 2. Search the minimum element in the list. 3. If the minimum clement is found then swap it with Min. 4, Increment the position of Min to the next adjacent element 5. Repeat step 2 to 4 until the complete list gets sorted. Pseudo code Algorithm Selection(A(0...n-1]) //Problem Description : This algorithm is for sorting the //elements using selection sort method /Muput:An array of eléments A[0...1-1] that is to be sorted Output: The sorted array A[0...n-1] for(i=0;i< =n-2;i+ +) 1: { Min=i; forj=it 1j<=n-1j++) { if(A[]] #include int n; void’ main() A Guide for Engineering StudensData Structures 2-30 Searching and Sorting Algorithms int 1,A[10]; void selection(int A[10]); elrser(); printf(""\n\t\t Selection Sort\n"); printf("\n How many elements are there?”); scanf("%d" ,&n); printf(""\n Enter the elements\n"); for(i=0;i =0)&&(Alj]>temp)) { AU+1=Aqj); j=i-1;Data Structures 2-33 Searching and Sorting Algorithy, Alj+1]=temp; } Printf("\n The sorted list of elements is...\n"); for(i=0;i A [i] increment i and if A [j] > A [pivot] then decrement j. Otherwise swap A [i] and A [j] element, Step 2: A Gulde for Engineering Students