0% found this document useful (0 votes)
45 views6 pages

Dsa 03

The document discusses algorithms for inserting and deleting elements from linear arrays. It provides pseudocode for inserting an element into a sorted or unsorted array, and for deleting an element from a sorted array. It also assigns a task to write a program implementing insertion and deletion on arrays based on the given algorithms.

Uploaded by

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

Dsa 03

The document discusses algorithms for inserting and deleting elements from linear arrays. It provides pseudocode for inserting an element into a sorted or unsorted array, and for deleting an element from a sorted array. It also assigns a task to write a program implementing insertion and deletion on arrays based on the given algorithms.

Uploaded by

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

University of Engineering and Technology, Taxila

Data Structures and Algorithms

Experiment 3
Arrays : Implementation of insert and delete operations

CLO-1: Construct the experiments/projects of varying complexities

CLO-2: Use modern tools and languages.

CLO-3: Demonstrate an original solution of problem under discussion.

CLO-4: Work individually as well as in teams


University of Engineering and Technology, Taxila 1
Array Operations

INSERT Operation in Arrays:


Sorted Array:

AIM: Write a program to Insert an Item into Linear Array:

Description: Inserting refers to the addition of a new element in an array, say ‘A’. Inserting an
element at the end of the array can be easily done. On the other hand, if an element is to be
inserted at the beginning or in the middle of array ‘A’, then the elements (from insertions point
inclusively) must be moved downward to new locations, to accommodate the new element.
Assume, values inside the ‘A’ are in sorted order and it is needed to insert the new item at such
location in ‘A’ so that the sorted order of ‘A’ should not be disturbed.
See the algorithm:
The following set of algorithms inserts a data element ITEM at Kth position in a linear
array. Here A is a Linear Array with SIZE locations, N is the number of total values stored in
A and K is a positive integer (location where to insert) such that K<= N.
University of Engineering and Technology, Taxila 2
This algorithm inserts an element ITEM into the Kth position in A.

Algorithm:(part 1)
1. if N = SIZE then write: “Overflow, Array is Full. “ and End
2. Get ITEM from user to be inserted in array
[ Find location in sorted array ]
3. Call Find_Location_To_Insert (A, N, K, ITEM)
[ Above algorithm finds the location K where to insert]
4. Call INSERT (A, N, K, ITEM)
5. End

Algorithm:(part 2)
Find_Location_To_Insert(A, N, K, ITEM)
1. LB = 0 and UB = N – 1
2. Repeat Step-3 for K = LB to UB.

3. If A[K] > ITEM then return K. [ Insertion at front or in middle]


[End of Step 2 loop.]
4. return K. [ Insertion at the end of array ]
5. End.

Algorithm: (part 3)
INSERT (A, N, K, ITEM)
1. Set J = N - 1. [Initialize counter.]
2. Repeat Step-3 and 4 while J≥K.
3. Set A[J+1] = A[J]. [Move Jth element downward.]
4. Set J = J-1. [Decrease counter.]
[End of Step 2 loop.]
5. Set A[K] = ITEM. [Insert element.]
6. Set N = N+1. [Reset N.]

7. End.
University of Engineering and Technology, Taxila 3
Unsorted Array:
In an unsorted array, the insert operation is faster as compared to sorted array because we do
not have to care about the position at which the element is to be placed.

Delete Operation:
In delete operation, the element to be deleted is searched using the linear search and then
delete operation is performed followed by shifting the elements. Element might be present at
start, end or middle of array.
University of Engineering and Technology, Taxila 4
AIM: Write a program to Delete an Item from Linear Array:

Description: Deletion refers to the operation of removing one of the element from the array
‘A’. Deleting an item at the end of the array presents no difficulties. But, deleting an item
from beginning or somewhere in the middle of array would require that each subsequent
element be moved one location upward in order to fill up the array.

The following algorithm deletes the Kth element from a linear array A with N values stored
in it. Here A is a Linear Array with SIZE locations, N is the number of total values stored
in A and K is a positive integer such that K<=N. This algorithm deletes the Kth element
from A.

Algorithm:part 1
1. If N = 0 then write: “Underflow. Array is Empty. “ and End
2. Get ITEM from user to be deleted from array
[ Find location in sorted array ]
3. Call Find_Location_To_Delete(A, N, K, ITEM)
4. If found Call DELETE (A, N, K, ITEM) [ if K = -1 then not found ]
Else write: “Not Found in array”
5. End

Algorithm: part 2
Find_Location_To_Delete(A, N, K, ITEM)
1. Repeat Step 2 for I = LB to UB.
2. If A[I] = ITEM then K = I and return K. [ Deletion from front or from middle]
[End of Step 2 loop.]
3. K = -1 and return K. [ITEM Not found in array ]

4. End.

Algorithm: part 3
DELETE (A, N, K, ITEM)
1. Set ITEM = A[K].
2. Repeat for J=K to N-1:
Set A[J] = A[J+1] [Move J + 1 element upward.].
[End of loop.]
3. Set N = N-1. [Reset the number “N” of element in A.]
4. End.
University of Engineering and Technology, Taxila 5
Task 1: Write a program, which perform insertion as well as deletion on array elements
(User’s choice) by using above-mentioned algorithms.

Home task In-Place merge two Sorted arrays:

Given two sorted arrays A[] and B[] of size m and n, merge elements of A[]
with elements of array b[] by maintaining the sorted order i.e. fill A[] with
smallest elements and fill b[] with remaining elements

A[]={1,4,6,9,11}
B[]={0,2,5,7}

Output:
A[]={0,1,2,4,5}
B[]={6,7,9,11}

You might also like