SlideShare a Scribd company logo
1 of 44
Lecture 2:
Linear Data Structures
1
DATA STRUCTURE AND ALGORITHM
1. Linear Data Structures
• A type of data structure that arranges the
data items in an orderly manner where
the elements are either physically or
logically attached adjacently
• Items are traversed sequentially
• Single-level
• Easier to implement
2
Examples
3
Physically vs. Logically Linear
• The linear data structure whose successive
components occupy consecutive memory
locations are called physically linear data
structures. Memory utilization is inefficient.
• Example: Arrays
• The linear data structure whose components are
accessed in some sequence but are not
necessarily placed in consecutive memory
locations are called logically linear data
structures.
• Example: Linked Lists
• Queues and Stacks can be either physical or logical depending
upon the data structure being used for implementation.
4
2. Arrays
• An Array is a finite sequence of storage cells, for which the
following operations are defined:
• Create an Array A with a storage for N items
– A[i]=item; stores an item in the ith position of
the Array A
– A[i] returns the value of the item stored in the
ith position of the Array A
5
Array as an ADT
• Data: A fixed-size sequence of
elements, all of the same type
• Operations:
Direct access to each element in the
array by specifying its position so
that values can be retrieved from or
stored in that position.
6
Static vs. Dynamic Arrays
10/24/2023 7
Memory Management
10/24/2023 8
Memory Management
10/24/2023 9
Static Arrays
10
const int size=5;
int arr[size];
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
int arr[5];
for(int i=0;i<5;i++)
{
cin>>arr[i];
}
int arr[]={1,2,3,4,5}; int size = 5;
int arr[size];
int size;
cin>>size;
int arr[size];
Dynamic Arrays
11
int *a;
int size=5;
a = new int[size];
for(int i=0;i<size;i++)
{
cin>>a[i];
}
int *a;
int size;
cin>>size;
a = new int[size];
for(int i=0;i<size;i++)
{
cin>>a[i];
}
delete []a;
Accessing Array Elements
10/24/2023 12
int main()
{
int a[] = {1, 2, 3, 4, 5};
int *p;
p=a;
for(int i=0; i<5; i++)
{
//Subscript notation with array name
cout<<a[i];
//Subscript notation with pointer name
cout<<p[i];
//Offset notation using array name
cout<<*(a+i);
//Offset notation using pointer name
cout<<*(p+i);
return(0);
3. Arrays as Data Structures
(Applications)
• Sorting items
– Ascending order
– Descending order
– Lexicographical order
• Searching items
– Linear Search (Unsorted Arrays)
– Binary Search (Sorted Arrays)
13
Ascending order
(Bubble Sort)
14
Descending order
(Bubble Sort)
• Unsorted list of consists of 5
elements (students).
15
Descending order
(Bubble Sort)
• Unsorted list of consists of 5
elements (students).
10/24/2023 16
Bubble Sort
17
Linear Search
18
 Work for both sorted and unsorted array.
For example; We have given an array (1to6) and
indexes are (0to5). Need to search element (4).
Linear Search
19
Linear Search
20
Example 1: Binary Search
21
 Work only for sorted array.
For example; We have given an array (1to6) and indexes are
(0to5). Need to search element (4).

Medium = low + high /2
= 0+5/2 = 2
2
1 4
3 6
5
2
1 4
3 6
5
0 1 2 3 4 5
0 1 2 3 4 5
Element 3 < 4. Therefore delete
(0, 1, 2)
Example 1: Binary Search
22
 Work only for sorted array.
For example; We have given an array (1to6) and indexes are
(0to5). Need to search element (4).
= 3+5/2 = 4
4 6
5
3 4 5
4 6
5
3 4 5
Element 5 > 4. Therefore delete
(4, 5)
4
3
Example 2: Binary Search
23
Binary Search
24
Binary Search
25
Example 3: Binary Search
26
Binary Search
• Binary Search:
27
Binary Search vs. Linear Search
10/24/2023 28
4. Greatest common divisor
• The greatest common divisor (GCD) of two or
more numbers is the greatest common factor
number that divides them, exactly. It is also
called the highest common factor (HCF). For
example, the greatest common factor of 15
and 10 is 5, since both the numbers can be
divided by 5.
• 15/5 = 3
• 10/5 = 2
Greatest common divisor
• Suppose, 4, 8 and 16 are three numbers. Then
the factors of 4, 8 and 16 are:
• 4 → 1,2,4
• 8 → 1,2,4,8
• 16 → 1,2,4,8,16
Example
Example
Flowchart
GCD Algorithm (Pseudocode)
• The Euclidean Algorithm for calculating GCD of two numbers A and B can be
given as follows:
• If A=0 then GCD(A, B)=B since the Greatest Common Divisor of 0 and B is B.
• If B=0 then GCD(a,b)=a since the Greates Common Divisor of 0 and a is a.
• Let R be the remainder of dividing A by B assuming A > B. (R = A % B)
• Find GCD( B, R ) because GCD( A, B ) = GCD( B, R ). Use the above steps again.
Code Example: 1. Find HCF/GCD using
for loop
Output
Code Example: 2. Find GCD/HCF using
while loop
Output
Asymptotic Analysis
• Asymptotic Notations: Asymptotic notations are the
mathematical notations used to describe the running
time of an algorithm when the input tends towards a
particular value or a limiting value.
• For example: In bubble sort, when the input array is
already sorted, the time taken by the algorithm is
linear i.e. the best case.
• But, when the input array is in reverse condition, the
algorithm takes the maximum time (quadratic) to sort
the elements i.e. the worst case.
Asymptotic Analysis
• When the input array is neither sorted nor in
reverse order, then it takes average time.
These durations are denoted using asymptotic
notations.
• There are mainly three asymptotic notations:
• Big-O notation
• Omega notation
• Theta notation
Big-O Notation (O-notation)
• Big-O notation
represents the
upper bound of
the running time
of an algorithm.
Thus, it gives the
worst-case
complexity of an
algorithm.
Omega Notation (Ω-notation)
• Omega notation
represents the
lower bound of
the running time
of an algorithm.
Thus, it provides
the best case
complexity of an
algorithm.
Theta Notation (Θ-notation)
• Theta notation
encloses the function
from above and below.
Since it represents the
upper and the lower
bound of the running
time of an algorithm, it
is used for analyzing
the average-case
complexity of an
algorithm.
Example
Example
Code Example: C++ program to find
time complexity for single for loop
Output

More Related Content

Similar to Lec2-Array.pptx

b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.pptclassall
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptxclassall
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysMartin Chapman
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm. Abdul salam
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 

Similar to Lec2-Array.pptx (20)

Big O Notation
Big O NotationBig O Notation
Big O Notation
 
b,Sc it data structure.ppt
b,Sc it data structure.pptb,Sc it data structure.ppt
b,Sc it data structure.ppt
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
b,Sc it data structure.pptx
b,Sc it data structure.pptxb,Sc it data structure.pptx
b,Sc it data structure.pptx
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
DSA
DSADSA
DSA
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
Data structure
Data structureData structure
Data structure
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Data structure and algorithm.
Data structure and algorithm. Data structure and algorithm.
Data structure and algorithm.
 
Complexity
ComplexityComplexity
Complexity
 
Data structures
Data structuresData structures
Data structures
 
Data structure Unit-I Part A
Data structure Unit-I Part AData structure Unit-I Part A
Data structure Unit-I Part A
 
DAA Notes.pdf
DAA Notes.pdfDAA Notes.pdf
DAA Notes.pdf
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 

Lec2-Array.pptx

  • 1. Lecture 2: Linear Data Structures 1 DATA STRUCTURE AND ALGORITHM
  • 2. 1. Linear Data Structures • A type of data structure that arranges the data items in an orderly manner where the elements are either physically or logically attached adjacently • Items are traversed sequentially • Single-level • Easier to implement 2
  • 4. Physically vs. Logically Linear • The linear data structure whose successive components occupy consecutive memory locations are called physically linear data structures. Memory utilization is inefficient. • Example: Arrays • The linear data structure whose components are accessed in some sequence but are not necessarily placed in consecutive memory locations are called logically linear data structures. • Example: Linked Lists • Queues and Stacks can be either physical or logical depending upon the data structure being used for implementation. 4
  • 5. 2. Arrays • An Array is a finite sequence of storage cells, for which the following operations are defined: • Create an Array A with a storage for N items – A[i]=item; stores an item in the ith position of the Array A – A[i] returns the value of the item stored in the ith position of the Array A 5
  • 6. Array as an ADT • Data: A fixed-size sequence of elements, all of the same type • Operations: Direct access to each element in the array by specifying its position so that values can be retrieved from or stored in that position. 6
  • 7. Static vs. Dynamic Arrays 10/24/2023 7
  • 10. Static Arrays 10 const int size=5; int arr[size]; for(int i=0;i<size;i++) { cin>>arr[i]; } int arr[5]; for(int i=0;i<5;i++) { cin>>arr[i]; } int arr[]={1,2,3,4,5}; int size = 5; int arr[size]; int size; cin>>size; int arr[size];
  • 11. Dynamic Arrays 11 int *a; int size=5; a = new int[size]; for(int i=0;i<size;i++) { cin>>a[i]; } int *a; int size; cin>>size; a = new int[size]; for(int i=0;i<size;i++) { cin>>a[i]; } delete []a;
  • 12. Accessing Array Elements 10/24/2023 12 int main() { int a[] = {1, 2, 3, 4, 5}; int *p; p=a; for(int i=0; i<5; i++) { //Subscript notation with array name cout<<a[i]; //Subscript notation with pointer name cout<<p[i]; //Offset notation using array name cout<<*(a+i); //Offset notation using pointer name cout<<*(p+i); return(0);
  • 13. 3. Arrays as Data Structures (Applications) • Sorting items – Ascending order – Descending order – Lexicographical order • Searching items – Linear Search (Unsorted Arrays) – Binary Search (Sorted Arrays) 13
  • 15. Descending order (Bubble Sort) • Unsorted list of consists of 5 elements (students). 15
  • 16. Descending order (Bubble Sort) • Unsorted list of consists of 5 elements (students). 10/24/2023 16
  • 18. Linear Search 18  Work for both sorted and unsorted array. For example; We have given an array (1to6) and indexes are (0to5). Need to search element (4).
  • 21. Example 1: Binary Search 21  Work only for sorted array. For example; We have given an array (1to6) and indexes are (0to5). Need to search element (4).  Medium = low + high /2 = 0+5/2 = 2 2 1 4 3 6 5 2 1 4 3 6 5 0 1 2 3 4 5 0 1 2 3 4 5 Element 3 < 4. Therefore delete (0, 1, 2)
  • 22. Example 1: Binary Search 22  Work only for sorted array. For example; We have given an array (1to6) and indexes are (0to5). Need to search element (4). = 3+5/2 = 4 4 6 5 3 4 5 4 6 5 3 4 5 Element 5 > 4. Therefore delete (4, 5) 4 3
  • 23. Example 2: Binary Search 23
  • 26. Example 3: Binary Search 26
  • 28. Binary Search vs. Linear Search 10/24/2023 28
  • 29. 4. Greatest common divisor • The greatest common divisor (GCD) of two or more numbers is the greatest common factor number that divides them, exactly. It is also called the highest common factor (HCF). For example, the greatest common factor of 15 and 10 is 5, since both the numbers can be divided by 5. • 15/5 = 3 • 10/5 = 2
  • 30. Greatest common divisor • Suppose, 4, 8 and 16 are three numbers. Then the factors of 4, 8 and 16 are: • 4 → 1,2,4 • 8 → 1,2,4,8 • 16 → 1,2,4,8,16
  • 34. GCD Algorithm (Pseudocode) • The Euclidean Algorithm for calculating GCD of two numbers A and B can be given as follows: • If A=0 then GCD(A, B)=B since the Greatest Common Divisor of 0 and B is B. • If B=0 then GCD(a,b)=a since the Greates Common Divisor of 0 and a is a. • Let R be the remainder of dividing A by B assuming A > B. (R = A % B) • Find GCD( B, R ) because GCD( A, B ) = GCD( B, R ). Use the above steps again.
  • 35. Code Example: 1. Find HCF/GCD using for loop Output
  • 36. Code Example: 2. Find GCD/HCF using while loop Output
  • 37. Asymptotic Analysis • Asymptotic Notations: Asymptotic notations are the mathematical notations used to describe the running time of an algorithm when the input tends towards a particular value or a limiting value. • For example: In bubble sort, when the input array is already sorted, the time taken by the algorithm is linear i.e. the best case. • But, when the input array is in reverse condition, the algorithm takes the maximum time (quadratic) to sort the elements i.e. the worst case.
  • 38. Asymptotic Analysis • When the input array is neither sorted nor in reverse order, then it takes average time. These durations are denoted using asymptotic notations. • There are mainly three asymptotic notations: • Big-O notation • Omega notation • Theta notation
  • 39. Big-O Notation (O-notation) • Big-O notation represents the upper bound of the running time of an algorithm. Thus, it gives the worst-case complexity of an algorithm.
  • 40. Omega Notation (Ω-notation) • Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides the best case complexity of an algorithm.
  • 41. Theta Notation (Θ-notation) • Theta notation encloses the function from above and below. Since it represents the upper and the lower bound of the running time of an algorithm, it is used for analyzing the average-case complexity of an algorithm.
  • 44. Code Example: C++ program to find time complexity for single for loop Output

Editor's Notes

  1. Index = element
  2. https://www.youtube.com/watch?v=qTb1sZX74K0 Static Array work as stack. [Delete at runtime] Dynamic Array work as Heap. [Donot delete easily. Switch-off PC] Heap: Heap storage is used to allocate storage that has a lifetime that is not related to the execution of the current routine. 
  3. For Dynamic Array; we use pointer to
  4. a [ ] > a [ ] = swap a [ ] < a [ ] = NO swap
  5. a [ ] > a [ ] = No swap a [ ] < a [ ] = swap
  6. https://www.youtube.com/watch?v=sSYQ1H9-Vks
  7. https://www.youtube.com/watch?v=sSYQ1H9-Vks