High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
03 Linear Arrays Memory Representations .pdf
1. Data Structures & Algorithms
By
Dr. Prakash Singh Tanwar
Associate Professor
Lovely Professional University, Punjab
Topic: Linear Arrays Memory
Representations
2. Linear Arrays Memory
Representation
Basic Terminology
Linear Array
Memory Representation of Linear Array
Traversing Array
Insertion and Deletion in Array
Sorting (Bubble Sort)
Searching (Linear Search and Binary Search)
Review Questions
By:Dr. Prakash Singh Tanwar
3. Basic Terminology
Linear Data Structures:
A data structure is said to be linear if its elements form a
sequence or a linear list.
Linear Array:
It is a list of a finite number n of homogeneous data
elements (same type of data elements) such that:
• (a) the elements of the array are referenced by an index set
consisting of n consecutive numbers.
• (b) the elements of the array are stored respectively in successive
memory locations
By:Dr. Prakash Singh Tanwar
4. Revision
Definition
Array is a group of variables of same data type
having a common name.
Elements of an array can be accessed through
its subscript(0 to n-1) where n is the size of
array.
Continous memory allocation
By:Dr. Prakash Singh
Tanwar
5. Revision: 1D Array
A single- dimensional or 1-D array consists of
a fixed number of elements of the same data
type organized as a single linear sequence.
Array declaration
Syntax
Ex
<data_type> <array_name>[<size>];
int a[5];
char name[20];
float num[4];
By:Dr. Prakash Singh Tanwar
7. 1D Array Memory
Representation
Array declaration
a[0] a[1] a[2] a[3] a[4]
20
int a[5];
a[0]=9;
a[1]=6;
a[2]=7;
a[3]=5;
a[4]=20;
9 6 7 5
1001 1003 1005 1007 1009
size of int = 2 bytes
&a[0]= 1001
&a[1]= 1001+2=1003
&a[2]= 1003+2=1005
&a[3]= 1005+2=1007
&a[4]= 1007+2=1009
size of int = 2 bytes
&a[0]= 1001
&a[1]= 1001+2=1003
&a[2]= 1003+2=1005
&a[3]= 1005+2=1007
&a[4]= 1007+2=1009
Note: size of int in 32 bit compiler =2 bytes
Note: size of int in 32 bit compiler =2 bytes
By:Dr. Prakash Singh Tanwar
8. 1D Array Memory
Representation
Array declaration
a[0] a[1] a[2] a[3] a[4]
20
int a[5];
a[0]=9;
a[1]=6;
a[2]=7;
a[3]=5;
a[4]=20;
9 6 7 5
1001 1005 1009 1013 1017
size of int = 4 bytes
&a[0]= 1001
&a[1]= 1001+4=1005
&a[2]= 1005+4=1009
&a[3]= 1009+4=1013
&a[4]= 1013+4=1017
size of int = 4 bytes
&a[0]= 1001
&a[1]= 1001+4=1005
&a[2]= 1005+4=1009
&a[3]= 1009+4=1013
&a[4]= 1013+4=1017
Note: size of int in 64 bit compiler =4 bytes
Note: size of int in 64 bit compiler =4 bytes
By:Dr. Prakash Singh Tanwar
9. Q/A
Are these valid statements?
int a[10];
a[10]=10;
a[10]=10; is not valid
Because, in array a[10] we have only 10 elements
from a[0] to a[9]
By:Dr. Prakash Singh Tanwar
11. Basic Terminology
Size / Length of Array
Index of Array
Upper bound and Lower bound of Array
By:Dr. Prakash Singh Tanwar
a[0] a[1] a[2] a[3] a[4]
Size / Length of Array=5
Index of array : 0 to 4
Lower Bound Upper Bound
12. Traversing Linear Array
Suppose we have to count the number of element in an
array or print all the elements of array.
By:Dr. Prakash Singh Tanwar
Algorithm 1: (Using While Loop)
1. [Initialize Counter.] Set K = LB.
2. Repeat Step 3 and 4 while K<= UB.
3. [Visit Element.] Apply PROCESS to A[K].
4. [Increase Counter.] Set K = K+1.
[End of Step 2 Loop.]
5. Exit.
a[0] a[1] a[2] a[3] a[4]
20
9 6 7 5
K
LB UB
13. Traversing Linear Array
Algorithm 2: (Using for loop)
By:Dr. Prakash Singh Tanwar
1. Repeat for K = LB to UB
Apply PROCESS to A[K].
[ End of Loop.]
2. Exit.
a[0] a[1] a[2] a[3] a[4]
20
9 6 7 5
K
LB UB
14. Q/A
Question.1: Find the Number of elements in an array
which are greater than 25
Question 2: Find out the sum of all the two digit
numbers in an array.
By:Dr. Prakash Singh Tanwar
a[0] a[1] a[2] a[3] a[4]
20
25 6 27 5
K
LB UB
15. Insertion in an Array
Two types of insertion are
possible:
Insertion at the end of array
Insertion in the middle of the array
By:Dr. Prakash Singh Tanwar
16. Insert an element in an
Array
Write a program to insert an element in
an array?
Where to insert?
What are the current elements in the array?
What value you want to insert?
How many elements are there in this array?
a[0] a[1] a[2] a[3] a[4]
10 20 30
By:Dr. Prakash Singh Tanwar
17. Insertion in an Array
Algorithm: (Insertion of an element ITEM into Kth position in a Linear
Array A with N elements)
By:Dr. Prakash Singh Tanwar
Input:
A: Array A ; N: Array A have N elements in it
ITEM :item to insert ; Pos: insert item at position Pos
Output:
Array A with item inserted at position index ‘Pos’
Algorithm
1. [Initialize Counter.] Set I = N.
2. Repeat Steps 3 and 4 while I >= Pos.
3. [Move Ith element downward] Set A[I+1] = A[I].
4. [Decrease Counter.] Set I = I-1.
[End of Step 2 loop]
5. [Insert element.] Set A[Pos] = ITEM.
6. [Reset N] N = N+1.
7. Exit
18. Insert an element in an
Array
Insert 15 at pos index=1, Size=3
a[0] a[1] a[2] a[3] a[4]
10 20 30
a[0] a[1] a[2] a[3] a[4]
10 20 30 30
a[0] a[1] a[2] a[3] a[4]
10 20 20 30
a[0] a[1] a[2] a[3] a[4]
10 15 20 30
A[I+1]=A[I]
A[I+1]=A[I]
Algorithm(for index starting from 1)
1. [Initialize Counter.] Set I = N.
2. Repeat Steps 3 and 4 while I >= Pos.
3. [Move Ith element downward] Set A[I+1] = A[I].
4. [Decrease Counter.] Set I = I-1.
[End of Step 2 loop]
5. [Insert element.] Set A[Pos] = ITEM.
6. [Reset N] N = N+1.
7. Exit
I
A[Pos]=ITEM
Algorithm (for index starting from 0 and pos is also in the
form of index)
1. [Initialize Counter.] Set I = N-1.
2. Repeat Steps 3 and 4 while I >=Pos.
3. [Move Ith element downward] Set A[I+1] = A[I].
4. [Decrease Counter.] Set I = I-1.
[End of Step 2 loop]
5. [Insert element.] Set A[Pos] = ITEM.
6. [Reset N] N = N+1.
7. Exit
By:Dr. Prakash Singh Tanwar
19. Insert an element in an
Array
int insert(int a[], int n, int key, int pos)
{
int i;
//shift the elements to right
for(i=n-1;i>=pos; i--)
{
a[i+1]=a[i];
}
//insert the element
a[pos]=key;
//increase the size
n++;
return n;
}
//display array
printf("n After insertion in arrayn");
for(i=0;i<size;i++)
{
printf("%dt",a[i]);
}
By:Dr. Prakash Singh Tanwar
cout<<"n After insertion in array“<<endl;
cout<<a[i]<<“t”;
int main(void)
{
int arr[6] = { 10,20,30 };
int n = 3;
int key=15;
int pos=1;
//insert element key at given pos
n=insert(arr,n,key,pos);
//display array
display(arr,n);
return 0;
}
20. Linear Search
Write a program to search an element in
an array?
What are the current elements in the array?
What value you want to search?
By:Dr. Prakash Singh
Tanwar
a[0] a[1] a[2] a[3] a[4]
10 8 30 5 60
21. Linear Search
(un-ordered data)
Input:
Array a[] with size n
Key to search
Output :
pos index of key element in the array. If not found then return -1
Algo:
Start searching the element from the left most position to the end of an array.
• If key element is found the return the position index
• Otherwise repeat the step 1 for next position
If it is not found in all position index of array then
• return not found
By:Dr. Prakash Singh
Tanwar
22. Linear Search
(un-ordered data)
Search key=15
By:Dr. Prakash Singh
Tanwar
a[0] a[1] a[2] a[3] a[4]
10 8 30 5 60
i=0
i=0
a[i]==key
Not
matched
i=1
i=1
Not
matched
i=2
i=2
Not
matched
i=3
i=3
Not
matched
i=4
i=4
Not
matched
i=5 (out)
i=5 (out)
Key=15
23. Linear Search
(un-ordered data)
Search key= 30
a[0] a[1] a[2] a[3] a[4]
10 8 30 5 60
i=0
i=0
a[i]==key
a[i]==30
Not
matched
i=1
i=1
Not
matched
i=2
i=2
Matched
30 is found at pos i=2
Key=30
By:Dr. Prakash Singh Tanwar
24. Linear Search
(un-ordered data)
// linear search for unordered data
int linearSearch(int a[], int size, int key)
{
int i;
//check each element to search the key
for(i=0; i < size ; i++)
{
//if key is found in array
if( a[i] == key )
{
return i; // then return the pos i
}
}
//if not found then return -1
return -1;
}
By:Dr. Prakash Singh Tanwar
25. Linear Search
(Sorted Array)
// linear search for ordered data
int linearSearch(int a[], int size, int key)
{
int i;
//check each element to search the key
for(i=0; i<size && a[i]<=key ;i++)
{
//if key is found in array
if( a[i] == key )
{
return i;// then return the pos i
}
}
//if not found then return -1
return -1;
}
By:Dr. Prakash Singh Tanwar
26. Linear Search
(Sorted Array)
Linear Search (sorted array)
a[0] a[1] a[2] a[3] a[4]
10 20 30 40 50
i=0
i=0 i=1
i=1
key= 30
Key not
matched
30<10
=false
Key not
matched
30<20
=false
10==30 20==30
i=2
i=2
Key
matched
30==30
Try next
element
Try next
element
30 found at
pos i=2
By:Dr. Prakash Singh Tanwar
27. Linear Search
(Sorted Array)
Linear Search (sorted array)
a[0] a[1] a[2] a[3] a[4]
10 20 30 40 50
i=0
i=0 i=1
i=1
key= 25
Key not
matched
25<10
=false
Key not
matched
25<20
=false
10==25 20==25
i=2
i=2
Key not
matched
30==25
Try next
element
Try next
element 25 not found in
sorted array
25<30
=true
Come
out from
loop
Where the control will move
from this break statement?
By:Dr. Prakash Singh Tanwar
28. Q/A
Where the control will move from this break statement?
A. Out side of loop
B. come out from if
C. Come out from else if
D. move to increment section
By:Dr. Prakash Singh Tanwar
29. Linear Search
(Sorted Array)
// linear search for ordered data
int linearSearch(int a[], int size, int key)
{
int i;
//check each element to search the key
for(i=0; i<size && a[i]<=key ;i++)
{
//if key is found in array
if( a[i] == key )
{
return i;// then return the pos i
}
}
//if not found then return -1
return -1;
}
Both program works same
Both program works same
Opposite
condition
By:Dr. Prakash Singh Tanwar
30. Q/A
It can search sorted and unsorted elements both
best case : key is found at the first element (index=0):
Ω (1)
worst case: key element is found at last position n
(index=n-1) : O(n)
By:Dr. Prakash Singh Tanwar
31. Q/A
What is the best case search order for
linear search?
A. Ω(1)
B. O(n)
C.O(n2)
D. Ω(n)
By:Dr. Prakash Singh Tanwar
32. Q/A
What is the best case search order for
linear search?
A. Ω(1)
B. O(n)
C.O(n2)
D. Ω(n)
By:Dr. Prakash Singh Tanwar
33. Q/A
What is the worst case search order for
linear search?
A. Ω(1)
B. O(n)
C.O(n2)
D. Ω(n)
By:Dr. Prakash Singh Tanwar
34. Q/A
What is the worst case search order for
linear search?
A. O(1)
B. O(n)
C.O(n2)
D. Ω(n)
By:Dr. Prakash Singh Tanwar
35. Q/A
Which of the program is correct for linear search algorithm?
A B C D
int index = -1;
int i = 0;
while(size > 0)
{
if(a[i] == key)
{
index = i;
}
if(a[i] > key))
{
index = i;
break;
}
i++;
}
return index;
int index = -1;
int i = 0;
while(size > 0)
{
if(a[i] == key)
{
index = i;
break;
}
if(a[i] > key))
{
break;
}
i++;
}
return index;
int index = -1;
int i = 0;
while(size > 0)
{
if(a[i] == key)
{
index=i;
break;
}
if(a[i] > key))
{
index = i;
}
i++;
}
return index;
int index = -1;
int i = 0;
while(size > 0)
{
if(a[i] == key)
{
break;
}
if(a[i] > key))
{
break;
index = i;
}
i++;
}
return index;
By:Dr. Prakash Singh Tanwar
36. Q/A
Write a program to delete an element
from an array?
What are the current elements in the array?
What you wants to delete?
How many elements are there in this array?
a[0] a[1] a[2] a[3] a[4]
10 20 30
By:Dr. Prakash Singh Tanwar
37. Delete Element of an
array
Delete 10 , Size=3
Search 10 in this array using search algo.
a[0] a[1] a[2] a[3] a[4]
10 20 30
a[0] a[1] a[2] a[3] a[4]
20 20 30
a[0] a[1] a[2] a[3] a[4]
20 30 30
a[0] a[1] a[2] a[3] a[4]
20 30
Size--
a[0]=a[1]
a[1]=a[2]
posToDelete=0
By:Dr. Prakash Singh Tanwar
38. Delete Element of an
array
By:Dr. Prakash Singh Tanwar
Input: Pointer to an array a[], size of array, Key to delete
Output: array with deleted item and
size of array
Step1: posToDelete =search the element in the array and get the position
to delete
Step 2: if key not found
display message
Step 3: Otherwise (if key found in array)
replace the elements from posToDelete to the size of array
Step 3(a): repeat the step from last element to the posToDelete
Shift elements to the left hand side a[i]=a[i+1];
Step 3(b): decrease the size by one
size--;
Step 4 return the size
39. Delete Element of an
array
By:Dr. Prakash Singh Tanwar
cout<<“Element ” <<key<<“ not fount in array”<<endl;
cout<<“Before deletion of an array”<<endl;
cout<<“Which element you want to remove?”<<endl;
cin>> key;
cout<<“After deletion from array”<<endl;
40. Binary Search
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
mid=(9+0)/2=4 Mid=4
Mid=4
Key=60
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
Mid=(5+9)/2=7
Mid+1
Mid-1
Mid-1
Mid=(5+6)/2=5
Left=0
Left=0 Right=9
Right=9
key not found
key not found
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
Mid=7
Mid=7 Mid+1
Mid+1
Mid-1
Mid-1
Left
=mid+1
Right
Right
key not found
key not found
Mid=5
Mid=5
key found at
pos 5
key found at
pos 5
Right
=mid-1
By:Dr. Prakash Singh Tanwar
41. Binary Search
Algorithm
Step 1: calculate the mid position
Step 2: If the key to search is found at mid then return the mid pos
Step 3: If key < a[mid] value, then it is in left side.
so, recursively find it in left side from left to mid - 1
Step 4: otherwise , then it is in right side.
so, recursively find it in right side i.e. from mid + 1 to right
Step 5: if it is not found in left and right then return -1 (not found)
By:Dr. Prakash Singh Tanwar
42. Binary Search
cout<<“key is not found in array” <<endl;
cout<<“key is found at index ”<<pos<<endl;
By:Dr. Prakash Singh Tanwar
43. Binary Search
cout<<“key is not found in array” <<endl;
cout<<“key is found at index ”<<pos<<endl;
By:Dr. Prakash Singh Tanwar
44. Binary Search-Algo-2
Algorithm
1. BINARY ( DATA, LB, UB, ITEM, LOC )
2. [Initialize Segment Variables]
Set BEG = LB, END = UB and MID = INT ((BEG+END)/2).
3. Repeat Steps 3 and 4 while BEG <= END and DATA [MID] != ITEM.
4. If ITEM < DATA[MID], then:
Set END = MID - 1.
Else:
Set BEG = MID + 1.
[End of if Structure.]
5. Set MID = INT ((BEG+END)/2).
[End of Step 2 Loop.]
6. If DATA [MID] = ITEM, then: Set LOC= MID
Else:
Set LOC = NULL.
[End of if structure.]
7. Exit.
By:Dr. Prakash Singh Tanwar
45. Binary Search
Binary search order is
O(log n )
If n=1024 then it will take max.
log2(1024)=log2(210) =10 steps
1024512256128643216
8421
By:Dr. Prakash Singh Tanwar
46. Q/A
If there are 1000 elements in an array. Then
how many maximum steps are needed to
search an element in an array using binary
search ?
A. 9
B. 1000
C. 999
D. 10
By:Dr. Prakash Singh Tanwar
47. Limitations of
Binary Search
Although the complexity of Binary Search is
O (log n), it has some limitations:
The list must be sorted
One must have direct access to the middle element in
any sublist.
By:Dr. Prakash Singh Tanwar