Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
32 views
30 pages
AOA-1,2 Modules
Very good analysis of algorithm .it's very helpful for preparation of engineering exam.
Uploaded by
badshaah720
AI-enhanced title
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
Download
Save
Save AOA-1,2 modules For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
32 views
30 pages
AOA-1,2 Modules
Very good analysis of algorithm .it's very helpful for preparation of engineering exam.
Uploaded by
badshaah720
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save AOA-1,2 modules For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save AOA-1,2 modules For Later
You are on page 1
/ 30
Search
Fullscreen
Scanned with CamScannerThe 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 CamScannerAsymptotic 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 CamScanner3.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 CamScannerLet ‘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 CamScanner1D 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 CamScannerSelection 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 CamScanneron 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 CamScannerx ~ 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 CamScanner2.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 CamScannerAlgorithm 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 CamScannerLogic 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 CamScannerScanne d with CamScannerDivide 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 CamScannerStep 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 CamScanner123 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 CamScannera ; - 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 KEY
A[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
Lecture1 Intro
PDF
No ratings yet
Lecture1 Intro
21 pages
DAA Unit-1
PDF
No ratings yet
DAA Unit-1
20 pages
Algo 1
PDF
No ratings yet
Algo 1
16 pages
U2 - Decrease and Conqure
PDF
No ratings yet
U2 - Decrease and Conqure
38 pages
Bcs 042
PDF
No ratings yet
Bcs 042
22 pages
s24 - Csal3233 - Aa - Obe - Outline
PDF
No ratings yet
s24 - Csal3233 - Aa - Obe - Outline
9 pages
Daa Unt 1
PDF
No ratings yet
Daa Unt 1
72 pages
DAA Unit 1 Notes
PDF
No ratings yet
DAA Unit 1 Notes
34 pages
Introduction To Algorithm Design and Analysis: A, A,, A A ', A ',, A ' Such That A ' A ' A
PDF
No ratings yet
Introduction To Algorithm Design and Analysis: A, A,, A A ', A ',, A ' Such That A ' A ' A
21 pages
DSNotes M1
PDF
No ratings yet
DSNotes M1
18 pages
DAA Practical File
PDF
No ratings yet
DAA Practical File
57 pages
ALGO Notes
PDF
No ratings yet
ALGO Notes
24 pages
Insertion Sort: While Some Elements Unsorted
PDF
No ratings yet
Insertion Sort: While Some Elements Unsorted
22 pages
Insertion Sort: While Some Elements Unsorted
PDF
No ratings yet
Insertion Sort: While Some Elements Unsorted
22 pages
CSE373L1
PDF
No ratings yet
CSE373L1
72 pages
Introduction
PDF
No ratings yet
Introduction
11 pages
CSC 401
PDF
No ratings yet
CSC 401
17 pages
Data Structures: Lecture 1: Introduction
PDF
No ratings yet
Data Structures: Lecture 1: Introduction
27 pages
Chap 1
PDF
No ratings yet
Chap 1
34 pages
Unit1 SP
PDF
No ratings yet
Unit1 SP
41 pages
Unit I
PDF
No ratings yet
Unit I
88 pages
Algorithms
PDF
No ratings yet
Algorithms
24 pages
Introduction To Algorithms
PDF
No ratings yet
Introduction To Algorithms
41 pages
Lect1 1
PDF
No ratings yet
Lect1 1
36 pages
2 Program Complexities
PDF
No ratings yet
2 Program Complexities
37 pages
CPS 305 - Lecture Note and Course Outline
PDF
No ratings yet
CPS 305 - Lecture Note and Course Outline
26 pages
Week 5
PDF
No ratings yet
Week 5
37 pages
DAA Handouts Apr 29
PDF
No ratings yet
DAA Handouts Apr 29
114 pages
DSA - Notes 2
PDF
No ratings yet
DSA - Notes 2
19 pages
Daa Decode
PDF
No ratings yet
Daa Decode
205 pages
Chap 1
PDF
No ratings yet
Chap 1
36 pages
7 Sorting
PDF
No ratings yet
7 Sorting
85 pages
Soluzioni Cormen
PDF
No ratings yet
Soluzioni Cormen
20 pages
Ca 1
PDF
No ratings yet
Ca 1
24 pages
2 ND Note Sorting
PDF
No ratings yet
2 ND Note Sorting
20 pages
Slides9 8
PDF
No ratings yet
Slides9 8
55 pages
Unit I: Introduction: Algorithm
PDF
No ratings yet
Unit I: Introduction: Algorithm
17 pages
UNIT I (Repaired)
PDF
No ratings yet
UNIT I (Repaired)
43 pages
IntroductiontoDataStructureandArray PDF
PDF
100% (1)
IntroductiontoDataStructureandArray PDF
34 pages
Cs 316: Algorithms (Introduction) : SPRING 2015
PDF
No ratings yet
Cs 316: Algorithms (Introduction) : SPRING 2015
44 pages
Foundations of Computing Science
PDF
No ratings yet
Foundations of Computing Science
97 pages
Data Structure
PDF
No ratings yet
Data Structure
36 pages
Asymptotic Analysis (Big-O Notation) : Big O Notation Is Used in Computer Science To Describe The Performance
PDF
No ratings yet
Asymptotic Analysis (Big-O Notation) : Big O Notation Is Used in Computer Science To Describe The Performance
10 pages
Brief Introduction of Data Structure
PDF
No ratings yet
Brief Introduction of Data Structure
36 pages
AlgDs1LectureNotes 2025 02 16
PDF
No ratings yet
AlgDs1LectureNotes 2025 02 16
89 pages
Module - 01
PDF
No ratings yet
Module - 01
3 pages