Home > Sample essays > 33 Exploring Algorithms: Linear and Binary Search

Essay: 33 Exploring Algorithms: Linear and Binary Search

Essay details and download:

  • Subject area(s): Sample essays
  • Reading time: 10 minutes
  • Price: Free download
  • Published: 1 April 2019*
  • Last Modified: 23 July 2024
  • File format: Text
  • Words: 2,936 (approx)
  • Number of pages: 12 (approx)

Text preview of this essay:

This page of the essay has 2,936 words.



Algorithms

An algorithm is a certain set of instructions that can be used to perform a task. These instructions can be as simple as subtracting and adding, or as complex as creating viruses that could shut down google, but they all use the same base concepts. An algorithm generally consists of input, calculation and output. The most common and well known algorithm would be a cooking recipe as it is a set of instructions to take base products and transform then into a new product

Algorithms can have various structures, such as linear ( meaning searching through one by one is order ) branch (if/else), and loop ( repeat until what is required has been achieved). These different structures can be combined in several different ways to make a more complex and of higher use, algorithm. Nearly all algorithms can be broken down into these components.

When a computer is given an algorithm, it will perform exactly as it is told to, and if there is a problem, will notify the user, or crash. This is not the same with a person, who can interpret instructions in different ways and take information and expand of this. An algorithm is a specific way of telling a computer what to do, without deviation. This requires an algorithm to be scripted perfectly or else it will not work at all.

An example of this that I will use is the first 10 letters A-J

A

B

C

D

E

F

G

H

I

J

I used a program ( scratch ) that will randomly chose a letter from in between A and J, and the result corresponded to a letter between A and J. The following picture is the code that I used on Scratch

Linear Search

A linear search involves the operation of going through each number starting at A and going up to J in order and determining if it is the number we are trying to find. In this case, I am searching for the letter G. Here is the logical process undertaken during a linear search.

Is letter number 1, the letter G ? No, so continue to the next letter.

Is letter number 2, the letter G ? No, so continue to the next letter.

Is letter number 3, the letter G ? No, so continue to the next letter.

Is letter number 4, the letter G ? No, so continue to the next letter.

Is letter number 5, the letter G ? No, so continue to the next letter.

Is letter number 6, the letter G ? No, so continue to the next letter.

Is letter number 7, the letter G ? Yes. Output that the number of the letter being searched for is the number 7

This took 7 steps to find the specified letter. Each letter must be individually analysed in order to find the specific letter. The maximum possible number of steps is the number of items that are being searched for, so in this case only 10 were being searched. If there were 26 letters to search through, then the maximum number of steps needed to find the letter desired would be 26 in the worst case scenario. This is also the same with "n" items. Searching through "n" items will require the maximum of "n" steps. This is an easy, but inefficient way to search through data, as the more items there are to search through, it will take longer in most cases than other algorithms, which is very unnecessary.

Binary Search

A binary search uses the off/on, no/yes, 0/1 concept. Either it is, or it isn't. In terms of a binary search, it takes the provided information, divides it in half again and again until the individual letter is found. This time, I will be searching for the letter I, and its position in the list ( number 9 ). Here is the order of the letters alphabetically.

A

B

C

D

E

F

G

H

I

J

The letter I am looking for is number 9. Here are the steps required to find it using the binary search method:

The middle letter, E, is above the letter in the alphabet. Use all the letters below it (6-10)

The middle letter, G, is above the letter in the alphabet. Use all the letters below it (7-10)

The middle letter, H, is above the letter in the alphabet. Use all the letters below it (8-10)

The middle letter, I, matches what we have been searching for (9)

it took 4 steps to find the letter I was looking for. This is represented by the below algorithm, in pseudocode:

Sort songs alphabetically

Ask user what song they are looking for. letterSearched

Find the middle letter

Define variables

Repeat while flag(not)=1

If middleLetter is equal to letterSearched

 Delete all letters above middleLetter

 Delete all letters below middleLetter

If middleLetter is lower in the alphabet to letterSearched

Delete all letters above middleLetter

   c.  If middleLetter is lower in the alphabet to letterSearched

Delete all letters below middleLetter

This shows a rough outline of the procedure to perform a binary search. I tried every possibility for the example using this algorithm, and determined that there can never be more than 4 steps required to find any letter. If there was 100 objects to search, the maximum number of steps would be 7.

In order to calculate this, take the total number of items so search, divide by 2 again and again until you reach the final objective. If a value is a decimal (e.g. 15/2=7.5) round up to the nearest integer. The maximum number of steps possible in a binary search is equal to the number of times you divided the total by 2 to get 1. Here's and example , with 200 items to search through: 200/2=100  -> 100/2=50  ->  50/2 = 25  ->  25/2 = 12.5(13)  ->  13/2=6.5(7)  ->  7/2=3.5(4)  – > 4/2=2  ->  2/2=1

