Top Banner
91074 – Algorithm Languages and User Interfaces Sorting: Bubble Sort, Selection Sort, Insertion Sort, Quick Sort. Comparisons, Cost of an Algorithm.
58

91074 – Algorithm Languages and User Interfaces

Feb 25, 2016

Download

Documents

Tudor Ciumara

91074 – Algorithm Languages and User Interfaces. Sorting: Bubble Sort, Selection Sort, Insertion Sort, Quick Sort. Comparisons, Cost of an Algorithm. Contents. Sorting Algorithms Informal Instructions, Algorithms and Programs High and Low Level Languages Translating Between Languages - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 91074 – Algorithm Languages and User Interfaces

91074 – Algorithm Languages and User InterfacesSorting: Bubble Sort, Selection Sort, Insertion Sort, Quick Sort. Comparisons, Cost of an Algorithm.

Page 2: 91074 – Algorithm Languages and User Interfaces

Contents•Sorting Algorithms

• Informal Instructions, Algorithms and Programs

•High and Low Level Languages

•Translating Between Languages

•User Interfaces

Page 3: 91074 – Algorithm Languages and User Interfaces

Sorting AlgorithmsSorting: Bubble Sort, Selection Sort, Insertion Sort, Quick Sort. Comparisons, Cost of an Algorithm.

Page 4: 91074 – Algorithm Languages and User Interfaces

Activity1) In groups, sort a small number of cards numerically2) What was your algorithm/strategy?

– tell your group.

3) New Rules: only allowed to look at two cards at a time, only allowed to swap two, no other moves.

4) What was your algorithm/strategy?

Page 5: 91074 – Algorithm Languages and User Interfaces

NotesSorting Algorithms: There are lots of ways computers sort lists. In this course we will look at Bubble sort, Selection Sort, Insertion Sort and Quick Sort.

Cost of an Algorithm: For sorting giant lists (like a phone book) on a computer, speed is important. We call this the ‘cost of the algorithm’ and we measure it by how many comparisons the algorithm has to make for a set list size.

Page 6: 91074 – Algorithm Languages and User Interfaces

Bubble Sort Instructions1) Compare the first two2) If out of order swap them3) Look at the next two, 4) Repeat Steps 2 and 3 until the (new) end of

the list.5) Repeat steps 1 – 4 until the list is sorted

Called Bubble Sort because like bubbles in a glass, the next one makes it way to the top

Page 7: 91074 – Algorithm Languages and User Interfaces

Bubble Sort Cost

E4E: Formula?

List size Comparisons required

5 10

6

7

8

10

Page 8: 91074 – Algorithm Languages and User Interfaces

Example Bubble Sort Video

Bubble Sort and Cost Video (sorting by height)youtube.com/watch?v=cdsfjU8qUss

Page 9: 91074 – Algorithm Languages and User Interfaces

Selection Sort1) Compare the first two and find the biggest2) Then compare the biggest with the next one3) Remember which is the (new) biggest4) Repeat steps 2 and 3 until the end5) Swap the biggest one with the (new) last one6) Repeat steps 1 to 5 until the list sorted

Called Selection, because you are selecting the biggest one and swapping it to the end.

Page 10: 91074 – Algorithm Languages and User Interfaces

Selection Sort Cost

E4E: Formula?

List size Comparisons required

5 10

6

7

8

10

Page 11: 91074 – Algorithm Languages and User Interfaces

Insertion Sort1) Compare the first two, and put them in order in a

new list2) Insert the next item into the right place in the new

list by comparing it with each item until you find it’s place.

3) Repeat step 2 until the new list is sorted, and the old list is empty.

Called Insertion, because you’re inserting into the correct position in a new list.

Page 12: 91074 – Algorithm Languages and User Interfaces

Insertion Sort Cost

E4E: Formula?

List size Comparisons required

5

6

7

8

10

Page 13: 91074 – Algorithm Languages and User Interfaces

Quick Sort (pivot)1) Select the next (first) item and make it a pivot2) Compare each item with the pivot, creating a list of

smaller items and a list of bigger items. 3) Repeat steps 1 and 2 for each sublist, until the list is

sorted

Is one of a group of “divide and conquer” sorting algorithms

Called Pivot, because you separate the list into smaller lists based on a pivot.

‘Pivot’, Friends on Youtube

Page 14: 91074 – Algorithm Languages and User Interfaces

Quick Sort Cost

E4E4E: Formula?

