0% found this document useful (0 votes)
32 views30 pages

AOA-1,2 Modules

Very good analysis of algorithm .it's very helpful for preparation of engineering exam.

Uploaded by

badshaah720
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)
32 views30 pages

AOA-1,2 Modules

Very good analysis of algorithm .it's very helpful for preparation of engineering exam.

Uploaded by

badshaah720
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/ 30
Scanned with CamScanner The problem size could be : Cis The ni i umber of inputs/outputs in an algorithm ee pee Example : F 5 ; or sorting algorithm, the number of inputs_is arranged eci ie number of outp ged ina g er. The number of outputs is th The ions i \¢ number of operations involved in the algorithm Example : For a searchin ithm, ig algorithm, the number ce I 1 er_of operations is equal to Cmparisons made with the search element, _ earch clement aul 1 the 9G! numer @ 4 ©” TFwe are sea i yehing an clement in an array having ‘n’ clements, the problem size is same as the number of clements i a ! its in the array to be searched. He i input size are the same and is equal to ‘n’. pee rn Se . If i 4 a be some copy operations (swaps) os problem seo e Humber OF elements in the array or the number of copies performed during sorting, , © If two arrays of size n and ize ii sie and m are merged , the prablem size is the sum of two array © Ifn' factorial is being computed, the problem size is n. 7 1.2.5.1 Time Complexity : f » So ® a & ‘The Time Complexity of an algorithm is the amount of computer time it nceds to execute the rogram and get the intended result. 1.2.5.2 Space Complexity : hms is also defined as the process of determining a formula for Analysis of algori prediction of the memory requirements (piano a plexity of the algorithm. That is, Space Complexity is defined as the amount called Space Com F emory required for running an algoritim. algorithm is aimed at determination of Time complenty and In summary, analysis of Space complexity o ‘he ulgoritimn. itude of an Algorithm : 4.2.6 Order of Magnit algorithm is the sum of number of ‘The Order of Magnitude of an statements contained in it. Consider the following co ‘occurrences of stand the order of magnitude of algorithm: oat nae ae “aN on tert SN | b| 2b, 6 a Scanned with Cam$canner .de snippet to ynder: 1.2.7 Worst Case, Average Case and Best Case Running Time of an Algorithm : While analyzing the algorithm based on Priori Analysis principles, three different cases are identified. It is important to note that this classification is purely based on the nature of the Problem for which we are developing the algorithm, Worst case: Refers to maximum number of instructions / operations that could be executed by the algorithm im order to give the desired output, “ Average case: Refers to number of instructions / of an average in order to give the desired output. Best case: Refers to minimum number of instructions/operations that_could_be uted by the algorithm in order to give the desired outpi 7 Example : Assume there are 100 students in a class The worst case of the problem is none of the students clearing the exams, , perations that could be executed by the algorithm on. ° ° The best of the problem is all100 students clearing the exams. The average case of the problem is around 50 students clearing the exams. Scanned with CamScanner Asymptotic Notations (0, ©, 6) : In order to analyse which data structure or algorithm is the best suited for the job, itis necessary to understand how a data structure or algorithm behaves over time and space. An : important tool in this analysis is Asymptotic Notations. ; Big-Oh notation- ‘0’ Notation : Whi Care uppers bow dD) 4 ions of + This can be used to represent the worst hase or average case or best case conditi algorithms Big ‘O’ refers to set of all functions whose growth rate is higher than that of the C gavew bord) algorithm’s growth rate. Omega notation- ‘A’ Notation : It represents the lower bound for time of execution of an algorithm, It represents the most optimal algorithm to the given problem Omega refers to set of all functions whose growth rate is lower than that of the algorithm's growth rate. Theta notation-‘@' Notation: JL 4° TOS * @-notation is used to denote both upper and lower bounds. 1.2.8.1 Big-Oh Notation- ‘O’ Notation : >>> [ University Exam - May 2003, May 2005, Dec. 2005, Dec. 2006 !!!} . 0() is called as Big Oh. Its meaning is grows as or behaves as. It is defined as the rate at which the time of execution grows in terms of the problem size. Refer Fig. 1.9 al ye 54 om olv™5 eorblerns jeer MOS Scanned with CamScanner 3.3 Insertion Sort : 3.3.1, Simple Insertion : ee >>> [ University Exam — May 2003, May 2006 111} ————_—_ An insertion sort is one that sorts a set of records by inserting records into an existing sorted file, In simple insertion sort, initially x{0] is thought of as a sorted file of one element, In sugcoeding passes one clement (starting with x[1]) 7S Elements which are greater than that clement are shified down by one position, Then that Clements above it. clement is copied into empty place. eee aera me Example: Consider following array x of size 5. so cpate T | ceeaeN Array x[5| : o [i [2 [3 [4 25 | 57 | 48 | 37 | 52 0 [1 3] 4 [ Original File 25 | 37] 4s | 37 [52 Pass 1 ]os [57]: Pass 2 25 | 48 | 57 Pass 3 25 | 37 | 48.[ 57 Pass 4 25 | 37 | 48 | 52 | 57 é Scanned with CamScanner Let ‘x’ be an array of ‘n’ elements Algorithm for insertionsort(x,n) : Step1: Start Step2: fork = ton-l repeat following steps. Step 2.1: Assign x[k] to ‘y’ Step 2.2: fori=k-1 tod Step 2.3 : X[F I] =x{i); ify Comsiders 5 as — GIO TE) ~ 28 $7. GB amd compat S7 Ys ——_——— seen a” a $7 _ —— a : = yo eee - - Ssh SZ : : fa Scanned with CamScanner 1D Seer Sem, / 2 ema Sin AS A MBAMCD_SY Cin ¢ _ . P yeid mate) (wea » 5 - / wt 1 Pa 5 ind PEC” tree wd ghee? ys a SEO? 7a", ino > Gems” CHO eters ane 4 a Ae" Se tee (F505 len ie FS” > oe _ sfey a | UC11 saree » INSEAD _ CoS Cand, PLO wed anye Sieh 4 fer Cine pien cit) ) PEC yA” 2 “a X97 Bet Awa, Weld INSEAD _sose CMH xCJ_ inte) 5 wer i, k, yy chee Y(z0) / i fer (KZN, wan, ket spike * ¥(0T may be 5 “4 tuaht ® as saxedt Oe ome Ae: : yo et wy <— n, sen (Ser?) Ghepy Co <3), XTkKD MA ,, Pe aa i>Z03 Ad ycaciz.; SY move 4 aQiedexciy eryeep, toy A Og 1, , ACUHIBEY sm sey yan 2D: arenton ia Per | 4 , Cary PRS 2 Scanned with CamScanner Selection Sort THs method «'50 req elements, selection sort needs 4 passes. In pass number ‘i’, selection sort finds minimum element between a[i] and a[n — 1] (i.e. from position ito n-1) and then interchange the minimum element with afi] Let us show working of selection sort using diagrams s(n =1) passes for sorting array of n elements. For eg, to sort array of S Let ‘a’ be arrary of 5 integers o [5s In pass 0 ' 8 We check minimum ¢lement between a [0] to a [4], which is a[4], 2 0 We interchange this minimum with a [0] , 7 2 4 [1 ‘The array after pass 0 is a7, e,op2 ts 1 2 3 4 ‘ ; We check minimum element between af1] to a{n ~ 1] which is ag (= 0) and we interchange In pass 1 : this minimum with a[1] The array after pass | is 0 8 2 5 =I o 1 2 3 4 imilarly all passes can be shown as follows “ee "pass 0 Pass | Pass 2 Pass 3 Sorted array of4 o[s ; a7 2 2{[0 ; 3 [2 $ 4fa i TSE MTOSFINGIOE 278 / Scanned with CamScanner , we Program for selection sort import java.util.*; Public class Selection + static void selecti i f selectionsori(int D> int min, pt; for (int 1=0;i<=alength - 2; iH) - i Gene) min = ai] ; passes = n-\ et gr clements ,then ALES 5 4 OOD for (int j=i+ 135 <= a.length «1: fap} >> [University Exam — May 2003, Let x be an array, and 1 the number of elements in the array to be sorted. Choose an clement a (pivot element) from a specific position within the array (for example, a can be chosen as the first clement so that a = x[0]). Suppose that the elements of x aré-partitioned so that a is placed into position j and the following Conditions hold: 1. Each of the elements in positions 0 through j-1 is less than or equal to a 2. Each of the elements in positions +1 through n-1 is greater than or equal to a. Py 0 jist nt | £ q the o fepnotoneonss) TY opted ence oy HK — Prot = NA KK & w If these two conditions hold for a particular a and j, a is the j smallest clement of x, so that @ remains in positions j when the array is completely sorted. If these process is repeated with the sub arrays x[0] through x[/-1] and x[/+1] through x[7-1] and any sub arrays created by the process in successive iterations, the final result is a sorted array. Example : eh Let us illustrate the quick sort with an example. If an initial array is givenas: . g\@ ~ of-J~pafalstals)] .~ oo Numbers | 25 | 57 | 4s | 37 (12, 92 | 86,[ 33.[=—~ =, And the first clement (25) is placed in its proper position, the resulting array is 12 25 57 483792 BHC 8B At this point, 25 is in its proper position in the array (x{1]), cach clement below hat position (12) is less than or equal to 25, and cach element above that position (57,48,37.92,86,33) is greater than or equal to 25.Since 25 is in its final position ‘The original problem has been decomposed into the problem of sorting the two sub arrays : [i2) and [57 48 37 92 86 33) Noting need to be done to sort the first of these subs rays, a file of one element is further subdivided. The entire array may now be viewed as 2 25) 87, 4837 2 8H 33] ‘Where brackets enclose the sub arrays that are yet to be sorted, Repeating the precess on the subarray x[2] through x{7] yields. S 1225) (4837.33) ST 9288 Scanned with CamScanner ‘Quick Sort (partition sort) — : < This method works on Divide & Conquer policy. It partitions the array into 2 partitions using an algorithm which is described below : \ eg: Let array be as shown 50 75:10 100 40 200 «45 300 15 ~~ 400 Partition process is as follows : We have to fix 50_i.¢. left most element at such a place that to the left of 50 all elements are less than 50, and all elements to the right of 50 are more than 50-— ec hie For fixing 50 at right place, we use two counters i and j, where i starts with lowest bound and j starts with highest bound. 2... i increases by 1 till it gets value more than 50 and j decreases by 1 till it gets value less than or equal to 30. We interchange a [i] and a [j] and repeat this process till (i 15 10 45 40 200. 100 300-75 400 = on 7 7 Next i stops at 200 which js more than 50 Next j stops at 40 which is less than or equal to.50 We cannot interchange a [i] and a {j] asi>j : 50 15 10 45 40 200 100 300 75 400 if ii —— Final step : j gives place of partition, Hence’S0 will be fixed-at j place, (40; 15,1045 | 56| +200 100 300 75- 400 S87 TS sofa atsightplacs, Scanned with CamScanner on WwW 3 int it + 748 Nal 21 ee i= ten; Wee i = right; 7 v 29 ~ X= alleft; , dee While (i\cj ss a AES) Doe, 067 AL ALT Sted: Scanned with CamScanner x ~ Hence ~Le_c Obscene 4dhat no. Of Coy antse,. oni by 4 for © ven . Cor _ or Yea) Son snd (ny) AC Ore ASG ~~ 24 no > Best cose: Picea nis Quick Sort williwoal ta best Case only iF paotiHon fe alutays at He midpoint So tnat lett § aight aver 7 @{ equod length tnsuck « ‘ ton)= © (n 104% 9) Votes eg : Hus devivadion is “appl cable a Baga oat Y pane oy Ey (eat devivention ) Look. Scanned with CamScanner 2.5 Merge Sort “ The merge sort is a sorting algorithm that uses the divide and conquer strategy, this method division is dynamically carried out. Merge sort on an input array with n elements consists of three steps: Divide : partition array into two sub lists s1 and s2 with n/2 elements each. Conquer : Then sort sub list s1_ and sub list s2. Combine : merge s1 and s2 into a unique sorted group. Example : Consider the elements as 70, 20, 30, 40, 10, 50, 60 Now we will split this list into two sublists. | z/2% 2.1: Sort the following elements using merge sort. yp Example 10, 5,7 6& 1 % 8 3, 2, 9 Scanned with CamScanner Algorithm MergeSort(intA(@_..n-1],low,high) /feroblem Description: This algorithm is for sorting the //élements using merge sort /Mnput: Array A of unsorted elements, low as beginning /pointer of array A and high as end pointer of array A //Output: Sorted array AJ0...n - 1] if(low < high)then mid < (low+high)/2 _// split the list at mid MergeSort(A,low,mid) _// first sublist MergeSort(A,mid+ 1,high) // second sublist F Combine(A,low,mid, high) // merging of two sublists } Algorithm Combine(A(0...n-1],low, mid, high) while(i_ <= mid andj <= high}do — | je mid gt // jas indox Enea A and ? if(Ali]<=Afj]then // if smaller element is present in left sublist = { _ // copy that smaller element to temp array templk] « Ali ieitl kekt+ eise //smaller element is present in right sublist //copy that smaller element to temp array temp[k] — Ali] joitt kek+1 } I/copy remaining while(i<=mid)do elements of left sublist to temp tempi] < Alil Scanned with CamScanner Logic Explanation Te © understand above algorithm consider a tk ipt of elenrents as [ To 20, | 30 40 Pic om, co] O74 2 3 4 5 6 T T v low _ — > mid high Then we will first make two sublists as [ermal] Jlense a By @ Trisistis “This list can be subdivided This st @ Ganbe Margo sont (A. subdivided ‘Combine two sublists in temp array @) Combine two sublists Combine wo sublists Combine these two sublists @ combine two ‘sublists Scanned with CamScanner Scanne d with CamScanner Divide and Conqui : ype S 2 & max < A [i] n co ; min A jj] j = erin else if (i ONIE min_new) then min¢ min_new //combine solution } Example : Consider a list of some elements from which maximum and minimum element can be found out. 3 5 6 7 Scanned with CamScanner Step 1: er rr 1 Lo [0 Ts [=e [a | suis 7 6 7 9 l a Ls 25 75 | Sublist 2 lists : Sublist 1 and s' from each sublist. We have divided the original list at mid and two sul ublist 2 |are created. We will find min and max values respective sublists 8 9 Gh sublist obtain’ Again divide each sub) list and create further sublists. ‘Then from ea min and max values 6 7 8 9 It is possible to divide the list (50, 40, ~ 5) further. Hence we have divided the lis into sublists and min, max values are obtained. ~ Now further division of the list is not possible. Hence we start combining the solutions of min and max values from each sublist. be Scanned with CamScanner 123 78 [e[o[-=] Este] Combine (1, ... 3) and (4, 5) Min = - 9, Max = 50 Now we will combine (1, ... 3) and (4, 5) and the min and max values among} them are obtained. Hence, 9 50 min value max value Step 5: se Combine (6, 7) and (8, 9) Min = 25, Max = 90 Step 6: 1 2 3 4 5 6 sg 8 3 LT [-s[-e[s] [so [es [os [re] Combine the sublists (1, are 5) and @, .... 9). Find out min and max values which min =-9 max = 90 Thus the complete list is formed from which the min and max values are obtained. Hence final min and max values are min = - 9 and max = 90 Scanned with CamScanner a ; - c é 2.3 Binary Search i njesval seareohy | legamith “ sea Bigary searcl efficient searching method. While se: i lements. using this method theaiost essential thing is that fhe elements in the array should be sorte ) one. *— An element which is to be searched from the list of elements stored in array Al0....=T] is called KEY element = Let Alm] be the mid element of array A. Then there are three conditions that needs to be tested while searching the array Using this method 1. If KEY=A[m] then desired element is present in the list. 2. Otherwise if KEYA[m] then search the right sub list. This can be represented as Al0] .. Afm-1] A[m] A[m+1] ... Afn-1] cart Search here if KEY? Search here if KEY Ac IRV AG) Let us take one example to understand this methoa Example Consider a list of elements stored in array A as 0 1 2 3 14 5 6 [ 10 | 20 30 | 40 50 60 7o_| T T Low High The KEY element (i.e. the element to be searched) is 60. Now to obtain middle element we will apply a formula : m = (low + high)/2 m = (0+6)/2 m=3 Then Check A[m] 2 KEY Scanned with CamScanner #include #define SIZE 10 int n; void main() { int A[SIZE] KEY J,flag,low,high; int BinSearch(int A(SIZE],int KEY, int low,int high); elrscr(); print{("\n How Many elements in an array?”); scanf("%d",&n); 1; x sinSearch(A,KEY low, high); Ay ag print{(“\n The elerient is at A[%d] location” flag); Gy My getch(); xf “f & 5 } & e we es int BinSearch(int A[SIZE],int KEY,int low,int high) 3° { int m; y m=(low+high)/2; _//mid of the array is obtained 4x es if(KEY==A[m)) 0 Teturn m; . else if(KEY

You might also like