In order to get from 100 to 1, I divided by 2 7 times, meaning the maximum number of steps required to find any items in the list is 7 times.

At best, the minimum number of steps is 1, if the letter in question is the middle number to begin with. The number of steps taken to find an object is not its distance from the beginning, as in a linear search, but it's relation to the total number of objects in the set.

Problems that can possibly occur during the coding of this specific algorithm:

Clarity about deleting the data above and/or below the median

Procedure if the median is a decimal value

Different types of data input e.g. string, numbers or characters

Linear vs Binary

9In terms of resources, it is easily to understand that using a binary search is much more efficient than a linear search. Comparing the number of steps each search can take for 100 items, I calculate that a linear search takes anywhere from 1 step, or worst case scenario being 100 steps to find, whereas a binary search of 100 items can take a maximum of 7 steps and as little as 1 step. A binary search is far more efficient than a linear search. In this day and age, companies are always trying to locate a much faster and cheaper way to do things, and using binary search instead of a linear search will save companies resources that could be put to far better use.

So different programs and different algorithms may produce the same result, but quicker, smarter, more efficient algorithms will produce the same result but just faster, or with a greater degree of accuracy. If scripted correctly, a set of specific instructions will be performed by a computer faster than a human can possibly think about it, and there is no room for different interpretations

Basic Structures

Different algorithms may perform their own different functions, but in order to work together efficiently, they must be put together in the right way. As before, the different structures are linear ( one after the other) branch (if/else), and loop ( repeat until criterion are reached).

A linear or sequential structure is when tasks are performed one after the other, until the end is reached. Here is an example of a linear program:

Each line of code is performed one after the other

A branch, or conditional, structure gives the program a choice. It can either go one way or the other, right or left, for example. The example I have given below shows a number, and if the number entered is greater than 10m it will output a statement confirming that, whereas if it is less than or equal to 10, it will output that statement:

The third type of structure, is the iteration, or loop statement. This is where a program will continue to do the same thing over and over again, until a certain condition is reached. This is helpful if, for example, calculating the square root of a number: the user will be repeatedly asked to enter a number until the number is not a negative. Here is an example:

So, there are many different ways a program can be put together. The same algorithm put together in different ways, may produce a completely different outcome, like brackets in a mathematical equation. If used properly, they can be combined into the most simple, yet complex programs for any desirable task.

Task 2 – Concept of a programing language

Programming languages are ways of helping humans communicate with computer. As we communicate with each other in humanoid languages, such as English, computers have their own base language, which consists of nothing but 0's and 1's. This is the lowest form of a programming language, binary, and is the machine code that computers run on. other, higher end languages are easier for humans to understand. A language such as scratch can help someone understand the basics of programming, because it is visually easy to understand and has all its instructions in common language.

But higher end languages must be translated into machine code before being run. This job is done by a compiler, which takes a higher level programming language and converts it into a language that is easier for the computer to run on. It is possible to create a program using only 0's and 1's, but for a human this would take a ridiculously long time and is not efficient.

Task 3 – Comparing 2 devices

I will compare the Apple iPod Touch 4g and the Toshiba NB520 notebook. Both are commonly used and their interfaces which I will focus on, are widely known across the world.

Apple iPod Touch 4g

The Apple iPod run has rapidly turned out to be a standout amongst the most prominent convenient registering gadgets. The model I am looking at is the most recent equipment, fourth era, and the most recent programming iOS 5. The product running on it isn't open source, and is vigorously confined by Apple to the product. All applications that can be downloaded from the application store have experienced examination and have been affirmed by Apple. This constrains the abilities of a few highlights, yet implies that all accessible applications are good, suitable and are likewise protected to utilize. The iPod 4g is outfitted with a 3.5 inch touchscreen, fit for multitouch signals, has a worked in 3d accelerometer, is equipped for picture and scene introduction, and associates with the web by means of any Wi-Fi organize