List size Comparisons required

5

6

7

8

10

Page 15: 91074 – Algorithm Languages and User Interfaces

Some ComparisonsWhich one is fastest, which is second fastest?

Sorting lists of 20Bubble Selection Insertion Quick

Click to see them race

Page 16: 91074 – Algorithm Languages and User Interfaces

Some ComparisonsWhich one is fastest, which is second fastest?

Sorting lists of 20Bubble Selection Insertion Quick

Page 17: 91074 – Algorithm Languages and User Interfaces

Some ComparisonsBubble Sort vs Quick Sort (Youtube) Lists of 30 coloured blocks, Comparing swaps, not Comparisons

1) Which has the least comparisons?2) Which has the least swaps?3) Is comparisons or swaps the best way to measure

“Cost of Algorithm?”

Page 18: 91074 – Algorithm Languages and User Interfaces

Comparing Sorting Comparisons

Watch Pivot Video (youtube)

List size (n)

BubbleComparisons

Selection Comparisons

InsertionComparisons

QuickComparisons

5 10 10 9~ 8~6 15 15 12.5~ 11~7 21 21 16.5~ 13~

10 45 45 31.5~ 25~20 190 190 114~ 63~

100 4950 4950 2574~ 500~Formula ½(n2 – n) ½n2 – ½n ¼n2 +¾n –1

(Average)n ln n

(Average)

Page 19: 91074 – Algorithm Languages and User Interfaces

Practice Task

Part A) Pick a sort, and six items. Step by step talk through sorting six items. E.g Carrot, Apple, Grape, Banana, Eggplant, Feijoa, - compare Carrot and Apple. (Out of order, so swap)- Compare Carrot and Grape (in order)- Compare Grape and …Part B) Do an insertion sort for 5, 6, 7 and 8 items, recording the cost.Part C) Explain how you calculated the cost and say why that is the cost.

Page 20: 91074 – Algorithm Languages and User Interfaces

Links

Getting sort data quicklyhttp://www.mundayweb.com/progs/applets/saas/Or search “Munday Saas”

Running your own raceshttp://www.sorting-algorithms.com/ Has eight common sorts

Page 21: 91074 – Algorithm Languages and User Interfaces

Informal Instructions Algorithms and ProgramsTheir Characteristics and Roles

Page 22: 91074 – Algorithm Languages and User Interfaces

Algorithm Notes

▫An algorithm is a precise unambiguous specification of how to accomplish some computational task in a finite number of well-defined steps.

▫An algorithm is distinct from a computer program.

▫An algorithm has a cost (the number of steps it will perform) for a task.

▫Different algorithms for the same task may have different costs.

Page 23: 91074 – Algorithm Languages and User Interfaces

Informal Instructions, Algorithms and Programs

Youtube Video (Eating a Chocolate)youtube.com/watch?v=cdsfjU8qUss

Page 24: 91074 – Algorithm Languages and User Interfaces

Friendship Algorithm

Page 25: 91074 – Algorithm Languages and User Interfaces

Characteristics of Informal Instructions, Algorithms, & Programs

Informal Instructions: Vague, Imprecise, easy to read/create, interpreted many waysMay have: gaps, bad spelling, incompleteForm: oral or written,

Algorithms: Precise, single task, has inputs/outputs, made up of steps, has a cost, Form: code, flow chart, text

Programs: Many tasks, made up of algorithms, single file, precise, self running, many tasksForm: file

Page 26: 91074 – Algorithm Languages and User Interfaces

When are they used (Roles)Informal Instructions, Algorithms, & Programs

Informal Instructions: Humans, communication, ease,

Algorithms: code, computer, part /functions in prog,

Programs: a variety of functions, self contained

Page 27: 91074 – Algorithm Languages and User Interfaces

Programming Concepts

Informal Instructions:

Algorithms:

Programs:

Page 28: 91074 – Algorithm Languages and User Interfaces

High and Low Level LanguagesCharacteristics

Page 29: 91074 – Algorithm Languages and User Interfaces

High and Low Level Languages Video

Youtube Video (Eating a Chocolate)youtube.com/watch?v=cdsfjU8qUss

Page 30: 91074 – Algorithm Languages and User Interfaces

Low Level Languages Machine Code (1GL)

•1st Generation Language (1GL)•Numbering system called “Binary”•Also called “Machine Code”•The only way to Program computers in

the 1960s and 70s•Old Explanation of Binary (before 3GL)Binary total 8s

