Day 2 – Logic and Algorithms REACHING WIDER SUMMER SCHOOL
Day 2 – Logic and Algorithms
REACHING WIDER SUMMER SCHOOL
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
WHY IS IT SLOW•
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 orderRecursive Calls
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
1
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
1 2
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
1 2 3
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
1 2 3 5
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
1 2 3 5 6
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
1 2 3 5 6 7
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
1 2 3 5 6 7 8
DEMONSTRATION5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 8 2 6 1
5 9 3 7 2 8
Now merge each list in the right order
1 6
3 5 7 9 1 2 6 8
1 2 3 5 6 7 8 9
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