Download presentation
Presentation is loading. Please wait.
Published byOphelia Dean Modified over 9 years ago
1
Day 2 – Logic and Algorithms REACHING WIDER SUMMER SCHOOL
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. http://en.wikipedia.org/wiki/Sorting_algorithm 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 59378261 59378261 59378261 59378261
25
59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16
26
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 3
27
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35
28
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 357
29
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 3579
30
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791
31
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 357912
32
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 3579126
33
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268
34
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268 1
35
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268 12
36
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268 123
37
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268 1235
38
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268 12356
39
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268 123567
40
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268 1235678
41
DEMONSTRATION 59378261 59378261 59378261 59378261 593728 Now merge each list in the right order 16 35791268 12356789 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.