Featured Create an Ad

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

  • 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. 

    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.

    Related article: Graphical User Interface: An overview of Java Swing interface as a GUI

    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

    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”.

    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!

    You may also want to check this article on fundamentals of Java programming language.

No Stickers to Show


Recent Blogs

  • What do you know about the violence against persons (prohibition) act?

    Posted Thu at 8:03 PM

    On the 25th of November 2019, a standstill rally was organized by a group of courageous young Nigerians in a bid to get the Violence Against Persons (Prohibition) Act domesticated in all states. This heroic act, however, was met with criticism which may have resulted fr...

  • Rough Diamonds: Chapter One

    Posted Thu at 6:15 PM

    Torn trousers, worn-out shoes, undersized, dirty T-shirts, uncombed hair and a generally unkempt appearance. These introduce them before they even speak or extend their arms for alms. But Mukhtar Sadiq wants the world to know that he is different. 'I speak English,&rsq...

  • An insight into suicide according to diathesis-stress models

    Posted Thu at 5:46 PM

    The biopsychosocial model is currently the foremost comprehensive and influential approach to explaining psychological disorders, especially as it applies to the Nigeria context. Having explained suicide from a cognitive perspective, behavioural perspective, biological ...

  • Social and health consequences of female genital mutilation

    Posted Feb 21

    Female genital mutilation is a barbaric procedure that involves the full or partial removal of the external female Genitalia. This horrible practice is said to have affected over 200 million girls all over the world, with about three million girls under the risk of unde...

View All

Random Blogs

  • The wonders of the lifestraw

    Posted January 22, 2017

    Water is essential for survival and a necessary item in every emergency supply kit.You can survive longer without food than you can without water. Water is a transparent and nearly colorless chemical substance that is the main constituent of Earth’s streams, lakes...

  • Seven strategies for improving memory in students

    Posted September 16, 2017

    Have you ever been faced with a pile of study materials that won't just stick to your head? Many students find it hard to recall vital information when they need it most, during tests and exams. To prevent the occurrence of these embarrassing situations, it is important...

  • Will computers make teachers obsolete?

    Posted March 2, 2018

    Teaching has long been seen as a noble profession as teachers are responsible for educating the population. But the role of teachers in the educational system is increasingly being threatened by the day. Technology enthusiasts are claiming that in some years to come, th...

  • Ebook vs paperback: Is the new generational mode of reading taking precedence?

    Posted November 30, 2018

    My one prayer whenever I'm stressing my eyes for hours unending reading my favourite novel sometimes when the room is dark, and the light from my phone is assaulting my eyes is for my eyes to not let me down in the future, because I hardly doubt it is healthy. But becau...

View All