0% found this document useful (0 votes)
31 views

Array

The document summarizes key concepts about algorithms, arrays, and searching. It defines an algorithm as a well-defined sequence of steps to solve a problem, while a program is an implementation of an algorithm in a programming language. It then discusses arrays as a linear data structure that stores elements in contiguous memory locations that can be accessed via indexes. It covers array initialization, representation in memory, basic operations like traversal, insertion, deletion, and time complexity analysis for different array operations.

Uploaded by

ankushsoun123
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Array

The document summarizes key concepts about algorithms, arrays, and searching. It defines an algorithm as a well-defined sequence of steps to solve a problem, while a program is an implementation of an algorithm in a programming language. It then discusses arrays as a linear data structure that stores elements in contiguous memory locations that can be accessed via indexes. It covers array initialization, representation in memory, basic operations like traversal, insertion, deletion, and time complexity analysis for different array operations.

Uploaded by

ankushsoun123
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Subject: Data Structure

Week 2 Lecture

Algorithm, Time Complexity, Array,


and Searching
What, Why and How?

by

Dr Vinay Pathak
Why Algorithm

Once we find an algorithm for All the knowledge required for


solving a problem, we do not need solving the problem is present in
to re-discover it the next time we the algorithm.
are faced with that problem.

Makes it easy when explaining


the process to others
How Algorithm is different from Program

Algorithm Program
• A program is a set of instructions written in Programming
• An algorithm is a well-defined
Language (computer understandable language) to
sequence of steps (written in Natural
perform a certain task.
Language) to solve a given problem
• Program is an implementation of an algorithm
• Design Time
• Knowledge of Programming Language
• Domain Knowledge
• Testing
• Analyze
• Dependent on OS and H/W
• Independent from OS and H/W
What is Array Data
Structure ??
DS- Array
• An array is a Linear type data structure used to
store a collection of data of same type.

• All arrays consist of contiguous memory


locations.

Syntax type arrayName [arraySize];

• Element − Each item stored in an array is called an element.

• Index − Each location of an element in an array has a numerical


index, which is used to identify the element.
Array Initialization

double balance[10];

Type Array size


name elements

double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};


Array Visualization in Memory

int arr[5];

base address

Meaning of contiguous memory allocations


Array Program
Storing the arrays values Printing the arrays value
#include <iostream>
using namespace std; for (i = 0; i < 5; i++)

int main() { cout<<var[i];


{
}
int var[5], i;
for (i = 0; i < 5; i++)
{ return 0;

cin>>var[i]; }
}
General Assessment Time
10
9
8
7
6
5
4
3
2
1
#include <iostream>
using namespace std; Which one is the
int main() correct answer?
{
a) 25
int a[] = {1,2,3,4};
b) 52
int b[4] = {5,6,7,8};
c) 36
Correct Answer is c cout<< a[2] << b[1];
d) 63
return 0;
}
Operations in Array

• Traverse − print all the array elements one by one.

• Insertion − Adds an element at the given index.

• Deletion − Deletes an element at the given index.

• Update − Updates an element at the given index.

• Search − Searches an element using the given index or by the value.

• Sorting – Arrange the elements in ascending or descending order


Traversing (printing) Array Elements

