Skip to content

Data structure and algorithm: A brief explanation of binary search and arrays

Data structure and algorithm A brief explanation of binary search and arrays
ClosePlease login

No account yet? Register

Data is all we hear these days. From our Facebook usage to our daily rant on Twitter, is a common knowledge that we input tons of data as well as get a lot of it every second we use our devices – even if it doesn’t make sense to us, at least that’s what we’re made to believe. We know our favorite, Mack Zuckerberg, the funder of Facebook, got in trouble because of this phenomenon – data! But, in all the buzz about data and how important they are in today’s world, I’m not sure if a lot of people have an inkling of how those data are being collected and get stored and eventually get retrieved for whatever usage there is. It begs the question, how do programmers, as the brains behind all the programs on our computers, collect, arrange, sort and output the data? What are the best practices for dealing with this very vital pieces of resources? This is where the concept of data structure and algorithm in programming comes in – the process where data’s behavior is defined. In this article, we’re going to look at some popular data structure and algorithms that programmers, especially java programmers, use. If you are ready, let’s begin.

I think it is not fair to talk about data structure and algorithm without defining or explaining the meaning of data itself. So, let’s see what data is.

What is data?

Data is information in different forms. Data could be anything from the personal information we store on our computers or on Facebook to our daily updates on various online platforms to the online retail websites we shop on.  Anyways, this is my little way of explaining our modern-day oil, DATA. Technically though,  according to,  computer data is information processed or stored by a computer. This information may be in the form of text documents, images, audio clips, software programs, or other types of data. Computer data may be processed by the computer’s CPU and is stored in files and folders on the computer’s hard disk.

What are data structures and algorithms?

Data structures in programming or computer science, in general, are different ways of how computer program’s data are being structured and saved based on the required circumstances.  As a programmer, knowing how data structures work is essential regardless of the kind of programming language you use.

There are several data structure types that a programmer should be conversant with. Some of which are arrays, arraylists, linkedlists, trees, hash tables, queue and so on. Even though understanding data structures does not depend on a single programming language, for the purposes of this article, we are going to focus on Java programming language. Again, for more emphasis, data structure and algorithm are the most significant concepts that every programmer should know and be comfortable working with them. 

READ ALSO:  Learning how to use Python without any programming background

In order to fully understand and be able to use any type of data structure though, a programmer needs to understand what is algorithm and how they are used, then one can combine the two and work with them. Data structure cannot be correctly applied without the effective use of the right algorithm.

This brings us to the next question. What then is algorithm? Algorithm is just a fancy word that programmers use to describe a method of doing something. Basically, in computer science, algorithm means a step by step procedures or instructions on how to solve computational problems. Therefore, after having the appropriate data structure type to work with, the next and most important thing is the steps of implementing the data. Algorithm is as important as the data structure itself, without it the data and how it’s being structured is almost useless.

Types of algorithm

There’re so many types of algorithms in computer science that programmers use every day, but the most popular and widely used are searching and sorting algorithm categories.

Searching algorithms as the name suggests are algorithms that are used for searching an item or a group of items from a given set of data. Some of the prominent in this category are binary search, linear search, depth first search (DFS) and breadth first search (BFS) and of course tons of others.

While, in the category of sorting algorithm, we have so many of them too, some of which include, mergeSort, quick sort and so on.

Let’s take a closer look at one of the popular algorithm that is used widely today by programmers across programming languages, binary search!

What is a binary search?

The name binary got its meaning from its root ‘bi’ which means two. Basically, binary search is a method or an algorithm that is used to perform a search in a set of data. It approaches search by dividing a set of data into two groups until the search item is found. It follows a divide and conquer technique.

An important point to note that though, before using a binary search algorithm, a set of data has to be arranged in order. For instance, let’s say, the data at hand is an integer of 1 to 10, the arrangement of this set of data most begin from the smallest number to the highest number or vice versa.

How a binary search is performed

The process of binary search begins by splitting the data into two in order to eliminate the part where the search item couldn’t be found. Then the remaining will be split into two as well, and the part that couldn’t have the search item get eliminated. This is how the circle continues until the search item is found or not. Here is a more vivid explanation of it;

  1. If search item < middle element of list, the search is restricted to first half of the list. 
  1. If search item > middle element of list, search second half of the list. 
  2. If search item = middle element, search is complete. 
  3. If search item is not found, return -1
