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

1 Array

Uploaded by

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

1 Array

Uploaded by

Lakruwan Kasun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

DATA STRUCTURES AND

ALGORITHMS-1 (CS)
M.G.Noel A.S Fernando (PhD)
Professor
Head, Dept. of Information Systems Engineering, UCSC

Lecture 1
What is data structures?
What is data structures?
• Data Structure can be defined as the group of
data elements which provides an efficient
way of storing and organizing data in the
computer so that it can be used efficiently.
• Some examples of Data Structures are arrays,
Linked List, Stack, Queue, etc.
What is data structures?
• Data Structures are widely used in almost
every aspect of Computer Science
• i.e. Operating System, Compiler Design,
Artificial intelligence, Graphics and many
more.
Different types of Data Structures
Categories of data structure
• Data structures can be subdivided into two major types.
• Linear Data structure
• Non-Linear Data structure

• Linear Data structure : A data structure is said to be linear if


its elements combine to form any specific order.
• E.g. Arrays and Linked List.

• Non-Linear Data structure : This structure mainly represents


data with a hierarchical relationship between different
elements.
• E.g. Trees and Graphs
Difference between Linear and Non-
linear Data Structures
Properties Linear and Non-linear Data
Structures
Arrays
• Variable: Placeholder/Structure to store
a basic unit of data.
• Example:
• One integer variable stores single integer
data.
• One float variable stores single float data.
Arrays
• Question: In a computer, where is that
value assigned to a variable stored?
• Answer: In memory, possibly Random-Access Memory (RAM)

• Question : But where exactly in memory?

• Answer: On some physical address in memory because,


Memory is divided in physical locations and,
Each location has a particular address associated
with it.

Array
• Variable: –Sometimes in our program, we
need to store multiple data of same type
• Example:
• index numbers of 10 students.
• Marks of DSA of 10 students.
Arrays
Two methodologies can be used:

1: Create different variables each having a different


name.
E.g. int num1, num2, num3 etc.

2: Create a collection of variables referred by a common


name.
E.g. int num[3]

This looks much better solution.


Array Data Structures
• Array is a container which can hold a fix number of
items and these items should be of the same type.
• Most of the data structures make use of arrays to
implement their algorithms.
• Following are the important terms to understand the
concept of Array.
• 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.
Arrays
Array Representation
Arrays
Array: Finite, Ordered collection
of Homogeneous data elements where,
Ordering is maintained by storing all
elements in continuous / contiguous
locations of memory.
Arrays
Properties:
Finite: Contains only a limited (finite) number of elements.
Ordered: All elements are stored one by one in continuous /
contiguous locations of computer memory.
Homogeneous: All the elements of an array are of the same data
type.

Example:
An array of integers to store the integer numbers of all students in
a class.
An array of strings to store the names of all villagers in a village.
Basic Operations

Following are the basic operations supported by an


array.
• Traverse − print/visit 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.
Array Structure cont.…
• Consider a situation in which we have 20
students in a class, and we have been asked to
write a program that reads and prints the
marks of all the 20 students.
• In this program, we will need 20 integer
variables with different names, as shown in
Fig. 1.1
Figure 1.1
Array Structure cont.…
• If it is just a matter of 20 variables, then it might be
acceptable for the user to follow this approach.
• But would it be possible to follow this approach if we
have to read and print the marks of students,
• in the entire course (say 100 students)
• in the entire college (say 500 students)
• in the entire university (say 10,000 students)
• The answer is no, definitely not! To process a large
amount of data,
• we need a data structure known as array.
Array Structure cont.…
• An array is a collection of similar data elements.
• These data elements have the same data type.
• The elements of the array are stored in
consecutive memory locations and are
referenced by an index (also known as the
subscript).
• The subscript is an ordinal number which is used
to identify an element of the array
DECLARATION OF ARRAYS
• An array must be declared before being used.
• Declaring an array means specifying the
following:
• Data type—the kind of values it can store, for
example, int, char, float, double.
• Name—to identify the array.
• Size—the maximum number of values that the array
can hold.
DECLARATION OF ARRAYS cont..
• Arrays are declared using the following syntax:
type name[size];
• The type can be either int, float, double, char, or any
other valid data type.
• Example
int marks[10];
• then the statement declares marks to be an array
containing 10 elements.
• In C, the array index starts from zero. The first element
will be stored in marks[0], second element in marks[1],
and so on.
Figure 2.2: Memory representation of
an array elements
Figure 2.3 Declaring arrays of different
data types and sizes
ACCESSING THE ELEMENTS OF AN
ARRAY
• Code 1 : initialization of the array.

Array marks after executing the code given in the above.