#include <iostream>
using namespace std;
int main(){
int num[] = {2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
for (int i = 0; i < n; i++)
Time Complexity
{
cout<< num[i]; Time Complexity for Traversal – O(n)
(in all cases)
}
return 0;
}
Insertion − Adding an element

Add an Element 9 #include <iostream>


using namespace std;
int main(){
int num[] = {2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
No Space in the Existing Array, for (int i = 0; i < n; i++)

We can not add any new element. {


cout<< num[i];
}
return 0;
}
Insertion − Adding an element

Create New Space before adding an element


int new_element=9;
int num[] = {2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
2 8 7 6 0

New Element can be added

1. At the end of the array

2. At the beginning or at any index


Insertion − Adding an element

Case 1 : Add the new element at the end of the array


#include <iostream>
using namespace std;
int main(){
int new_element = 9;
int num[] = {2,8,7,6,0};
2 8 7 6 0 9
int n = sizeof (num) / sizeof(num[0]);
n=n+1;
num[n-1]=new_element;
Time Complexity
for (int i = 0; i < n; i++){
cout<< num[i]; Time Complexity of Insertion at the
} end of array – O(1)
return 0; }
Insertion − Adding an element

Case 2 : Add the new element at the beginning of the array

int new_element = 9 2 8 7 6 0
int num[]={2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);

n=n+1; Beginning of the array


Insertion − Adding an element

Case 2 : Add the new element at the beginning of the array

Shifting element to the next available space

2 8 7 6 0

Beginning of the array


Insertion − Adding an element

Case 2 : Add the new element at the beginning of the array

2 8 7 6 0
Shifting last element to
the next available space
Beginning of the array
Insertion − Adding an element

Case 2 : Add the new element at the beginning of the array

int new_element = 9;
int num[]={2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]); 2 8 7 6 0
n=n+1;
0 1 2 3 4 5
num[5]=num[4]; num[0] num[1] num[2] num[3] num[4] num[5]
num[4]=num[3];
num[3]=num[2];
num[2]=num[1];
num[1]=num[0];
9 2 8 7 6 0
num[0]=new_element;
Insertion − Adding an element
Case 2 : Add the new element at the
beginning of the array

int new_element = 9; 2 8 7 6 0
int num[]={2,8,7,6,0}; num[0] num[1] num[2] num[3] num[4] num[5]

int n = sizeof (num) / sizeof(num[0]);

for (i = n-1; i >=1; i--) Time Complexity


n=n+1;
num[5]=num[4]; { Time Complexity– O(n)
num[4]=num[3]; num[i] = num[i-1];
num[3]=num[2];
}
num[2]=num[1];
num[1]=num[0];
num[i]=new_element;
Insertion − Adding an element

Case 3 : Add the new element at some index of an array

int new_element = 12; 2 8 7 6 0


int index=2;
num[0] num[1] num[2] num[3] num[4] num[5]
int n = sizeof (num) / sizeof(num[0]);
n=n+1;

int num[n]={2,8,7,6,0};
num[5]=num[4]; At this index of the array
num[4]=num[3];
num[3]=num[2]; Time Complexity
num[2]=new_element; Time Complexity– O(n)
Deletion of an element in Array

Element can be deleted

1. At the end of the array

2. At the beginning or at any index


Deletion of an element in Array

Case 1 : Element can be deleted at the end of the array

Last element of the array

Just reduce the one size of the array


Time Complexity
Time Complexity of Deletion– O(1)
Deletion of an element in Array

Case 2 : Element can be deleted at the any Index of the array

8 7 6 0
num[0] num[1] num[2] num[3] num[4]

num []= {2,8,7,6,0};

Element at Index 0 of the array int n = sizeof (num) / sizeof(num[0]);


num[0]=num[1];
num[1]=num[2];

Time Complexity
num[2]=num[3];
num[3]=num[4];
8 7 6 0
n=n-1;

Time Complexity of Deletion– O(n)


General Assessment Time
10
9
8
7
6
5
4
3
2
1

Array in C++ indicates?

A) A group of elements of same data type.

B) An array contains more than one element

C) Array elements are stored in memory in continuous or


contiguous locations.

Answer: D D) All the above.


General Assessment Time
10
9
8
7
6
5
4
3
2
1

Choose a correct statement about arrays ?

A) An array size can not change once it is created.

B) Array element value can be changed any number of times

C) To access Nth element of an array students, use students[n-1]


as the starting index is 0.

D) All the above


Answer: D
Updating an element in Array

2 8 5 6 0
num[0] num[1] num[2] num[3] num[4]

int num []= {2,8,7,6,0};


num[2]=5;
Update Value
for (int i = 0; i < 5; i++)
with 5
{
cout<<num[i];
Time Complexity }

Time Complexity for updation– O(1)


Multi-Dimensional Arrays
A multi-dimensional array is an array of arrays. string letters[2][2][2] = {
string letters[2][4]; {
{ "A", "B" },
string letters[2][4] = {
{ "C", "D" }
{ "A", "B", "C", "D" },
},
{ "E", "F", "G", "H" }
{
};
{ "E", "F" },
{ "G", "H" }
}
};
Initializing 2-D Arrays

for ( int i=0; i<n ;i++)


{
for (int j=0; j<n; j++)
{
a[i][j] = 0;
}
}
Accessing Element of an Array

1D Array

Address of element A[i] = base address + size * ( i - first index)

Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size of an element = 2
bytes, find the location of A[-1].

Address of (A[-1]) = 999 + 2 x [(-1) - (-10)]


= 999 + 18 =1017
Accessing Element of in 2D Array

2D Array
Note: 2-D arrays exists only from the user point of view and created to implement a relational
database table look alike data structure. In computer memory, the storage technique for 2D
array is similar to that of an one dimensional array.

Visualization/Representation of 2-D arrays in Memory

• Row Major ordering

• Column Major Ordering


Accessing Element of in 2D Array

Visualization/Representation of 2-D arrays in Memory

• Row Major ordering

• All the rows of the 2D array are stored into the memory contiguously.

• First, the 1st row of the array is stored into the memory completely, then
the 2nd row of the array is stored into the memory completely and so on
till the last row.

a11 a12 a13 a21 a22 a23 a31 a32 a33


Accessing Element in 2D Array

Visualization/Representation of 2-D arrays in Memory

• Row Major ordering

If array is declared by a[m][n] where m is the number of rows while n


is the number of columns, then address of an element a[i][j] of the
array stored in row major order is calculated as,

