0% found this document useful (0 votes)
7 views

Lesson 04

The document provides an overview of arrays for a data structures and algorithms course. It defines what an array is, how arrays are represented in memory, basic operations on arrays like traversal, insertion, deletion, and updating elements. It discusses array implementation in C++ and includes pseudocode for insertion and deletion algorithms. The objectives are to understand array representation and usage, as well as how to perform common operations. Students are assigned to create a program implementing insertion and deletion on arrays. The next topic will be sorting.

Uploaded by

omer salim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lesson 04

The document provides an overview of arrays for a data structures and algorithms course. It defines what an array is, how arrays are represented in memory, basic operations on arrays like traversal, insertion, deletion, and updating elements. It discusses array implementation in C++ and includes pseudocode for insertion and deletion algorithms. The objectives are to understand array representation and usage, as well as how to perform common operations. Students are assigned to create a program implementing insertion and deletion on arrays. The next topic will be sorting.

Uploaded by

omer salim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Jahan University

Vice Chancellor office


Computer Science faculty
IS/IT Department

Course:Data Structure and Algorithms

Chapter 3 - Lesson 4 Array


Semester: 8th Instructor : Ziaur Rahman
Lecture: Khalilullah Khalid
Computer Science Faculty
Prepare by: Khalilullah Khalid year: 1402
JAHAN UNIVERSITY
Contents
• Introduction to Array
• Representation of Array
• Basic Operations on Array
• Implementation in C++
• Array traversing
• Array insertion and deletion
Objective & Usage in the Organizations
• To be able to use Array in algorithms and
programming.
• Working with Representation of Array.
• Be able to work with basic operations on Array.
• To be able to work with Array Implementation in
programming.
• To be able to work with Array traversing, Array
insertion and deletion
Arrays
❑ An array is a variable which can store multiple values of same data type at a time.
❑ Collection of similar data items stored in continuous memory locations with single
name

int a[3];

1
Arrays
❑ Element − Each item stored in an array is called an element.
❑ Index − Each location of an element in an array has a numerical index, which is used to
identify the element.

int a[3];

2
Array Representation
❑ Arrays can be declared in various ways in different languages. For illustration, let's
take C++ array declaration.

3
Array Representation
❑ As per the below illustration, following are the important points to be considered.
❑ Index starts with 0.
❑ Array length is 10 which means it can store 10 elements.
❑ Each element can be accessed via its index. For example, we can fetch an
element at index 6 as 19.

4
Basic Operations on Arrays
Following are the basic operations supported by an array.
❑ Traverse − print all the array elements one by one.
❑ Insertion − Adds an element at the given index.
❑ Deletion − Deletes an element at the given index.
❑ Search − Searches an element using the given index or by the value.
❑ Update − Updates an element at the given index.

6
Insertion
❑ Insertion refer to the process of adding a new element to an existing one
dimensional array.
❑ Insertion can be at
❑ Any random position within array
❑ The end of an array

❑ Inserting an element at any arbitrary position, all the elements starting from the
specified position are shifted towards right to make a vacant position for new
insertion.
❑ Insertion at the end is very easy process as there is no shifting of elements.

7
Method for Insertion Operation
❑ Insertion at any arbitrary position
❑ Lets take array of size 6 elements.

00 11 22 33 44 65
10 20 30 40

❑ Suppose, we want to insert a new element 35 at 2 location of array, then shifting of


elements is required to vacant the location 2.
❑ Now First of all , shift the element 40 to location 4

8
Cont...
0 1 2 3 4 5
10 20 30 40

❑Next shift element 30 to location 3.

0 1 2 3 4 5
10 20 30 40

Vacant for insertion

❑ So, here we got the vacant location to insert element 35.


9
Method for Insertion Operation

❑ Insertion at any arbitrary position


❑ Lets take array of size 5elements.

0 1 2 3 4
25 35 40

❑ If we want to insert element 45 at the end of this array, then there is no


need of shifting of elements.
0 1 2 3 4
25 35 40 45

Inserted here
10
Algorithm for Insertion Operation
Step 1: Start
Step 2: i  n-1
Step 3: Repeat steps 4 and 5 while i >= k
Step 4: a[i+1]  a[i]
Step 5: ii -1
[end of while loop]
Step 6: a[k]  item
Step 7: n n+1
Step 8: end
Note: n denotes the size of an array, k denotes the position of new element for insertion.
11
Deletion
❑ Deletion refers to a process of removing an existing element from an array.
❑ To delete an element, first search it, using a searching technique.
❑ After locating the specified element, remove it from array.
❑ When the specified element is removed then shift upwards all the elements to
maintain the order of an array.
❑ If the specified element for deletion is the last element, then it is very easy to
delete to as there is no need of shifting of remaining element.

12
Method for Deletion Operation
❑ Lets take array of size element.

0 1 2 3 4 5
10 20 30 40 50 60

❑ Suppose, we want to delete element 40 from of an array.


❑ First of all, Searching operation is performed to locate the element 40.

13
Method for Deletion Operation
❑ When Search method locate the element 40 at index 3.
❑ Now delete the element 40 from array in such a way that the element at index 4
shifted to index 4 shifted to index 3 and element at index 5 shifted to index 4 so on.

0 1 2 3 4 5
10 20 30 50 60

Element 40 is deleted and 50 shifted to index 3

14
Cont...
❑ Now to maintain the order of elements in an array, element 60 shift to index 4.

0 1 2 3 4 5
10 20 30 50 60

Element 40 is deleted from array and


remaining elements shift upwards.

15
Algorithm for Deletion Operation

Step 1: start
Step 2: Repeat for i=k to n-1
Step 3: a[i]  a[i + 1 ]
[end of for loop]

Step 4: n  n-1
Step 5: end

16
Advantages of Array

❑Here are the advantages of arrays:


❑Very efficient, in terms of both time and space

❑Convenient syntax

❑Accessing an element by its index is very fast (constant time)

20
17
Disadvantage of Array

❑Here are the disadvantages of arrays:


❑All elements must be of the same type

❑The array size is fixed and can never be changed

❑Insertion into arrays and deletion from arrays is very slow

❑wasting memory if not fully used

21
18
Assignment
• Create a Program to Implement Insertion and Deletion operation on
array.

• Submission date : next class


Summery & Students Evaluation
• Introduction to Array

• Representation of Array

• Basic Operations on Array

• Implementation in C++
Any Questions?
Next Topic

Sorting

25

You might also like