column4s

column2s

column1s

column

2 0 0 1 0

7 0 1 1 1

Page 31: 91074 – Algorithm Languages and User Interfaces

Low Level Languages Machine Code (1GL)

Binary GameBinary total 8s

column4s

column2s

column1s

column

3 0 0 1 15 0 1 0 1

13 1 1 0 17 0 1 1 1

Page 32: 91074 – Algorithm Languages and User Interfaces

Low Level Languages Machine Code (1GL)

Flight of the Conchords•Binary Solo

Big Bang Theory•Chuck Norris of Numbers•Palindrome = Reverse ,

1001001 1001001

Page 33: 91074 – Algorithm Languages and User Interfaces

Low Level Languages Machine Code (1GL)

Characteristics• Lowest Level Language•Very Hard for humans to understand•Very slow to create code•No abstraction: Human knows exactly

what’s going on•Directly interacting with hardware•Doesn’t need translating or compiling•Native Language of Computer

Page 34: 91074 – Algorithm Languages and User Interfaces

Low Level Languages Assembly (2GL)

Page 35: 91074 – Algorithm Languages and User Interfaces

Low Level Languages Assembly (2GL)

• 2nd Generation Language (2GL)•One step up from Binary/Machine code• Has functions like Move (MOV), Compare

(CMP), Increment (INC), Jump(JMP) •Uses Hexadecimal (like web colours #ffffff )• Has no Compound functions like “If (this,

then do this)” or “Loop (3 times)”• At the end it is “Assembled” into Binary•Used when you need exact control over

code (e.g very small microchip, efficient code needed)

Page 36: 91074 – Algorithm Languages and User Interfaces

Low Level Languages Assembly (2GL)

Assembler ExampleHello WorldIgnore music and second half

“The main problem with “assembler” jokes is, they are difficult to read”— scapegrace

Page 37: 91074 – Algorithm Languages and User Interfaces

Low Level Languages Assembly (2GL)

Characteristics•Second Lowest Level Language•Hard for humans to understand•Slow to create code• Little abstraction: Human knows

almost exactly what’s going on•Needs Assembling into Binary /

Machine Code / 1GL translating or

Page 38: 91074 – Algorithm Languages and User Interfaces

High Level Languages (3GL)

Common Programming LanguagesLike Basic, C, Java, C# and Scratch

Page 39: 91074 – Algorithm Languages and User Interfaces

High Level Languages (3GL)

Features•Easier to Read and Write (and maintain)•Portable across different CPU families•Written as Text•Many High Level Languages, each with

their own Syntax

Page 40: 91074 – Algorithm Languages and User Interfaces

High Level Languages (3GL)

Brief History of 3GLs

•1957 Fortran (punch cards)

•1964 Basic MS Visual Basic

•1972 C (text colouring) C++

•1995 Java (Object Oriented)

•2001 C# (OO, components)

•2007 Scratch (Drag n Drop)

Page 41: 91074 – Algorithm Languages and User Interfaces

High Level Languages (3GL)

Characteristics•High Level Language• Lots of Abstraction into easily

readable code•Easy for humans to understand•Quick to create code•Needs Translating

into Machine code

Page 42: 91074 – Algorithm Languages and User Interfaces

HighLevel Languages (4GL)

Lots of Abstraction

Page 43: 91074 – Algorithm Languages and User Interfaces

High Level Languages (4GL)

Features•Flow charts and

modules

•4GL creates the code for you

•Often for a specific task e.g Managing a tax database

Page 44: 91074 – Algorithm Languages and User Interfaces

High Level Languages (4GL)

Characteristics• Examples: Ruby on Rails, Oracle, Powerbuilder • High Level Language • Lots of Abstraction sometimes

not even looking at code • Very Quick to create code•Needs Translating

into Machine code

“All 4GLs are designed to reduce programming effort, the time it takes to develop software, and the cost of software development. They are not always successful in this task, sometimes resulting

in inelegant and unmaintainable code.” (wikipedia)

Page 45: 91074 – Algorithm Languages and User Interfaces

Comparison of Language Levels1 GL 2 GL 3 GL 4 GL

Level Low Low High High

Example Language(s) Machine Code Assembler C, Java,

Scratch…Oracle,

PowerBuilder…

Write in Binary (1, 0) Hexadecimal, and Text Text Text, Diagrams

Abstraction None A little Lots Lots

