Presentation is loading. Please wait.

Presentation is loading. Please wait.

Some comments on lab4. Hi Philippe! Can you tell me if my code works? Thanks! I’ll show you what works…

Similar presentations


Presentation on theme: "Some comments on lab4. Hi Philippe! Can you tell me if my code works? Thanks! I’ll show you what works…"— Presentation transcript:

1 Some comments on lab4

2 Hi Philippe! Can you tell me if my code works? Thanks! I’ll show you what works…

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

4 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…

5 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…

6 Sorting by practice

7 We’ll learn sorting algorithms through activities. We’ll need 8 guinea pigs! Come! Come!

8 Activity #1 Sorting with constraints

9 Sorting by practice You can only speak to your immediate neighbour and change position with this person. 5184 5 > 1, we should swap 8 > 5, good8 > 4, swap 5 > 4, swap

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

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

12 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 case The data is already sorted and you find it in one pass. O(n) Worst case The data is in descending order. 4321 3421 3241 3214 It 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 inO(n²)

13 Sorting by practice Sorting with constraints You can only speak to your immediate neighbour and change position with this person. Best case The data is already sorted and you find it in one pass. O(n) Worst case The 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!

14 Activity #2 Free sorting

15 Sorting by practice 5184 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. 60

16 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 case There 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²)

17 Free sorting Sorting by practice 5184 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. 60 sorted

18 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 case The data is already sorted: the 2nd loop stops right away. O(n) Worst case The data is in descending order: n shifts in O(n). O(n²)

19 Activity #3 Recursive sorting

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

21 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

22 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 sortTemporary array LeftRight Start General case Recursively cut in halves Combine sorted arrays Base

23 8 7 6 Recursive sorting Sorting by practice 1 3 5 2 4 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]; }

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

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

26 A family’s business Sorting by practice There are mainly four families, or types, of sorting algorithms. Exchange SelectionInsertionMerge We’ve seen one example for each family.

27 A family’s business Sorting by practice There are mainly four families, or types, of sorting algorithms. Exchange SelectionInsertionMerge We’ve seen one example for each family. Bubble sort, gnome sort… Selection sort, Heapsort… Insertion sort, Library sort… Merge sort, Strand sort…

28 Sorting by practice www.sorting-algorithms.com


Download ppt "Some comments on lab4. Hi Philippe! Can you tell me if my code works? Thanks! I’ll show you what works…"

Similar presentations


Ads by Google