Chapter III: Arrays - Algorithms and Data Structures I

Telechargé par darttox
1
Course
Algorithms and Data Structures I
ENSM
2023 – 2024
Chapter III: Arrays
1.
Definitions
- An array is a data structure T which makes it possible to store a certain number of elements
T[i] identified by an index i. Arrays generally verify the following properties:
All elements must have the same type.
The number of stored elements is fixed beforehand.
Reading and modifying any element in the array number is performed in constant
time, independent of the index of the element and of the total number of elements in
the array.
- A one-dimensional array or linear array is an indexed variable allowing n values of the
same type to be stored. The maximum number of elements, which is specified in the
declaration, is called the dimension of the array. It is a constant. The type of the array is the
type of its elements. The position of an element within the array is called the index of the
element.
The definition states that:
All the elements of an array have the same name (that of the array).
All elements of an array have the same type; we will talk about an array of integers,
an array of characters, etc.
An array has a fixed size, known and defined beforehand (from the beginning of the
algorithm). This size can no longer change during the execution of the algorithm.
An array may not be completely filled, but it can never contain more elements than
the expected number that is defined at declaration.
2.
Syntax in algorithmic language
The declaration of an array is done as follows:
a: array [dim] T;
2
ð Array variable a, whose elements are of type T and whose dimension is dim. We
recall that the dimension is the size of the array, i.e. the maximum number of elements
the array a can store.
An array can be illustrated as follows:
Example:
a_int: array [8] integer;
Declares an array a_int whose elements are of type integer and whose
size is 8.
Accessing to the elements of an array is performed using indices:
table [3] => provides access to the 4th element of the array.
tab [k] => provides access to the (k-1)th element of the array.
The integer k must be between 0 and dimension-1.
tab[2] <- 5; => the 3rd element of the array tab receives the value 5.
x <- tab[k]; => the variable x receives the element with index k of tab
tab[2] <-'a'; => error, mismatched type: a is an array of integers and not an
array of characters
Remark :
Most languages check for overflow (bounds crossing) of an array and raise an error which
usually stops the program, but the C language, for example, does not perform any checking.
Application exercises:
Write an algorithm to read an integer array of 5 elements.
Write an algorithm to calculate the sum of the elements of an integer array of 5 elements.
Write an algorithm to calculate the maximum value of the elements of an integer array
of 7 elements.
0
1
2
3
dim - 1
Element of type T
The array a
3
3.
Advanced table techniques
4.1 Sorting an array
Remember that arrays allow us to store several elements of the same type within a single
entity (or structure).
When the type of these elements has a total order
1
, they can therefore be arranged/sorted
in ascending or descending order.
Sort an array <==> arrange the elements of an array in ascending or descending order.
There are several sorting methods that differ in their execution time (algorithmic
complexity) and their complexity of understanding.
All sorting algorithms use a method that swaps the value of two variables.
There are several possible strategies for sorting the elements of an array; we will see two
of them: The selection sort and the bubble sort.
4.1.1 Sorting by selection
Sort by selection <==> sort by minimum
This consists in putting in good position, the first element, that is to say the smallest. Then
we put the next element in the right position. And so on until the last one.
In other words, for a given place, we select the element that must be positioned there.
If the objective is to sort the following array of integers: [101, 115, 30, 63, 47, 20]
Sorting by selection proceeds according to the following 5 steps:
Stage 1: [
20
, 115, 30, 63, 47, 101 ]
Step 2: [20,
30
, 115 , 63, 47, 101]
Step 3: [20, 30,
47
, 63, 115 , 101]
Step 4: [20, 30, 47,
63
, 115, 101]
Step 5: [20, 30, 47, 63,
101
, 115 ]
To implement the sort by selection, we therefore need a “function” which makes it possible
to find the minimum (value and position of the smallest element) in an array or in a sub-
array. A sub-array is defined by a start index and an end index in an array.
The selection sort algorithm proceeds using two nested loops:
1.
A main loop takes as a starting point the first element of the array, then the second,
and so on, until the last one.
2.
The secondary loop: from this starting point (defined by the main loop) to the last
element of the array, we search for the smallest element. Once we find it, we swap
it with the element stored in starting point position of the array.
1In mathematics, we call total order relation on a set E any order relation ≤ for which two elements of E are
always comparable, i.e. ∀𝑥, 𝑦 𝐸, 𝑥 𝑦(𝑜𝑟(𝑦 ≤ 𝑥.
4
4.1.2 Bubble sort
The basic idea of bubble sort is to say that an array sorted in ascending order is an array in
which every element is smaller than the one that follows it (!).
This sorting method thus consists of taking each element of an array, and comparing it
with the element that follows it. If the order is not good, then these two elements are
swapped. We do this until we have no more permutations to perform.
The largest elements thus “rise” little by little towards the last places, which explains the
name of “bubble sorting”.
Let's go back to the array of integers: [101, 115, 30, 63, 47, 20].
The bubble sort of this table proceeds according to the following steps:
Step 1
[101, 115, 30, 63, 47, 20]
[101,
30, 115,
63, 47, 20]
[101, 30,
63, 115,
47, 20]
[101, 30, 63,
47, 115,
20]
[101, 30, 63, 47,
20, 115
]
Step 2
[101, 30, 63, 47, 20, 115]
[
30, 101,
63, 47, 20, 115]
[30,
63, 101,
47, 20, 115]
[30, 63,
47, 101,
20, 115]
[30, 63, 47, 20,
101, 115
]
Step 3
[30, 63, 47, 20, 101, 115]
[30,
63, 47,
20, 101, 115]
[30, 47,
63, 20,
101, 115]
[30, 47, 20,
63, 101
, 115]
Step 4
[30, 47, 20, 63, 101, 115]
[30,
47, 20,
63, 101, 115]
[30, 20,
47, 63,
101, 115]
Step 5
5
[30, 20, 47, 63, 101, 115]
[
20, 30,
47, 63, 101, 115]
Step 6
[20, 30, 47, 63, 101, 115]
4.2 Binary search (half-interval search, logarithmic search)
Imagine that we have to write an algorithm that checks if a word exists (or not) in the
dictionary. We can assume that the dictionary has been previously stored in an array whose
elements are of “string” type. Let's say the array (and therefore the dictionary) contains
40,000 elements.
A first way of checking whether a word is in the dictionary consists of successively
examining all the words in the dictionary, from the first one to the last one, and comparing
them with the word to be checked.
It works, but it may be being very time-consuming: If the word is not in the dictionary, the
algorithm will only know it after 40,000 iterations! And even if the word is in the
dictionary, the answer will still require an average of 20,000 iterations.
However, there is another way to perform this search, taking advantage of the fact that in
a dictionary, the words are sorted in alphabetical order.
This is binary search.
Moreover,
a human being looking for a word in the dictionary never reads all the words, from the first
to the last one: He/she uses the fact that the words are sorted.
Binary research consists of comparing the word to be checked with the word that is right
in the middle of the dictionary. If the word to be checked is earlier in the alphabetical order,
we know that we will have to look for it from now on in the first half of the dictionary.
Otherwise, we know we will have to look for it in the second half.
From there, we take the half of the dictionary that we have left, and we start again: we
compare the word to look for with the one that is in the middle of the remaining piece of
the dictionary. We discard the bad half, and we start again, and so on.
By dint of cutting our dictionary in half, then in half again, etc. we will end up with a piece
that only contains one word. And if we have not come across the right word at one time or
another, it is because the word to check is not in the dictionary.
Let's see what this gives in terms of the number of operations to be performed, choosing
the worst case: The one where the word is absent from the dictionary.
Initially, we look for the word among 40,000.
After test n°1, we only look for it among 20,000.
After test n°2, we only look for it among 10,000.
After test n°3, we only look for it among 5,000.
After test n°15, we only look for it among 2.
After test n°16, we only look for it among 1.
1 / 6 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!