Ease of Code Creation Very Difficult Difficult Easy Very Easy

Speed of Code Creation Very slow Slow Fast Very Fast

Control over Machine Code

Full Precise control

Full Precise control Some control No control

Inelegant Code

Portability None Same family of CPUs

Completely Portable

Completely Portable

Challenge Question

Page 46: 91074 – Algorithm Languages and User Interfaces

Low and High Levels

Invention of High Level Languages (3GL)High Level and Translators- 1983 Educational Cartoon

BASIC C Java

Page 47: 91074 – Algorithm Languages and User Interfaces

Translating between High and Low Level LanguagesCompilers, and Interpreters

Page 48: 91074 – Algorithm Languages and User Interfaces

Translating (Compiling) VideoYoutube Video (Eating a Chocolate)youtube.com/watch?v=cdsfjU8qUss

Page 49: 91074 – Algorithm Languages and User Interfaces

Translating between Levels

Why do we need to Translate high level languages into low ones? - Because Binary is the only language

the computer understands- Creates precise code rather than abstract- So code is portable

Page 50: 91074 – Algorithm Languages and User Interfaces

Translating between Levels

Interpreters and Compilerson Planet Gooble De Gook- Made in 1983

Two types of Translator 1) Compilers

Do the whole program at once at the endCan save program down as an .exe file

2) InterpretersTranslate the whole program one line at a timeCan cause errors or refuse to run

Page 51: 91074 – Algorithm Languages and User Interfaces

User InterfacesHeuristics

Page 52: 91074 – Algorithm Languages and User Interfaces

User Interface VideoYoutube Video (Box Crusher)youtube.com/watch?v=cdsfjU8qUss

Page 53: 91074 – Algorithm Languages and User Interfaces

Mr Bell’s New Home Nightmare

Accidentally open Garage door

Page 54: 91074 – Algorithm Languages and User Interfaces

Mr Bell’s New Home Nightmare

Door bell is locked behind front door

Page 55: 91074 – Algorithm Languages and User Interfaces

User Interfaces

We measure the usability of a user interface through a series of tests called “Heuristics”.

What is a User Interface?/ What is a User Interface’s role?- Communication between user and computer/machine- Makes the program do what user wants it to- Inputs like, Button, key, wheel, mouse, handle, Switch, - Outputs like, lights, sound, speakers, visual, screen,

heat, display- Manages/ Controls interactions

Page 56: 91074 – Algorithm Languages and User Interfaces

Heuristics (tests for User Interfaces)

1. Visibility of system status 2. Match between system and the real world 3. User control and freedom 4. Consistency and standards 5. Error prevention 6. Recognition rather than recall 7. Flexibility and efficiency of use 8. Aesthetic and minimalist design 9. Help users recognize, diagnose, and recover from

errors 10. Help and documentation Look these up online – Jakob Neilsen made them up.

Page 57: 91074 – Algorithm Languages and User Interfaces

User Interfaces

50 different things that have a user interfacePlaystation, Door, Computer, Toaster, Phone, Drill, Bike, Car, Clock, Gun, Printer, Office Chair, Distortion pedal, Ipod, TV, Stereo, Projector, Websites, lightbulb, Scissors, Pen, Power Plug, Window, Air Conditioning, Curtain, Fridge, Stove, Watch, Jacket zip, car jack, shoe, toilet, shower, cap, tap, tack, cat door, badge, earring, glasses, Potato peeler, Cheese Grater, knitting needles, sewing machine, watter bottle, knife, vhs, can opener, Milk bottle, Towel, chop sticks, light, calculator, filing cabinet, Microwave, elevator, .

Page 58: 91074 – Algorithm Languages and User Interfaces

User Interfaces

Broadly Different Example User Interfaces- Microwave, Computer, TV, Door, Switch, Playstation, Scissors, Pen, Tissues, Toothpaste, phone, ipod, draw, Lever, Shoes, stereo, socks, pants, plug, pin, clock, projector, printer, Light bulb, toilet, sink, tap, shower, bath, car, shovel, rifle, wrench, digger, back pack, fork, remote control car, shopping trolley, DVD, book, pram, finger nail clippers, curtains, some geeky thing, mouse, bike pump, Hospital beds, jet pack, balloon, envelope, aeroplane, boat, rubbish bin, umbrella, chocolate bar (in wrapper) fishing rod, pizza box, kayak, kite, drink bottle, map, bow and arrow, charger, calculator, website.