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

Slides:



Advertisements
Similar presentations
IS 2610: Data Structures Sorting Feb 16, Sorting Algorithms: Bubble sort Bubble sort  Move through the elements exchanging adjacent pairs if the.
Advertisements

CS 112 Introduction to Programming Sorting of an Array Debayan Gupta Computer Science Department Yale University 308A Watson, Phone:
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Sorting Algorithms and Average Case Time Complexity
CS203 Programming with Data Structures Sorting California State University, Los Angeles.
Data Structures and Algorithms
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
Merge sort, Insertion sort
C++ Plus Data Structures
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
1 7.5 Heapsort Average number of comparison used to heapsort a random permutation of N items is 2N logN - O (N log log N).
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Sort Algorithms.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CS 146: Data Structures and Algorithms July 9 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
1 Sorting اعداد: ابوزيد ابراهيم حامد سعد صبرة حميده الشاذلي عبدالاه السيد محمد احمد.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Recursion Method calls itself iteratively until a base case is met and usually containing the following: if-else for base case with return value increment/decrement.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Divide and Conquer Sorting Algorithms COMP s1 Sedgewick Chapters 7 and 8.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Prof. U V THETE Dept. of Computer Science YMA
Lecture 25: Searching and Sorting
Sorting Mr. Jacobs.
COP 3503 FALL 2012 Shayan Javed Lecture 16
Recitation 13 Searching and Sorting.
slides created by Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
CSE 154 Sorting reading: 13.3, 13.4
Adapted from slides by Marty Stepp and Stuart Reges
slides created by Marty Stepp
Sub-Quadratic Sorting Algorithms
slides created by Marty Stepp and Ethan Apter
slides created by Marty Stepp
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
CSE 373 Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
slides adapted from Marty Stepp
Application: Efficiency of Algorithms II
Application: Efficiency of Algorithms II
Stacks, Queues, ListNodes
Presentation transcript:

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!

Activity #1 Sorting with constraints

Sorting by practice You can only speak to your immediate neighbour and change position with this person > 1, we should swap 8 > 5, good8 > 4, swap 5 > 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 case The data is already sorted and you find it in one pass. O(n) Worst case The data is in descending order 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²)

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!

Activity #2 Free sorting

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

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²)

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

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²)

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

8 7 6 Recursive sorting Sorting by practice 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 SelectionInsertionMerge 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 SelectionInsertionMerge We’ve seen one example for each family. Bubble sort, gnome sort… Selection sort, Heapsort… Insertion sort, Library sort… Merge sort, Strand sort…

Sorting by practice