1+-+Algorithms

=Algorithms=

flat

=General Algorithms= Algorithms are sets of instructions that can be understood by computers if they were programmed to do so. You need to follow the instructions one step at a time,keeping track of any variables as they change. It is usually useful to draw a trace table to do this.

Sometimes you are asked to explain in words what a particular algorithm does. You cannot 'learn' every algorithm (only the important ones for each topic), so think about the INPUT and OUTPUT values to see what an algorithm does. Sometimes it helps to thinks what would happen if you had a different starting number(s).

Look at this interactive example to see how an algorithm works: media type="file" key="d11flowchart.swf" width="582" height="582"

=Bubble Sort= A bubble sort is a method for sorting a list of items in to ascending or descending order. It is one algorithm that can be performed by a computer over and over again very quickly.

The aim of the algorithm is to let items 'float' to one end of the list or another by comparing one item in the list with the item next to it and swapping places if necessary. Each 'pass' steps you through every position in the list, making swaps if necessary, until you get to the end of the list. Every pass guarantees that one more item will be in the correct position, so you have to make at most (n - 1) passes for a list of n items. Sometimes the algorithm gets there quicker.

It follows these steps strating with the first item in the list:
 * 1) Compare the current item with the next item;
 * 2) If they are in the correct order, move on one position and repeat step 1;
 * 3) If they are not in the correct order, SWAP THEM, and then move on one position and repeat step 1;
 * 4) When you get to the end of the list you have completed a PASS;
 * 5) When a pass is completed without any swaps, then the list is in the correct order. STOP.

Key points:
 * You must write down the list after a single pass, not after every swap;
 * You must write down the list the final time if there have been no swaps.

Use these powerpoints to go through an example step-by-step: media type="youtube" key="IK27o9W0q5s" height="450" width="600"

flat

=Quick Sort= A quick sort is a method for sorting a list of items into ascending or descending order. It is another useful method for a computer to organise items in to the correct order.

The algorithm creates lots of sub-lists by selecting pivots. These lists get ordered bit by bit until the whole list has been sorted.

It follows these steps:
 * 1) Choose the item at the mid-point to be the first pivot (select the item on the RHS if there are two middle items);
 * 2) Whilst keeping the order of the items you were given write the list as follows: items less than pivot, (pivot), items greater than pivot;
 * 3) The pivot is now fixed with 2 sub-lists, one before and one after the pivot (these sub-lists could be empty);
 * 4) Zoom in and repeat the process for each sub-list (maybe several times) until all items have been chosen as a pivot. STOP.

Key points:
 * If there are 2 middle items then always consistently choose the RHS item;
 * You must down the final list when all items have been chosen as pivots.

Look through these powerpoints to go through an example step by step. media type="youtube" key="Fju_NstEy3M" height="450" width="600"

flat

=Binary Search= A binary search will search a list that is in order to find out whether an item exists in the list, and what position that item is in.

The algorithm can be thought of as locating a target item in a list of n items:
 * 1) Get the list of n items in order (either manually or by using an appropriate algorithm) and label them 1, 2, 3, ...;
 * 2) Select the middle item m in the list using: (n + 1)/2 and rounding up;
 * 3) If the target item = m then the search is complete;
 * 4) If the target is before m, then delete all items including m and beyond to create a smaller list. Repeat step 2.
 * 5) If the target is after m, then delete all items up to and including m to create a smaller list. Repeat step 2.
 * 6) Keep shrinking the list until the target is found or showing that the target is not in the list. STOP.

Key Points:
 * You need to CLEARLY show which position is the middle and which items you are cutting;
 * At the end you must state whether you have found the item or not, and which position it is in.

Use this interactive example to demonstrate how it works: media type="file" key="d11binsearch.swf" width="645" height="645"

flat

=Bin Packing= Try and fit these items into the bins. Can you do it in the most efficient way? media type="file" key="binpacking.swf" width="456" height="420" There are 3 different (self-explanatory) algorithms you need to know for Bin Packing:
 * First fit;
 * First fit-decreasing;
 * Full-bin;

You might also be required to calculate the lower bound for the minimum number of bins needed to pack all the items. This is calculated by dividing the total 'weight' of the items by the capacity of a bin. Answers must be rounded up regardless.

Sometimes you are required to explain why your answer to a particular problem may or may not be the optimal solution (equal to the lower bound). Use your common sense as to why you might not be able to pair items together or consider whether combinations of items cannot possibly fit together.

First fit
In the first fit algorithm, you place the items in the order you are given them in to the first bin which they will fit. media type="file" key="d11firstfit.swf" width="600" height="600"

First fit decreasing
In the first fit decreasing algorithm, firstly put the items in decreasing order (biggest to smallest), then place them in the first available bin. media type="file" key="d11firstfitdec.swf" width="600" height="600"

Full-bin
For the full bin algorithm, try and link together items that will completely fill a bin. With the remaining items use the first fit algorithm to place them in empty bins.

flat