# 2. Data storage and algorithms

Before considering searching and sorting it should be understood that data can be stored in a number of common data structures

- data stored in a linear array
- data stored in a binary tree
- data stored in a linked list

We have already covered linked lists and binary trees. But another extremely common data structure is the 'array'.

An 'array' is a data structure that has a number of individual elements. For example, setting up an array with 20 elements could be defined like this (depends on the language being used)

var myarray = new Array(20)

Access to each element is achieved by referencing an index

myarray[9]

Note that it is very common for the first element in an array to have index zero

myarray[0]

So the last element in a 20 item array would be

myarray[19]

It is also common to use multi-dimensional arrays - for example to store the x,y co-ordinates of something

var myarray = new Array [20] [20]

A two dimensional array can be thought of as a matrix having rows and columns

Accessing a specific data element simply uses two indexes like this:

myarray[2] [3]

Searching and sorting is often done by storing data in an array and then using the appropriate algorithm to carry out the task.

### An algorithm

This is a computer programming term. An 'algorithm' is the set of steps needed to carry out a task, a bit like a recipe for doing something.

Note that algorithms are not language specific - they can be coded into any appropriate computer language.

There are thousands of algorithms defined by computer scientists over the last few decades. Algorithms help pass on the knowledge of how to do something rather than every programmer having to re-invent the wheel every time.

There are many computer books dedicated to algorithms, perhaps the most famous being *"The Art of Computer Programming" by Donald Knuth* which is a four-volume book covering some of the most widely used algorithms in use today.

**Challenge** see
if you can find out one extra fact on this topic that we haven't
already told you

Click on this link: Linear arrays algorithms

Copyright © www.teach-ict.com