Algorithms for working with arrays - an overview
Lesson content
- Algorithms for working with arrays
- Types of algorithms for working with arrays
Algorithms for working with arrays
It has already been stated that an algorithm can be defined as a procedure with clearly defined steps that lead to solving a particular problem, and that it can apply to any process, activity, or issue.
Although the previous two lessons on arrays and the FOR-EACH statement did not formally address algorithms for working with arrays, some of them were already used. For example, displaying (printing) all elements of an array to the screen, or inserting a new value into the array at the first available position.
The following lessons provide detailed explanations of some of the most commonly used algorithms for working with arrays. Although the examples and exercises are written in the Java programming language, the algorithms themselves are universal and can be applied in many other programming languages as well. In addition, many of these algorithms can also be applied to lists with some modifications.
There are a large number of array-related algorithms, and new ones can always be created by modifying existing algorithms or combining several algorithms. For that reason, the following lessons include only some of the most frequently used array algorithms as a foundation for learning.
It should also be noted that this collection does not cover the topic of algorithm efficiency (also known as algorithm complexity) — which deals with execution speed, memory usage, etc. Further study on algorithms and their efficiency can be pursued through courses or books on data structures and algorithms.
Types of algorithms for working with arrays
There are many classifications of array algorithms, which may seem intimidating or confusing to someone just beginning to learn programming. Therefore, a simple, somewhat informal, but clear classification is provided here, in which array algorithms are grouped by purpose, i.e., the goal they aim to achieve.
According to this classification, there are algorithms for:
- Searching and displaying/retrieving array elements
- Adding elements (values) to an array
- Replacing elements (values) in an array
- Deleting elements (values) from an array
- Group operations on array elements
- Sorting array elements
- Working with multiple arrays
Once again, it is emphasized that this is not an exhaustive classification, nor does it list all array algorithms. It is merely an overview of some of the most frequently used ones.
Depending on the needs of a specific software system, these algorithms may not always be used in their basic form, but rather with certain modifications or adaptations to the specific problem.
Finally, there are also more complex algorithms that are created by combining two or more basic algorithms.
From a practical standpoint, the task of a programmer is to, depending on the specific requirements, identify which algorithm best suits the given problem and use it — either in its original form, with modifications, or combined with another algorithm. Complex problems are usually broken down into simpler steps, and those steps can be solved using simpler algorithms.
When the above classification is broken down further, we get the following list of the most commonly used array algorithms:
-
Searching and displaying/retrieving array elements:
- Searching and displaying all elements (in normal or reverse order)
- Searching and displaying only some elements (in normal or reverse order)
- Searching and retrieving specific values (first or last occurrence)
- Searching and counting specific values
-
Adding elements (values) to an array:
- Setting all array elements to a specific value
- Adding a value to the first/last free position
- Adding/inserting a value while maintaining array order (e.g., ascending or descending)
- Inserting a value in a specific position in the array
-
Replacing elements (values) in an array:
- Replacing a value in a specific position in the array
- Replacing a specific value (first, last, or all matches)
- Shifting all elements one place to the right or left
-
Deleting elements (values) from an array:
- Deleting a value in a specific position in the array
- Deleting all values from the array
- Deleting a specific value (first, last, or all matches)
- Deleting the most recently entered value
-
Group operations on array elements:
- Summing, multiplying, or calculating the average of array elements
- Finding the maximum or minimum value in the array
-
Sorting array elements:
- Sorting in descending/ascending order using the Bubble Sort algorithm
- Sorting in descending/ascending order using the Selection Sort algorithm
-
Working with multiple arrays:
- Creating a new array as a copy of an existing one
- Copying elements from one array into another existing array
- Merging two arrays into a new one by appending one to the other
- Merging two arrays into a new one without duplicate values
Each of these algorithm groups is covered in detail in the following lessons.