Address(a[i][j]) = B. A. + (i * n + j) * size
or
Address(a[i][j]) = B. A. + ((i -lr)* n + (j-lc)) * size
lr - Lower limit of row or start limit of the row or assume 0 if it is not given
lc- lower limit of column o start limit of column or assume 0 if it is not given.
Accessing Element of in 2D Array
Address(a[i][j]) = B. A. + ((i-lr) * n + (j-lc)) * size

Example-
Given an array, arr[1………10][1………15] with base value 100 and the size of each element
is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major order.

Row Subset of an element whose address to be found i = 8


Column Subset of an element whose address to be found j = 6
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
Lower Limit of row/start row index of matrix lr = 1
= 100 + 1 * ((7) * 15 + (5))
Lower Limit of column/start column index of matrix lc = 1
= 100 + 1 * (110)
Number of column given in the matrix (n)
Address of a[i][j] = 210
n = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15
Accessing Element in 2D Array

Visualization/Representation of 2-D arrays in Memory

• Column Major ordering

If array is declared by a[m][n] where m is the number of rows


while n is the number of columns, then address of an element
a[i][j] of the array stored in column major order is calculated
as,
Address(a[i][j]) = B. A. + (i + j*m) * size
or
Address(a[i][j]) = B. A. + ((i -lr)+ (j-lc)*m) * size

lr - Lower limit of row or start limit of the row or assume 0 if it is not given
lc- lower limit of column o start limit of column or assume 0 if it is not given.
Visualization of Searching Algorithms

01 Linear Search

02 Binary Search
Visualization of Linear Search

8 3 1 2 4 5 6 7
Algorithm of linear search

Key Element = 4 Step 1- Start from the leftmost element of arr[] and one

Total Number of Comparison = 5 by one compare x with each element of arr[].

Step 2- If x matches with an element, return the index.

Key Element = 7
Step 3- If x doesn’t match with any of the elements,
Total Number of Comparison = 8 return -1.
Visualization of Linear Search

8 3 1 2 4 5 6 7

Time Complexity Analysis

Searching the key element present at the first index of a list

Key Element = 8
Total Number of Comparison = 1
f(n) = 1 = Constant = O(1)

Best Case Time Complexity


Visualization of Linear Search

8 3 1 2 4 5 6 7

Time Complexity Analysis

Searching the key element present at the last index of a list

Key Element = 7
Total Number of Comparison = 7

for n elements f(n) = n

Worst Case Time Complexity


f(n) = O(n)
Visualization of Linear Search

8 3 1 2 4 5 6 7

Time Complexity Analysis


Searching the key element present at any random index of a list

Key Element = 8 or 3 or 1 or 2 or 4 or 5…

Total Avg Time = All possible case time divided by number of cases

Average Case Time for n elements Average Time= (1+2+3+...n)/n = n(n+1)/2n = n+1/2

Complexity Avg Case Time Complexity


f(n) = O(n)
Visualization of Searching Algorithms

2. Binary Search
• Binary search is a fast way to find an item in a sorted list.

• It follows the divide and conquer approach in which the list is divided into
two halves, and the item is compared with the middle element of the list.

• If the match is found then, the location of the middle element is returned.
Otherwise, we search into either of the halves depending upon the result
produced through the match.
Visualization of Searching Algorithms

2. Binary Search
Visualization of Binary Searching Algorithms

1. Divide the search space into two halves by finding the middle index

“mid”.

2. Compare the middle element of the search space with the key.

3. If the key is found at middle element, the process is terminated.

4. If the key is not found at middle element, choose which half will be used as the next search space.

1. If the key is smaller than the middle element, then the left side is used for next search.

2. If the key is larger than the middle element, then the right side is used for next search.

5. This process is continued until the key is found or the total search space is exhausted.
Time Complexity of Binary Searching Algorithms
(Iterative Method Approach) int binarySearch(int[] A, int x)
{
int low = 0, high = A.length - 1;
while (low <= high)
Step 1. Divide the search space into two halves by finding the middle index “mid”.
{
Step 2. Compare the middle element of the search space with the key.
int mid = (low + high) / 2;
Step 3. If the key is found at middle element, the process is terminated. if (x == A[mid])
{
Step 4. If the key is not found at middle element, choose which half will be used as the return mid;
}
next search space. TC= O(Logn)
else if (x < A[mid])
Step 4.1 If the key is smaller than the middle element, then the left side is used for {
How ??
high = mid - 1;
next search.
}
Step 4.2 If the key is larger than the middle element, then the right side is used for else
{
next search.
low = mid + 1;
Step 5. This process is continued until the key is found or the total search space is }
}
exhausted.
return -1;
}
Next Week Topic for Prior Readings

1.Visualization of Time Complexity of


Binary Search Algorithm

Recursion

Sorting Algorithms and Analysis

Project Problem Exploration


Any Queries?

You might also like