1 / 42

Reaching Wider Summer School

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?

barid
Download Presentation

Reaching Wider Summer School

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Reaching Wider Summer School Day 2 – Logic and Algorithms

  2. 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

  3. Lets give this a go • How do you make a cup of tea?

  4. How did we do • Did we miss anything? • Is anything unclear? • How efficient are we? • Can we do better?

  5. 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”

  6. Back to my cup of tea • Lets try and refine this a little more. • Still a large step away from a program

  7. 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

  8. Possibly more useful • Lets try something a little more programming orientated • Reading a text file containing a list of text

  9. 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!

  10. 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

  11. 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;

  12. 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

  13. 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”

  14. 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

  15. 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

  16. 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;

  17. Demonstration • https://cs.senecac.on.ca/~catherine.leung/sketches/bubble.html

  18. Why is it slow

  19. 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.

  20. 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.

  21. How can we do better? • Example • Merge sort • Example of a recursive algorithm • An algorithm that calls itself • Let me explain

  22. 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

  23. 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

  24. Demonstration

  25. Demonstration Now merge each list in the right order

  26. Demonstration Now merge each list in the right order

  27. Demonstration Now merge each list in the right order

  28. Demonstration Now merge each list in the right order

  29. Demonstration Now merge each list in the right order

  30. Demonstration Now merge each list in the right order

  31. Demonstration Now merge each list in the right order

  32. Demonstration Now merge each list in the right order

  33. Demonstration Now merge each list in the right order

  34. Demonstration Now merge each list in the right order

  35. Demonstration Now merge each list in the right order

  36. Demonstration Now merge each list in the right order

  37. Demonstration Now merge each list in the right order

  38. Demonstration Now merge each list in the right order

  39. Demonstration Now merge each list in the right order

  40. Demonstration Now merge each list in the right order

  41. Demonstration Now merge each list in the right order Sorted!

  42. 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

More Related