I will now utilize the iPod to make a note utilizing a scratch pad, or other word processing application. I request to open the iPod; I need to slide over the screen, as coordinated by a message on the swipe bar. When open, a page of utilizations is set out in a 4×5 course of action, all named and showing a symbol of the application. On the off chance that the names are too little, a basic three-finger twofold tap zooms in on the coveted territory. The most helpful word handling application is the "Scratch pad" application, which is standard on all iPod touches. When open a full QWERTY console is shown underneath screen space where the content is shown. In the event that another dialect is required, the symbol with the globe can be taped, exchanging immediately between different universal console/interfaces. While messaging the extent of the keys implies botches are made. By and large, I can take as meager as 4 seconds to go from rest mode to open notebook, supportive to record unconstrained notes in a hurry.I will now utilize the iPod to make a note utilizing a scratch pad, or other word processing application. I request to open the iPod; I need to slide over the screen, as coordinated by a message on the swipe bar. When open, a page of utilizations is set out in a 4×5 course of action, all named and showing a symbol of the application. On the off chance that the names are too little, a basic three-finger twofold tap zooms in on the coveted territory. The most helpful word handling application is the "Scratch pad" application, which is standard on all iPod touches. When open a full QWERTY console is shown underneath screen space where the content is shown. In the event that another dialect is required, the symbol with the globe can be taped, exchanging immediately between different universal console/interfaces. While messaging the extent of the keys implies  are made. By and large, I can take as meager as 4 seconds to go from rest mode to open notebook, supportive to record unconstrained notes in a hurry.

Toshiba NB250 Notebooks

This notebook is running windows 7, the most recent working framework for a windows PC. It is a remote gadget, with a charge pack if necessary however has a worked in touchpad to enable a client to explore utilizing their finger. In any case, the touchpad is just 7x4cm, so is very little and can frequently be dubious to utilize. It is fit for utilizing remote and satellite web associations, yield and information sound, VGA and 3 usb ports. It has a full Qwerty console, with bolt and capacity keys. It has 2 speakers installed beneath the console, expected to be for impeccable sound quality.

The working framework that is running of this note pad, Windows 8, is the most unmistakable UIs on the planet. It has a "desktop", which contains connections to the greater part of your most often utilized applications, a "begin menu, which contains connections to all applications and archives. THere is likewise a " status bar" which can help you to screen the advance of different applications and framework forms. This interface is one that has been consolidated in all windows working frameworks since Windows 85, Released August 1995

I will now attempt to compose a sentence on a word preparing program. Because of the supportive area of the begin menu, it takes 2 ticks of the mouse to open Microsoft Word the most generally utilized word processor on the Windows stage, I initially opened the "begin" menu, and after that tapped the "Microsoft Word" symbol, After the program has stacked, it as just taken 5 seconds to open the application. Presently I write a sentence. The portable workstation has a full QWERTY Keyboard, making writing simple with the universally perceived console format. the extent of the keys considers touch-writing, and the delete key is strategically placed for anybody attempting to fix any mix-up.

Laptop vs iPod

First of all , both of the gadgets that were tried, are showcased as being compact. They are generally little and reduced, light and simple to convey. Be that as it may, their base designs are very unique. The NB250 is made to be a PC in your grasp, demonstrating the illustrations and interface of any consistent desktop PC, yet in addition being versatile and simple to transport. Be that as it may, the iPod was principally proposed for tuning in to music, yet the UI made by Apple takes into account significantly something other than music, however the UI made by Apple takes into consideration significantly something beyond music. Presently you can watch motion pictures, play amusements, surf the web and even send messages and messages ( if in scope of a working Wi-Fi switch )

The NB250 might be little in contrast with different PCs, yet not sufficiently little to fit in your pocket, similar to the iPod. The NB250 is more focused at the representative, dependably moving yet in constant need of a completely able PC. The full console is additionally more effective than the iPod console.

The possibility of a "mouse" for a PC is gradually vanishing, with touch screens winding up increasingly mainstream. There are even some full measured desktop PCs mind touch screens. Be that as it may, by and large, portable workstations still have the requirement for a trackpad, or mouse. This is viably only a place to put your finger to simuate a mouse. On te NB250, on account of its restricted size, the trackpad is very little and can regularly be precarious and disappointing to utilize. On the ipod, in any case, the 3.5 inch touch screen implies that your finger is the mouse. Essentially tap on the application you need to open, instead of clicking it. In any case, the NB250 has 3 USB ports, taking into consideration the expansion of outer consoles or mouse to upgrade convenience

Looking at these two gadgets is troublesome, as they are both focused at various gatherings of individuals, diverse occupations, distinctive ways of life, and so forth. On the off chance that you are searching for exactness and full computational capacity, you are best to search for a scratch pad, for example, the NB250. However, in the event that you simply need to hear some out great tunes, play a few recreations, type a couple of words, and keep everything in your pocket, you ought to be watchful for gadgets like the iPod Touch 4g

About this essay:

If you use part of this page in your own work, you need to provide a citation, as follows:

Essay Sauce, 33 Exploring Algorithms: Linear and Binary Search. Available from:<https://www.essaysauce.com/sample-essays/2017-10-31-1509491916/> [Accessed 15-04-26].

These Sample essays have been submitted to us by students in order to help you with your studies.

* This essay may have been previously published on EssaySauce.com and/or Essay.uk.com at an earlier date than indicated.