Array
Array
Week 2 Lecture
by
Dr Vinay Pathak
Why Algorithm
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.
double balance[10];
int arr[5];
base address
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
#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
int new_element = 9 2 8 7 6 0
int num[]={2,8,7,6,0};
int n = sizeof (num) / sizeof(num[0]);
2 8 7 6 0
2 8 7 6 0
Shifting last element to
the next available space
Beginning of the array
Insertion − 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
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 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
8 7 6 0
num[0] num[1] num[2] num[3] num[4]
Time Complexity
num[2]=num[3];
num[3]=num[4];
8 7 6 0
n=n-1;
2 8 5 6 0
num[0] num[1] num[2] num[3] num[4]
1D Array
Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size of an element = 2
bytes, find the location of A[-1].
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.
• 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.
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.
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
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
Key Element = 8
Total Number of Comparison = 1
f(n) = 1 = Constant = O(1)
8 3 1 2 4 5 6 7
Key Element = 7
Total Number of Comparison = 7
8 3 1 2 4 5 6 7
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
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.
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
Recursion