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

  • Now open - 10 scholarships for African students in 2020

    Posted Wed at 9:21 PM

    As an African student, no doubt, winning a scholarship award would make your academic journey somewhat easier. While some families and sponsors can afford tuition fees and foot the educational bills of their child/ward, others can’t really do this comfortably. How...

  • Kinds of information systems for different levels of management

    Posted Tue at 6:57 PM

    To run a business is to engage in a series of processes and activities that are interconnected to each other. These processes and activities must be coordinated in such a way that will facilitate smooth operation for the success and goal attainment of the organization a...

  • Historical development of the law of torts in Nigeria

    Posted Jan 19

    John Wigmore once said that ‘every institute and principle of law has a philosophy – as every object in the Sun has its attendant inseparable shadow.” We cannot possibly talk about the law generally without discussing the law of torts. The law of tort...

  • Here is why you need to detox

    Posted Jan 14

    Have you ever been to the hospital because you didn’t feel quite good, but after several tests, nothing seemed to be wrong with you? Perhaps it was a headache? Perhaps fatigue? Sometimes when you feel tired or sick, it doesn’t always mean that you need medi...

View All

Random Blogs

  • IT infrastructure: A brief overview

    Posted December 31, 2019

    Information technology infrastructure or IT infrastructure as it is widely known is a necessity for modern business be it small, medium or large. Traditionally, whenever one hears infrastructure, the first thing that comes to mind is some sort of physical structures tha...

  • 5 tips to lose weight the healthy way

    Posted September 9, 2018

    You have tried different weight loss strategies but still can’t shed the stubborn pounds clinging to your body. After trying a variety of diet programs, you have eaten your way back to more weight, much to your disappointment. The truth is that diets don’t ...

  • 16 things I wish someone had told me about love

    Posted October 30, 2019

    ‘We accept the love we think we deserve.’ the first time I came across this quote while reading ‘The Perks of Being A Wallflower,' I had to pause and just let it sink in. We accept the love we think we deserve, just how powerful is this sentence. How m...

  • Effects of cheating in schools

    Posted November 25, 2016

    As the habit of cheating fills our schools these days, our next question will be, what are its consequences? It definitely has short time and longtime effects on the lives of those involved, the educational system and the the society at large. Cheating in schools can s...

View All

(200 symbols max)

(256 symbols max)