Uploaded by Pius Donkor

Data Structures Arrays-1

advertisement
ARRAYS
COMP 203 - Data Structures and Algorithms
Definition
• An array is a list of contiguous memory cells that store data of the
same type
• An array is a finite, ordered and collection of homogeneous data
elements
• The adjoining cells are of the same data type and array name.
• Array data structure is usually finite and ordered in nature in the
sense that it stores only a known number of data items as per its size
in an ordered memory cells
COMP 203 - Data Structures and Algorithms
Basic Terminologies of Arrays
Element: This refers to any data or item stored in an array.
Size: The size of an array refers to the total number of elements that can be
stored in an array.
Name: Every array must have a name. In the case of figure 2 the array is called
ages.
Type: Type refers to the kind of data stored in an array. The data type can either
be an integer, decimal or textual (string or character).
Cell: A single memory location of an array is called a cell. The total number of
cells of an array is equal to the size of the array.
Index: An array index is a positive integer that uniquely identifies a cell of an
array. Indexing of arrays usually starts from zero (0) to the size of the array minus
COMP 203 - Data Structures and Algorithms
one (1) in
STRUCTURE OF AN ARRAY
COMP 203 - Data Structures and Algorithms
Consider the array below:
AGES:
Determine the
• Name
• Size
• Type
• Index of each array element of the array
COMP 203 - Data Structures and Algorithms
TYPES OF ARRAYS
• One dimensional arrays
• Two dimensional arrays
• Multi-dimensional arrays
COMP 203 - Data Structures and Algorithms
One dimensional arrays
• An array is said to be one dimensional if a single index is required to
store or retrieve elements.
• The physical structure of a one-dimensional array is linear as shown in
figure below.
• To store an element say 12 in the array, you are required to indicate the
index say 2
( k[2] =12).
Storing an Element in Index 2
COMP 203 - Data Structures and Algorithms
• Array Declaration
• To create or declare an array in C++, three things must be provided: the
name, type and size of the array.
double scores[6];
• The declaration above creates a one-dimensional array of type double
(floating points) named scores with a size of 6.
• The first index of the array, scores is zero (0) and the last is 5. When the
elements of the array are known already then the array can be declared
in this manner.
double scores[]={53,65,90,76,87,86}
• The above declaration automatically creates an array of fixed size 6. In
this kind of declaration the size is optional
COMP 203 - Data Structures and Algorithms
•Array Operations
• The basic operations that can be performed on arrays are
• Traverse: To visit each element of the array and print it.
 Insertion: To add a new element to the array.
 Delete: To remove an element from the array.
 Search: To check whether a particular element exist in the array.
 Sort: To arrange elements of an array in a particular order
(Ascending or descending).
 Merge: To add two distinct arrays together to form a single array.
COMP 203 - Data Structures and Algorithms
• Array Traversal Operation
• Traversing an array refers to visiting all the elements of the array and
processing (printing) them.
• Traversal starts from the first element (index 0) of the array and processing
each element in step of one up to the last item.
COMP 203 - Data Structures and Algorithms
• Insertion Operation
• An element can be added to an array only if it is not full.
• An element can be added to the beginning, end or at a given index of
the array per the requirements of the array.
• To insert an item at a given location (index):
• move all elements a step forward starting from the last element of
the array to the location to insert the new item as represented in
figure below.
COMP 203 - Data Structures and Algorithms
COMP 203 - Data Structures and Algorithms
• Insertion Algorithm
COMP 203 - Data Structures and Algorithms
• Deletion Operation
• To delete an item at a given index or location, simply move all elements
a step backward starting from the index just after the index of the item
to be deleted to the last element in the array.
• As the elements move, they override other elements originally at
various indices. Figure 5 illustrate the delete operation.
COMP 203 - Data Structures and Algorithms
COMP 203 - Data Structures and Algorithms
Deletion Algorithm
COMP 203 - Data Structures and Algorithms
• Search Operation
• This operation checks the existence of an item in an array.
• The operation returns or prints the element and/or its location (index)
in the array.
• To search for an item using a sequential search requires visiting every
element and comparing with the search item starting from the first
index.
COMP 203 - Data Structures and Algorithms
• Search Algorithm
COMP 203 - Data Structures and Algorithms
• Sorting Operation
• This operation orders elements in ascending or descending order.
• Elements can be sorted by comparing elements next to each other
and swapping them to follow a required order starting from the first
element.
• This process must be repeated until the elements are completely
sorted.
COMP 203 - Data Structures and Algorithms
• Sorting Algorithm
COMP 203 - Data Structures and Algorithms
• Merge Operation
• The merge operation combines two or more arrays to form a single array.
• To merge two arrays, a new array is created with a size equal to the sum
of the size of both arrays.
• Elements are then retrieved sequentially from the two arrays and
inserted unto the new array.
COMP 203 - Data Structures and Algorithms
• Merge Algorithm
COMP 203 - Data Structures and Algorithms
• Multi-Dimensional Array
• Multi-Dimensional arrays require more than just an index to
manipulate an array.
• When two indices is involved then the array represents a twodimensional array.
• Two-dimensional, three-dimensional arrays, etc are examples of
multi-dimensional arrays.
COMP 203 - Data Structures and Algorithms
• Two dimensional Arrays
• Two-Dimensional array is a group of adjoining memory locations of the
same type arranged in rows and columns.
• This array essentially looks like a matrix.
• Two dimensional arrays require two indices for referencing cells:
- the first index representing the row index
- the second index which refers to the column index.
COMP 203 - Data Structures and Algorithms
The Figure below represents a two-dimensional array with 3 rows and 4
columns.
What are the assignment statements of the array
elements
• 87
• 8
• 23
• 56
COMP 203 - Data Structures and Algorithms
COMP 203 - Data Structures and Algorithms
Download