420 likes | 541 Views
Reaching Wider Summer School. Day 2 – Logic and Algorithms. Algorithms. What are algorithms A set of step by step instructions To solve a problem Written in easy to understand language – not strictly controlled Like a recipe in cooking Algorithm efficiency can be measured…improved?
E N D
Reaching Wider Summer School Day 2 – Logic and Algorithms
Algorithms • What are algorithms • A set of step by step instructions • To solve a problem • Written in easy to understand language – not strictly controlled • Like a recipe in cooking • Algorithm efficiency can be measured…improved? • Algorithms are not programs
Lets give this a go • How do you make a cup of tea?
How did we do • Did we miss anything? • Is anything unclear? • How efficient are we? • Can we do better?
Pseudo code • More refined use of natural language • Still user defined • Often includes programming language like statements • If this and this then do that End; • Less ambiguous that natural language • “Time flies like an arrow, fruit flies like a banana”
Back to my cup of tea • Lets try and refine this a little more. • Still a large step away from a program
Why write algorithms • Aren’t they time consuming? • Could we just use this time programming • Programming prone to errors • Algorithms help us design our program • Avoid mistakes in our program – BUGS! • Can be analysed, compared, and improved without costly implementation
Possibly more useful • Lets try something a little more programming orientated • Reading a text file containing a list of text
Did we get it? • Get file name • Open file • while(reading a line != end of file) • Store string from line to memory • Close the file – Important! Alternative • Get file name • Open file • until(reading a line = end of file) do • Store string from line to memory • Close the file – Important!
Code examples • Java: BufferedReader in = new BufferedReader(new FileReader("infilename")); String str; while ((str = in.readLine()) != null) { process(str); } in.close(); • “process(str)” is a mathod that takes a String input parameter
Code examples • C – FILE *fp;char str[128];if((fp = fopen(argv[ 1 ], "r"))==NULL) {printf("Cannot open file.\n"); exit(1); }while(!feof(fp)) {if(fgets(str, 126, fp)) printf("%s", str); }fclose(fp);return 0;
Algorithms • Not restricted to any one language • Can (should) be readable by anyone with experience in any programming langauge. • In fact readable by anyone at all • Crucial for the design of good quality code
Sorting! • Sorting another classical problem in computer science • Many, many different algorithms developed • http://en.wikipedia.org/wiki/Sorting_algorithm Lists a lot of examples. • Today we’re going to look at “Bubble sorting”
Why Sort? • Already seen that a sorted list can be searched must faster than an unsorted list • Sort once. Improves search always. • Adding an element to a sorted list is possible • Insertion short
Bubble sort • Pretty strange name • So called because the smaller values “bubble” up to the top of this list. • Slow – only practically used for very small sets of data
Lets take a look at the algorithm • Sorted = false; • While sorted = false; • Sorted = true; • For all elements n in a list • If n > n +1 • Swap • Sorted = false; • N = n +1;
Demonstration • https://cs.senecac.on.ca/~catherine.leung/sketches/bubble.html
Practicality • How do we do the swap? • Array[i] = Array[i+1]; • Array[i+1] = Array[i]; • That wont work, we’re overwriting Array[i+1] before assigning its value to Array[i]. • So what do we do.
Practicality • Simples • We can use a local variable • int tmp; • tmp = Array[i]; • Array[i] = Array[i+1]; • Array[i+1] = tmp; • Now value of Array[i] is stored before being overwritten.
How can we do better? • Example • Merge sort • Example of a recursive algorithm • An algorithm that calls itself • Let me explain
Algorithm • mergeSort( input:Array) • If (Array.length > 1) • arrayFirst = Array[0..n/2] • arraySecond = Array[n/2+1 ..n] • mergeSort(arrayFirst); • mergeSort(arraySecond); • Combine arrayFirst with arraySecond • With elements in order
Algorithm • mergeSort( input:Array) • If (Array.length > 1) • arrayFirst = Array[0..n/2] • arraySecond = Array[n/2+1 ..n] • mergeSort(arrayFirst); • mergeSort(arraySecond); • Combine arrayFirst with arraySecond • With elements in order Recursive Calls
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order
Demonstration Now merge each list in the right order Sorted!
Faster than Bubble Sort? • Yes • Sorting each recurrence takes 2*n/2+n time of the level above. • Worst case O(n log n) • Requires more memory than bubble sort