Some comments on lab4
Hi Philippe! Can you tell me if my
code works? Thanks!
I’ll show you what works…
Some comments on lab4
You should find out what works and what doesn’t.
There is no company where you can throw code at your coworkers and
ask them if it works.
Start debugging as soon as you can!
Example: getScript. First, handle sequential programs and ensure that
works. Then, add complexity by having IF/ELSE statements.
Some comments on lab4
You’re not working alone: your code should be clean so other people can read it.
Some of your functions
are like murder attempts
on the TA.
When your functions start being long, see if you can break them
into smaller ones!
Example: getScript. You should have a function to turn an IF into a Transition, to turn a line into a Node, to attach a transition, etc…
Some comments on lab4
The reason for which you are in a team is not so that one can stare at the other while he’s typing.
You should both be working at the same time. A program is modular: work on different classes! And debug them as you go…
Sorting by practice
We’ll learn sorting algorithms through activities.
We’ll need 8 guinea pigs! Come! Come!
Sorting with constraints
Sorting by practice
You can only speak to your immediate neighbour and change position with this person.
5 1 8 45 > 1, we should swap8 > 5, good 8 > 4, swap5 > 4, swap
Sorting with constraints
Sorting by practice
You can only speak to your immediate neighbour and change position with this person.
for(int i = 0; i<array.length-1; i++){if(array[i]>array[i+1]){
int tmp = array[i]; array[i] = array[i+1]; array[i+1]=tmp;
}}
One pass.
We may have to do it several times.
When do we stop?
You want to stop sorting when… it’s sorted!
How do you know it’s sorted? A pass does not swap any elements.
Sorting with constraints
Sorting by practice
You can only speak to your immediate neighbour and change position with this person.
for(int i = 0; i<array.length-1; i++){if(array[i]>array[i+1]){
int tmp = array[i]; array[i] = array[i+1]; array[i+1]=tmp;
}}
boolean changed; do{ changed = false;
}while(changed);
changed = true;}
}
Continue sorting as long as you are
still swapping something.
Sorting with constraints
Sorting by practice
You can only speak to your immediate neighbour and change position with this person.
for(int i = 0; i<array.length-1; i++){if(array[i]>array[i+1]){
int tmp = array[i]; array[i] = array[i+1]; array[i+1]=tmp;
}}
boolean changed; do{ changed = false;
}while(changed);
changed = true;}
}
Best caseThe data is already
sorted and you find it in one pass.
O(n)
Worst caseThe data is in
descending order.
4 3 2 1
3 4 2 1
3 2 4 1
3 2 1 4It takes n – 1 swaps to bring the 1st number
to the end, n – 2 for the 2nd number…n – 1 + n – 2 + n – 3 + … 1 = (n – 1)*(n/2), which is in O(n²)
Sorting by practice
Sorting with constraintsYou can only speak to your immediate neighbour and change position
with this person.
Best caseThe data is already
sorted and you find it in one pass.
O(n)
Worst caseThe data is in
descending order.
O(n²)
If the data is almost sorted, this
algorithm is efficient.
In most cases, it’s slow.
Now you know bubble sort!
Free sorting
Sorting by practice
5 1 8 4
An easy way to think is selection sort: find the minimum, put it at the 1st place, find the 2nd minimum, put it at the 2nd place, etc.
6 0
Free sorting
Sorting by practice
An easy way to think is selection sort: find the minimum, put it at the 1st place, find the 2nd minimum, put it at the 2nd place, etc.
for (int i = 0; i < a.length - 1; i++) {
int min = i;
for (int j = i + 1; j < a.length; j++)
if (a[j] < a[min])
min = j;
if (i != min) {
int swap = a[i]; a[i] = a[min]; a[min] = swap; }
}
Find min
Swap
Best caseThere is nothing that breaks the loops before completion.
No worst or best case.
n for the 1st min, n – 1 for
the 2nd… O(n²)
Free sorting
Sorting by practice
5 1 8 4
Another easy way to think is insertion sort: shift the number left as long as its neighbour is larger. In other words, do it like cards: in the left part
it’s sorted, and you take the next card in the right part and insert it.
6 0sorted
Free sorting
Sorting by practice
Another easy way to think is insertion sort: shift the number left as long as its neighbour is larger. In other words, do it like cards: in the left part
it’s sorted, and you take the next card in the right part and insert it.
for(int i = 1; i<array.length; i++){ for(int j = i; j > 0 && array[j] < array[j-1]; j--){
int tmp = array[j-1];array[j-1] = array[j];array[j] = tmp;
}}
For each number…
…shift it left as long as its neighbour is
larger
Best caseThe data is already sorted: the 2nd loop
stops right away.
O(n)
Worst caseThe data is in
descending order: n shifts in O(n).
O(n²)
Recursive sorting
Sorting by practice
First, sort yourself with your immediate neighbour.
Then, knowing that each pair is sorted, sort two adjacent pairs together.
And, knowing that each half is sorted, sort the overall thing.
Recursive sorting
Sorting by practice
First part: break the array into two arrays, until you have arrays
of size 1.
Second part: knowing that each array is
sorted, merge them.
This is
merge sort
Recursive sorting
Sorting by practice
private void mergeSort(int[] a, int[] tmp, int left, int right ) { if (right - left <= 1) return; int middle = left + (right - left) / 2; mergeSort(a, tmp, left, middle); mergeSort(a, tmp, middle, right); merge(a, tmp, left, middle, right);}
mergeSort( a, tmp, 0, a.length - 1 );
Array to sort Temporary array
Left Right
Start
General case
Recursively cut in halves
Combine sorted arrays
Base
87
6
Recursive sorting
Sorting by practice
135
24
private void merge(int[] a, int[] tmp, int left, int middle, int right) { int i = left, j = middle; for (int k = left; k < right; k++) { if (i == middle) tmp[k] = a[j++]; // no more left else if (j == right) tmp[k] = a[i++]; // no more right else if (a[j]<a[i]) tmp[k] = a[j++]; // left is smaller else tmp[k] = a[i++]; // right is smaller } for (int k = left; k < right; k++) a[k] = tmp[k]; }
Recursive sorting
Sorting by practice
How would you go about computing the time complexity?
T(n) = 2.T(n/2) + O(n)
You divide in half
Each half is processed
And the process to merge two halves (previous
slide) is in O(n)
Apply the recursive formula: T(n) = c.T(n/d) + O(n^k), case c = d^k.
Result: T(n) is in O(n^k . log n)
Merge sort is in O(n.log n) for time.
Recursive sorting
Sorting by practice
…what about space complexity?
Merge sort is in O(n.log n) for time.
We needed to create a temporary array of the same size than the array to be sorted, hence O(n).
We say that an algorithm is in place when its space complexity is O(1). Otherwise, it is not-in-place.
Merge sort is the first algorithm that we see which is not-in-place.
A family’s business
Sorting by practice
There are mainly four families, or types, of sorting algorithms.
Exchange Selection Insertion Merge
We’ve seen one example for each family.
A family’s business
Sorting by practice
There are mainly four families, or types, of sorting algorithms.
Exchange Selection Insertion Merge
We’ve seen one example for each family.
Bubble sort, gnome sort…
Selection sort, Heapsort…
Insertion sort, Library sort…
Merge sort, Strand sort…