SlideShare a Scribd company logo
1 of 47
Download to read offline
Data Structures & Algorithms
By
Dr. Prakash Singh Tanwar
Associate Professor
Lovely Professional University, Punjab
Topic: Linear Arrays Memory
Representations
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
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
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
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
Revision: 1D Array
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
By:Dr. Prakash Singh Tanwar
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
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
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
1D Array Memory
Representation
Array declaration
Size of char
• 1 byte
Ex
• Array Size
4 characters
• Subscript
0 to 3
n[0] n[1] n[2] n[3]
char n[4];
5001 5002 5003 5004
size of array
By:Dr. Prakash Singh Tanwar
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
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
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
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
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
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
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
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
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;
}
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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;
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
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
Binary Search
cout<<“key is not found in array” <<endl;
cout<<“key is found at index ”<<pos<<endl;
By:Dr. Prakash Singh Tanwar
Binary Search
cout<<“key is not found in array” <<endl;
cout<<“key is found at index ”<<pos<<endl;
By:Dr. Prakash Singh Tanwar
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
Binary Search
Binary search order is
O(log n )
If n=1024 then it will take max.
log2(1024)=log2(210) =10 steps
1024512256128643216
8421
By:Dr. Prakash Singh Tanwar
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
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

More Related Content

What's hot

BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structurekalyanineve
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Array operations
Array operationsArray operations
Array operationsZAFAR444
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Raj Naik
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked ListNinad Mankar
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked listKeval Bhogayata
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
 
Unit 5 internal sorting &amp; files
Unit 5  internal sorting &amp; filesUnit 5  internal sorting &amp; files
Unit 5 internal sorting &amp; filesDrkhanchanaR
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queueSrajan Shukla
 

What's hot (20)

Arrays
ArraysArrays
Arrays
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
stack & queue
stack & queuestack & queue
stack & queue
 
Anomalies in database
Anomalies in databaseAnomalies in database
Anomalies in database
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Queue ppt
Queue pptQueue ppt
Queue ppt
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Array operations
Array operationsArray operations
Array operations
 
Expression trees
Expression treesExpression trees
Expression trees
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive
 
Data structures
Data structuresData structures
Data structures
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Insertion in singly linked list
Insertion in singly linked listInsertion in singly linked list
Insertion in singly linked list
 
Linked lists
Linked listsLinked lists
Linked lists
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Unit 5 internal sorting &amp; files
Unit 5  internal sorting &amp; filesUnit 5  internal sorting &amp; files
Unit 5 internal sorting &amp; files
 
Ppt on Linked list,stack,queue
Ppt on Linked list,stack,queuePpt on Linked list,stack,queue
Ppt on Linked list,stack,queue
 

Similar to 03 Linear Arrays Memory Representations .pdf

ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptxabhishekmaurya102515
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsAbdullah Al-hazmy
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxDEEPAK948083
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfMustafaJutt4
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrmsMisssaxena
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptxchin463670
 
Array 2
Array 2Array 2
Array 2Abbott
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinaryPrashant Rai
 

Similar to 03 Linear Arrays Memory Representations .pdf (20)

ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithms
 
Sorting
SortingSorting
Sorting
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
Searching
SearchingSearching
Searching
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptxdatastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
 
Chap10
Chap10Chap10
Chap10
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
DS Unit 1.pptx
DS Unit 1.pptxDS Unit 1.pptx
DS Unit 1.pptx
 
Lecture3b searching
Lecture3b searchingLecture3b searching
Lecture3b searching
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Array 2
Array 2Array 2
Array 2
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
 
search_sort.ppt
search_sort.pptsearch_sort.ppt
search_sort.ppt
 

Recently uploaded

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 

Recently uploaded (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
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
  • 6. Revision: 1D Array 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 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
  • 10. 1D Array Memory Representation Array declaration Size of char • 1 byte Ex • Array Size 4 characters • Subscript 0 to 3 n[0] n[1] n[2] n[3] char n[4]; 5001 5002 5003 5004 size of array 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 1024512256128643216 8421 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