Different types of Algorithms PDF

Title Different types of Algorithms
Course Computer Systems
Institution Canterbury Christ Church University
Pages 3
File Size 109.1 KB
File Type PDF
Total Downloads 64
Total Views 163

Summary

Computational algorithms, Problems solved by algorithms, Properties of a good algorithms, Using Pseudocode, Sorting algorithms, Bubble sort, Searching Algorithms, Writing “good” programs, Following an algorithm....


Description

Unit 3.2 Algorithms Algorithms  

An algorithm is a set of instructions to solve a problem or complete some well-defined task in a finite number of steps A recipe for chocolate cake, or a knitting pattern, are algorithms of a kind

Computational algorithms Examples: Merge sort, Selection sort, Bubble sort, Binary searches, and linear searches. There are thousands of real-world problems to be solved using different algorithms; some of them still unsolved

Problems solved by algorithms 

    

Routing problems; o Routing packets of data around the internet by the shortest route o Finding the shortest route for a salesman to cover his territory Timetabling commercial aircraft crews so that they do not exceed their permitted flight hours Searching information on the internet or from a database Encrypting communications so that they cannot be hacked Sorting large amounts of data Writing a compiler program to translate a high level language to machine code

Properties of a good algorithms     

Clear and precisely stated steps that produce the correct output for any set of valid inputs Should allow for invalid inputs Must always terminate at some point Should perform the task efficiently, in as few steps as possible. Should be designed in such a way that other people will be able to understand it wand modify it if necessary

Using Pseudocode  

Pseudocode is a halfway house between English statements and program code To a large extent, it is independent of any particular programming language

Unit 3.2 Algorithms Sorting algorithms 

There are many different sorting algorithms, from very simple one which execute very slowly, (e.g. an insertion sort) to more complex one which execute very fast (e.g. merge sort). On a given computer, a fast sort algorithm may sort ten million numbers in say 20 minutes, whereas an inefficient algorithm may take more than a month to sort the same data



Bubble sort  

This is one of the simplest sorts to understand Starting with the first item in the list, each item is compared with the adjacent item and swapped if it is larger At the end of the first pass the largest item ‘bubbles’ up to the end of the list



9 9 7

7

3

5 Worst in n x (n-1) check (loops)

7

9 9 3

3

5

7

3

9 9 5

5

3

7

5

3

5

7

1st pass 4 numbers it would 4 x 3 = 12 2nd pass

As n gets very large so  n2

9 9 9 9 9 9

Searching Algorithms   

Two of the most common searching algorithms are the linear search and the binary search. The linear search starts at the beginning of the list and examines every item The binary search uses “divide and conquer” strategy to halve the search area each time an item examined o It can only be used if the items are in sorted order – they could be alphabetic or numeric, for example;

Binary search Positives Much quicker for large lists (Efficient code) Negatives List must be sorted Harder to code

Unit 3.2 Algorithms 1st 2nd 3rd

1 5 8 10 28 33 60 1 5 8 8

Writing “good” programs  

Use comments to document your programs Use a standard for identifiers – e.g. o Start all variable names with a lowercase letter o Use CamelCaps rather than spaces or underscores Use properly indented code



Following an algorithm 

It is sometimes quite difficult to follow through an algorithm to determine its purpose or to find a logic error A trace table is a useful aid



a

b

a !=b

2 1 6

1 5

TRUE TRUE

Outpu t a b a !=b a > b 20 15 TRUE TRUE 5 FALSE

FALSE 9 3

3

a>b

TRUE FALSE

3

10 TRUE 5 FALSE

output

FALSE 5...


Similar Free PDFs