READ ALSO:  Cloud computing: An insight into its various concepts

Let’s see an example of how to find an item in a given array below.

Search item: 20

That was a binary search algorithm in action. After following three steps, we’re able to find the given element (20) in the array. For more on the binary search algorithm, check out this video.


Arrays are a collection of items or data that share the same variable name and data type. I’d like to look at array as a group of similar things in a single container with several partitions; each item in a partition; and these items could be anything. As mentioned, array elements (every item in an array is called element) bare same name and same data type. An array could be of primitive data type like integer, Boolean, double and so on, and could also be an object of a user-defined data type. Arrays could be one-dimensional or multi-dimensional. It is important to note that arrays are index-based, meaning each element of an array is linked to its position or index. The elements are accessible through their indexes.

Look at the example of how arrays are used in the table below:

The table above shows:­

  1. Array name: intArray
  2. Array index: 0-5
  3. Array data type: integer

Analysis: The array table above shows an array of integer, named, intArray. Keeping with the indexing rule of an array, its index starts from 0 which corresponds to element, 23, and ends at an index of 5 which corresponds to element, 21.

Let’s see more example of arrays of a different data type.

The table above shows:

  1. Array name: douArray
  2. Array index: 0-5
  3. Array data type: double

Analysis: The array table above shows an array of type double, named, douArray. Again, to keep with the indexing rule of array, the index starts from 0 which correspond to element, 23.5, and ends at an index of 5 which correspond to element, 22.4.

Array of string

The table above shows:

  1. Array name: strArray
  2. Array index: 0-5
  3. Array data type: String

Analysis: The array table above shows an array of type string, named, strArray. Keeping with the indexing rule of array, its index starts from 0 which correspond to element, “Dog”, and ends at an index of 5 which correspond to element, “Foxes”.

READ ALSO:  Exploring the basics of Java programming language

Declaration, instantiation and accessing an array

Array declaration

Arrays are declared by either first of all mentioning their data type with square bracket next, then followed by the array variable, or by stating the data type, then followed by the array variable with square bracket last. Below is an example.

Instantiation of an array

When an array is declared as in the above, it’s only the reference to an array that is created. To actually create or allocate a memory to the array an object has to be created by using the ‘new’ keyword together with the same data type used in the declaration statement. Then followed by the specific number of elements or size of this array, in a situation whereby the elements are not known. But if the elements are known, then the elements should come (assigned) right after the declaration statement or the variable inside curly braces. Below is an example of what I mean:

Accessing array elements and displaying them

Array elements can be accessed and displayed individually or collectively using for loop statement. Once an array is created and elements assigned, then those elements can be accessed using their indexes as mentioned earlier. To access an individual element the array variable or name should be mentioned together with the index number inside an array subscript (a pair of square brackets). And of course, when displaying, it should be displayed in Java print statement. See example below:

To access all the elements of an array at once, let’s use ‘for’ loop statement. In the ‘for’ loop statement, an integer variable will be declared for the indexes of the elements. The size of the array will be specified; either by mentioning the array name with dot length (.length) keyword or by simply writing the size. Remember, the number indicating the size always must be an integer.

Then lastly, finish up the loop statement by incrementing or decrementing, depending on how you want to display the values. This is how to increment or decrement; variable name and two plus signs for incrementing (i++) or variable name and two minus signs for decrementing (i- – ). Below is an example:

Adding or changing array elements

Array’s size is fixed, once the size is created in the memory it cannot be expanded later in the program. But the elements’ values can be changed using the array name and the index number. Here is an example below:

That’s the little I’m able to bring to you regarding data structure and algorithm this time around. Let me hear what you think in the comment section below. Until then, happy coding!

Rahmatu Lawan

Rahmatu Lawan

A wife and a mother who is always striving to improve.I am always excited to connect with new people and learn from each other.View Author posts

Drop a Comment

Your email address will not be published. Required fields are marked *

Discover more from Penprofile

Subscribe now to keep reading and get access to the full archive.

Continue reading