0% found this document useful (0 votes)
51 views19 pages

Data Structures Using C++: Ms - Akhila Shaji Assistant Professor SSM College, Rajakkad

This document discusses various operations that can be performed on arrays, including insertion, deletion, traversal, merging, sorting, and searching. It provides algorithms and examples for inserting and deleting elements at specific positions in an array. It also covers traversing an array by accessing each element once, and merging two sorted arrays into a new single sorted array. The key operations allow manipulating and accessing data stored in array data structures.

Uploaded by

Akhila Shaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views19 pages

Data Structures Using C++: Ms - Akhila Shaji Assistant Professor SSM College, Rajakkad

This document discusses various operations that can be performed on arrays, including insertion, deletion, traversal, merging, sorting, and searching. It provides algorithms and examples for inserting and deleting elements at specific positions in an array. It also covers traversing an array by accessing each element once, and merging two sorted arrays into a new single sorted array. The key operations allow manipulating and accessing data stored in array data structures.

Uploaded by

Akhila Shaji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Data Structures

Using C++

Ms.Akhila Shaji
Assistant Professor
SSM College,Rajakkad
Array operations
•There are several operations that can be
performed on an array. They are
Insertion
Deletion
Traversal
Merging
Sorting
Searching
Insertion operation
Insertion of a new element in an array can be done in
two ways:
i. Insertion at the end of the array
Inserting an element at the end of an array can be easily
done provided the memory space allocated for the array
is large enough to accommodate the additional element.
for eg: Insert15 at the end of the array.
int a[5]={11,13,14,4};
a[0] a[1] a[2] a[3] a[4]
Before insertion
11 13 14 4 0
After insertion
11 13 14 4 15
ii. Insertion at required position
• For inserting the element at required position,
elements must shifted one place to the right(down)
of their current position from the specified
position.
•Then we have placed the new element at vacant
position.
•The shifting is to be done to accommodate the
new element and keep the order of the elements.
For eg:insert 12 in 2nd position in the array
int a[5]={11,13,14,4};
a[0] a[1] a[2] a[3] a[4]
Before Insertion 11 13 14 4 0

Shift one position to the right(down) from 2nd position

11 13 14 4 0

After shifting 11 13 14 4
In the array 2nd position becomes vacant for insertion
After insertion
11 12 13 14 4
Insertion Algorithm
Insert(a,len,pos,num)
//a is a linear array with len elements
//pos is the position at which number num will be
inserted.
1. [ Initialize the value of i ] Set i = len
2. Repeat for i = len down to pos //for(i=len;i>=pos;i--)
[shift the elements down(right) by 1 position]
set a[i+1]=a[i]
[end of loop]
3. [Insert the element at required position]
set a[pos] = num
4. [ Reset len] Set len=len+1
5. Display the new list of arrays
6. End
Deletion operation
i. Deletion at the end of the array
Deleting an element at the end of an array can be
easily done. Deleting element from the end means just
remove the end element from the array and decrement
the array size.
for eg: Delete15 from the end of the array.
int a[5]={11,13,14,4,15};
a[0] a[1] a[2] a[3] a[4]
Before Deletion 11 13 14 4 15

After Deletion 11 13 14 4
ii. Deletion at required position

• For Deleting the element at required position,


elements must shifted one place to the left of their
current position from the deleted position in order
to fill the space emptied by the deletion of the
element .
• Decrement the size of the array
For eg:delete 12 in 2nd position in the array
int a[5]={11,13,14,4};
a[0] a[1] a[2] a[3] a[4]
Before Deletion 11 12 13 14 4

11 13 14 4
After Deletion
In the array 2nd position becomes vacant after
deletion. Shift one position to the left from 3rd position
After shifting 11 13 14 4
Deletion Algorithm
Delete(a,pos,n)
//a is a linear array with n elements
//pos is the position at which element will be deleted.
1. Set item=a[pos]
2. Repeat for j = pos to n-1 //for(j=pos;j<=n-1;j++)
[shift the elements up(left) by 1 position]
set a[j]=a[j+1]
[end of loop]
3. [ Reset n] Set n=n-1
4. Display the new list of arrays
5. End
Traversing Linear Arrays

 Traversing is accessing and processing ( visiting ) each


element of the data structure exactly ones

Linear Array
•••
Traversing Linear Arrays

 Traversing is accessing and processing (visiting ) each


element of the data structure exactly ones

Linear Array
•••
13 Traversing Linear Arrays

 Traversing is accessing and processing (visiting ) each


element of the data structure exactly ones

Linear Array
•••
14 Traversing Linear Arrays

 Traversing is accessing and processing (visiting ) each


element of the data structure exactly ones

Linear Array
•••
15 Traversing Linear Arrays

 Traversing is accessing and processing (visiting ) each


element of the data structure exactly ones

Linear Array
•••
16 Traversing Linear Arrays

 Traversing is accessing and processing (visiting ) each


element of the data structure exactly ones

Linear Array
•••
Traversing of Arrays

• Traversing is accessing or processing(visiting)


each element of the array exactly once starting
from first element to the last element.
Algorithm
//Let LB be the lower bound and UB be the upper
bound of the array a
1. [Initialize counter ] set i at lower bound LB
2. Repeat for i=LB to UB
[visit element] Display a[i]
[End of the loop]
3. Exit
Merging two Arrays
• Merging means combining elements of two
arrays to form a new array.
• Simplest way is ,first copy all elements of one
array into third empty array, and then copy all
the elements of other array into third array.
• The elements of the second array are copied into
third array after the position at which the last
elements of the first array are copied.
• If you want the array to be sorted, you can sort
the resultant array.
• Another way is to sort the array during merging .
This is requires sorting of resultant array even if
the two sources arrays are sorted beforehand.
Algorithm
1. Create three arrays array1[100],array2[100],array3[200]
2. Enter the element p and q in ascending order in array1 and
array2
3. Initialize m and n(lower bounds to 0)
4. Check if (array1[m]<=arra2[n])
[assign elements of array1 to array3]
array3[c]=array1[m++] set c=c+1
else
[assign elements of array2 to array3]
array3[c]=array2[n++] set c=c+1 [end of if]
5. Assign remaining elements of array1 and array2 to array3
6.Display the elements of array3
7.End

You might also like