Data structures and algorithms

marouane lhamidi
4 min readJul 11, 2023

(First Part — Arrays (1))

In this article, we will begin by exploring arrays. It is essential to discuss arrays when talking about data structures, as they form the foundational concept. By understanding arrays, we can pave the way for learning about other data structures, we will also include dynamic definitions to make the concepts easier to grasp. Throughout the article, we will explore what is necessary to know about different data structures and what is not.

The article will be structured as follows:

Array.

Unordered array.

- Insert.

- Search.

- Delete.

Array

In Java, an array is a data structure that allows you to store multiple elements of the same type in a contiguous memory location. It offers a convenient way to group related data items together under a single variable name.

To create an array in Java, you need to specify its type and size. For example, you can create an integer array of size 5 using the following syntax: int[] numbers = new int[5];

When you create an array, the computer allocates contiguous memory space to hold the elements of the array. In the case of our integer array, it allocates memory to store 5 integers.

For primitive data types like int, double, boolean, etc., the array directly holds the values themselves. Each element of the array stores the specific value assigned to it.

However, for objects (non-primitive types) in Java, arrays store references to the objects rather than the objects themselves. The elements of the array contain memory addresses (references) pointing to the actual objects stored elsewhere in the memory heap.

Unordered array

An unordered array, also known as an unsorted array, is a data structure in which elements are stored in a collection without any specific order or arrangement. It is a simple and straightforward way of organizing data, where the elements are placed in the array without any specific sorting criteria.

In an unordered array, the elements can be accessed and retrieved using their indices. However, there is no guarantee or requirement that the elements will be in any order. The elements can be inserted or removed from the array at any position without affecting the overall structure of the array.

- Insert

Inserting an element into an unordered array involves adding the new element to the end of the array. Since the array is not sorted, there is no specific position where the element needs to be inserted.

The algorithm to insert an element into an unordered array:

Find the insertion position: In an unordered array, the insertion position is always at the end of the array.

Increase the array size: If the array is already full, you need to increase its size to accommodate the new element.

Insert the element: Place the new element at the insertion position.

- Search

Searching for an element in an array involves locating the position or index where the element is stored within the array. The search algorithm iterates through the array elements and checks if the desired element is found.

The algorithm to search for an element in an array:

Start from the beginning of the array and initialize a variable to keep track of the current position.

Iterate through the array elements and compare each element with the target element you are searching for.

If a match is found, return the index or position of the element.

If the end of the array is reached without finding a match, indicate that the element is not present in the array.

- Delete

Deleting an element from an array involves removing the element from the array and adjusting the array’s size and elements accordingly. There are different approaches to perform the deletion based on the specific requirements of the situation.

algorithm to delete an element from an array:

Search for the position or index of the element you want to delete within the array.

If the element is found at position pos, shift all the elements after pos one position to the left, effectively overwriting the element to be deleted.

Update the length of the array by decrementing it by 1 to reflect the removal of the element.

In conclusion, unordered arrays provide a flexible and straightforward way to store and access elements without any specific ordering or arrangement. They allow us to store data in a collection and retrieve it based on its index. Unordered arrays are ideal when the order of elements is not important, and we simply need a convenient container to hold and manage data.

We have explored various operations on unordered arrays, such as insertion, search, and deletion. While these operations may not have a specific order constraint, they still provide efficient ways to manage and manipulate data within the array.

By understanding the concept of unordered arrays, we gain valuable insights into data structures and their applications. Whether it’s managing a list of names, storing user information, or organizing various data points, unordered arrays offer a practical and versatile solution.

--

--

marouane lhamidi

I am LHAMIDI Marouane, a 5th year student at the Moroccan School of Engineering Sciences, in the field of Computer Methods in Business Management.