OPERATIONS ON ARRAYS
• There are a number of operations that can be
preformed on arrays. These operations include:
• Traversing an array
• inserting an element in an array
• Searching an element in an array
• Deleting an element from an array
• Merging two arrays
• Sorting an array in ascending or descending order
Traversing an Array
• Traversing an array means accessing each and
every element of the array for a specific
purpose.
• Traversing the data elements of an array A can
include printing every element, counting the
total number of elements, or performing any
process on these elements.
Algorithm for array traversal
Write a program to read and display n
numbers using an array
Write a program to read and display n
numbers using an array
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The array elements are 1 2 3 4 5
Home work 1.1
• Write a program to find the mean of n
numbers using arrays.
• Write a program to print the position of the
smallest number of n numbers using arrays.
• Write a program to find the second largest of
n numbers using an array.
• Write a program to enter n number of digits.
Form a number using these digits.
Question: write a pseudocode
algorithm to Increment all the array
values by 1
Array Structure
• To insert or remove an element at an interior
location in an ArrayList requires shifting of
data and is an O(n) operation.
Insertion Algorithm
• Let LA be a Linear Array (unordered)
with N elements and K is a positive integer
such that K<=N.
• Following is the algorithm where ITEM is
inserted into the Kth position of array LA .
Insertion Algorithm
Setp1 : Start
Step 2: Set J=N
Step 3: Set N=N+1
Step 4 : Repeat Step 5 & 6 while J>=k
Step 5 : LA[J+1]=LA[j]
Step 6: J=J-1
Step 7 :LA[K]=ITEM
Step 8 Stop
Deletion Operation

• Deletion refers to removing an existing


element from the array and re-organizing all
elements of an array.
• Consider LA is a linear array with N elements
and K is a positive integer such that K<=N.
• Following is the algorithm to delete an
element available at the Kth position of LA.
Deletion Operation
• Step1 :Start
• Step 2 : Set J=k
• Step 3: Repeat Step 4 & 5 while J < N
• Step 4: Set LA[J]=LA[J+1]
• Step 5: Set J=J+1
• Step 6: Set N=N-1
• Step 7: Stop
Search Operation

• Consider LA is a linear array with N elements


and K is a positive integer such that K<=N.
• Following is the algorithm to find an element
with a value of ITEM using sequential search.
Search Operation
• Step 1:Start
• Step 2:Set J=0
• Step 3:Repeat Step 4 & 5 while J<N
• Step 4: If LA[J] = ITEM THEN GO TO Step 6
• Step 5:Set J=J+1
• Step 6:Print J, Item
• Step 7:Stop
Home work 1.2
• Write a program to find whether the array of
integers contains a duplicate number.
Inserting an Element in an Array
• If an element has to be inserted at the end of
an existing array, then the task of insertion is
quite simple.
Home work 1.3
Data[] is an array that is declared as int Data[20];
and contains the following values:
Data[] = {12, 23, 34, 45, 56, 67, 78, 89, 90, 100};
a) Calculate the length of the array.
(b) Find the upper_bound and lower_bound.
(c) Show the memory representation of the array.
(d) If a new data element with the value 75 has to
be inserted, find its position.
(e) Insert a new data element 75 and show the
memory representation after the insertion.
Solution
(a) Length of the array = number of elements, Therefore,
length of the array = 10
(b) By default, lower_bound = 0 and upper_bound = 9

(d) Since the elements of the array are stored in


ascending order, the new data element will be stored
after 67, i.e., at the 6th location. So, all the array
elements from the 6th position will be moved one
position towards the right to accommodate the new
value.
Algorithm to Insert an Element in the
Middle of an Array
The algorithm INSERT will be declared as INSERT
(A, N, POS, VAL). The arguments are:
(a) A, the array in which the element has to be
inserted
(b) N, the number of elements in the array
(c) POS, the position at which the element has
to be inserted
(d) VAL, the value that has to be inserted
Algorithm to insert an element in the
middle of an array
visualize the above algorithm by
taking an example.
Home work 1.4
• Write a program to insert a number at a given
location in an array
• Write a program to insert a number in an
array that is already sorted in ascending order.
Algorithm to delete an element from
the middle of an array
The algorithm DELETE will be declared as
DELETE(A, N,POS). The arguments are:
(a) A, the array from which the element has to
be deleted
(b) N, the number of elements in the array
(c) POS, the position from which the element
has to be deleted
Algorithm to delete an element from
the middle of an array
visualize this algorithm by
taking an example
• Calling DELETE (Data, 6, 2) will lead to the
following processing in the array.
Home work 2.5
• Write a program to delete a number from a
given location in an array.
• Write a program to delete a number from an
array that is already sorted in ascending order.
Merging Two Arrays
• Merging two arrays in a third array means first
copying the contents of the first array into the
third array and then copying the contents of
the second array into the third array.
Merging of two sorted arrays
Home work 1.6
• Write a program to merge two arrays
• Extend the above program merge any number
of arrays

You might also like