Arrays Data Structure
Arrays Data Structure
I
T
2
0 Arrays Data Structure
8
Lecture 2
I
T
ARRAYS
2
0
8
Introduction
Motivation
• You want to store 5 numbers in a program
I oNo problem. You define five int variables:
int num1, num2, num3, num4, num5;
T oEasy enough, right?
oBut what if you want to store 1000 numbers?
2 • Are you really going to make 1000 separate variables?
int num1, num2,..., num998, num999,
0 num1000;
• That would be CRAZY!
8 • So what is the solution?
oA data structure! Specifically, an array!
• An array is one of the most common data structures.
Array
• An array is one of the most common data structures
I • An Array is a collection of elements of the same data
type, stored at a contiguous memory location.
T
• An array is a data structure that is used to store multiple
2 values of similar data types in a contiguous memory
locations under one name.
0
• An array’s elements are accessed by using their
8 positions in the structure.
• Declaration of array
Datatype ArrayName[size];
• Example
4 o int data]; Dr. Belal Murshed
Data Structures and Algorithms
Basic Concepts
• Array name (data)
I
• Index/subscript (0...9)
T • The slots are numbered sequentially
2 starting at zero (Java, C++)
0 • If there are N slots in an array, the
8 index will be 0 through N-1
• Array length = N = 10
• Array size = N x Size of an element = 40
5 Dr. Belal Murshed
Data Structures and Algorithms
Using Arrays
• Array_name[index]
I
• For example, in C++
T ocout<<data[4];
2 • will display 0
0
odata[3] = 99;
8 • Will replace -3 with 99
Using Arrays
• data[ -1 ] What will be the output of?
I oillegal Data[8] = data[5] + 10
data[3] = data[3] + 20
T • data[ 10 ]
oillegal (10 > upper bound)
2 • data[ 1.5 ]
oillegal
0
• data[ 0 ]
8 oOK
• data[ 9 ]
oOK
7 Dr. Belal Murshed
Data Structures and Algorithms
Array Dimensionality
• One dimensional (just a linear list)
I
5 10 18 30 45 50 60 65 70 80
T oOnly one subscript is required to access an
2 individual element
0 • Two dimensional (matrix or table)
8 Col 0 Col 1 Col 2 Col 3
Row 0 20 25 60 40
Row 1 30 15 70 90
8 Dr. Belal Murshed
Data Structures and Algorithms
2D Arrays
• Given the following array (whose name is
I ‘M’) 20 25 60 40
T 30 15 70 90
2 oTwo indices/subscripts are now required to
0 reference a given cell
8 • M[0][0] ?
• M[1][2] ?
• M[3][4] ?
9 Dr. Belal Murshed
Data Structures and Algorithms
Representation of Array
• The representation of an array can be
I defined by its declaration.
T • A declaration means allocating memory for
2 an array of a given size.
0
8
Array
Remember: “Location of next index depends on the data
type we use”.
I o For example, in the next Figure the data type in the array
is char, that means each character has only one byte then
T o We notice that the memory location of the first element
2 is 200,
o and the memory location of the next element is 201 .
0
8
Array Declaration
• You declare an array as follows:
I Size=10;
int grades[Size];
T • This simply makes a an array variable (grades)
• array variable (grades) refers to an array of ten integers
2 • We can also declare the following:
int grades[10];
0 o Now the array variable grades refers to an array of ten integers
• By default, numeric elements are initialized to zero
8
Other example
int arr[5]; // This array will store integer type element
char arr[10]; // This array will store char type element
12 float arr[20]; // This array will store float type element
Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 ARRAY
0
As a Data Structure
8
What is an Array?
• An array is a data structure
I oAn array is a collection of items of same data type
stored at contiguous memory locations.
T oIt is a collection of multiple values of same type.
2 oExamples:
o An array of student grades
0 o An array of student names
o An array of objects (OOP perspective!)
8 • Assumption: Data in array is always sorted
• Array elements are initialized with “0”
16 Dr. Belal Murshed
Data Structures and Algorithms
Array Characteristics
• Homogeneity
I oAll elements in an array must have the same data
type
T • Contiguous Memory
2 oArray elements (or their references) are stored in
contiguous/consecutive memory locations
0 • Direct access to an element
oIndex reference is used to access it directly
8
• Static data structure
oAn array cannot grow or shrink during program
execution…the size is fixed
17 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 ARRAY
0
Operations
8
Array Operations
1. Accessing/indexing an element using its index
I o Performance is very fast
o We can access an index immediately without searching
T o myArray [1250] = 55;
o we immediately access array spot 1250 of myArray
2 void access(double a[] , int index) {
0 a[index] = a[index] + a[index] * 0.20;
8 Cout<<“Updated value “<< a[index]);
}
Array Operations
2. Traversing an array
I oDisplay all contents of an array
T • All elements will be displayed
• Every elements will be displayed exactly once
2
0 void display(int a[], int length)
{
8
for (int i=0; i<length; i++)
cout<<a[i]);
}
20 Dr. Belal Murshed
Data Structures and Algorithms
Array Operations
3. Insertion: add an element at a certain
I index
T oWhat if we want to add an element at the
2 beginning?
• What if we add an element at the end?
0 o It would be FAST. Why? No need to shift.
• This would be a very slow operation! Why?
8 o Because we would have to shift ALL other elements over
one position
Array Operations
(a) Insert operation in an unsorted array at the
I end :
T • In an unsorted array, the insert operation is
faster as compared to a sorted array because
2 we don’t have to care about the position at
which the element is to be placed.
0
8
Array Operations
(b) Insert an element at any position
I • Insert operation in an array at any position
can be performed by shifting elements to
T the right, which are on the right side of the
2 required position
0
8
Array Operations
• Deletion: remove an element at a certain
I index
T oRemove an element at the beginning of the
2 array
• Performance is again very slow.
0 o Because ALL elements need to shift one position
backwards
8 oRemove an element at the end of an array
• Very fast because of no shifting needed
Array Operations
(b) Delete Operation
I • In the delete operation, the element to be
deleted is searched using the (1)linear search,
T and then (2) the delete operation is performed
2 followed by(3) shifting the elements.
0
8
Delete Operation:
int deleteElement(int a[], int length, int value)
{
// Find position of element to be deleted
I int pos = findElement(a, length, value);
if (pos == -1) {
T cout << "Element not found";
length
return length;
2 }
0 // Deleting element
int i;
8 for (i = pos; i < length - 1; i++)
a[i] = a[i + 1];
return length - 1;
}
Cont’s…… Delete
int findElement(int a[], int length, int value)
{
I for (int i=0; i<length; i++)
{
T if (a[i] == value)
{
return i;
2 }
}
0 return -1;
}
8
• The code returns location of the values
oIf found it will return Index of the value
oOtherwise it will return -1
31 Dr. Belal Murshed
Data Structures and Algorithms
Array Operations
• Merging: combining the data of two or
I more arrays into one
T oImplementation is left for student exercise
public static void merge(int[] a, int[] b) {
2
0
8
Array Operations
• Searching through the array:
I • Depends on the algorithm
T • Some algorithms are faster than others
o More detail coming soon!
2 oLinear Search
0 oBinary Search
8
I
T
2 Linear Search
0
8
Linear Search
o In an unsorted array
I • The search operation can be performed by
linear traversal from the first element to
T the last element.
2
0
8
Linear Search
bool search(int a[], int length, int value)
{
for (int i=0; i<length; i++)
I {
if (a[i] == value)
T {
return true;
2 }
}
0 return false;
}
8
• The code returns a Boolean value
oIf found it will return TRUE
oOtherwise FALSE
37 Dr. Belal Murshed
Data Structures and Algorithms
Linear Search
int search(int a[], int length, int value)
{
I for (int i=0; i<length; i++)
{
T if (a[i] == value)
{
return i;
2 }
}
0 return -1;
}
8
• The code returns location of the values
oIf found it will return Index of the value
oOtherwise it will return -1
38 Dr. Belal Murshed
Data Structures and Algorithms
I
T
2 Linear Search
0
Analysis
8
I
T
2 Binary Search
0
8
0 • In this case:
8 oThe lowest index is 0
oThe highest index is 8
oSo the middle index is 4
I
T
2 Binary Search
0
Analysis
8
I
T
List Matching
2 Problem
0
8
}
}
58 Dr. Belal Murshed
Data Structures and Algorithms
2 int i;
Array Operations
• Sorting: Arranging the values in ascending
I or descending order
T oInsertion Sort
2 oSelection Sort
oQuick Sort etc.
0
• This will be covered later on
8
I
T
2
0
8