2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.

Slides:



Advertisements
Similar presentations
2014-T2 Lecture 25 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Advertisements

Lecture16: Heap Sort Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Heapsort By: Steven Huang. What is a Heapsort? Heapsort is a comparison-based sorting algorithm to create a sorted array (or list) Part of the selection.
Data Structures, Search and Sort Algorithms Kar-Hai Chu
Review. What to know You are responsible for all material covered in lecture, the readings, or the programming assignments There will also be some questions.
Sorting CS221 – 3/2/09. Recursion Recap Use recursion to improve code clarity Make sure the performance trade-off is worth it Every recursive method must.
CSE332: Data Abstractions Lecture 12: Introduction to Sorting Tyler Robison Summer
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
Data Structures & Algorithms What The Course Is About s Data structures is concerned with the representation and manipulation of data. s All programs.
Review for Test 2 i206 Fall 2010 John Chuang. 2 Topics  Operating System and Memory Hierarchy  Algorithm analysis and Big-O Notation  Data structures.
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Computer Science and Software Engineering University of Wisconsin - Platteville 12. Heap Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus.
INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS Marcus Frean School of Engineering and Computer Science, Victoria University of Wellington COMP T2.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
FASTER SORTING using RECURSION : QUICKSORT COMP 103.
Information and Computer Sciences University of Hawaii, Manoa
Sorting Sanghyun Park Fall 2002 CSE, POSTECH. Sorts To Consider Selection sort Bubble sort Insertion sort Merge sort Quick sort Why do we care about sorting?
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Lecture 2 Sorting. Sorting Problem Insertion Sort, Merge Sort e.g.,
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
FASTER SORT using RECURSION : MERGE SORT 2015-T2 Lecture 15 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
2015-T2 Lecture 20 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
2014-T2 Lecture 27 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
COMP 103 wrapping up and some exam tips 2015-T2 Lecture 32 Marcus Frean School of Engineering and Computer Science, Victoria University of Wellington 
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
SORTING ALGORITHMS Christian Jonsson Jonathan Fagerström And implementation.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Heap Sort Uses a heap, which is a tree-based data type Steps involved: Turn the array into a heap. Delete the root from the heap and insert into the array,
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
Data Structures Using C++ 2E
FASTER SORT using RECURSION : MERGE SORT
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
CS302 Data Structures Fall 2012.
Data Structures & Algorithms Priority Queues & HeapSort
ITEC 2620M Introduction to Data Structures
IT 4043 Data Structures and Algorithms
CMPT 225 Lecture 10 – Merge Sort.
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW COMP 103 John Lewis Review 2

2  Help desk: Thursday 13 and 20 in CO254, 3pm (Michael and Roma’s office)  Also this week at regular time/place (3pm Co242a)

3 Exam  Answer the easy questions first  Look at previous year’s exams  Solidify your knowledge by comparing things  List vs Array? Queue vs list? List vs BST? Array vs BST? ….  Be able to describe to your friend: the algorithm, its complexity, its advantage, disadvantage  Other similar courses online, wikipedia, etc.

4 Implement one data structure in terms of others  Ex: priority queue using heap  Queue using linked list  Stack using linked list

5  Binary search – BST

6 Sorts  Heap, Tree, Merge, Quick…  Worst case time?  Memory?

7 Analysing Sorting Algorithms  Efficiency  What is the (worst-case) order of the algorithm ?  Is the average case much faster than worst-case ?  Requirements on Data  Does the algorithm need random-access data?  Does it need anything more than “compare” and “swap” ?  Space Usage  Can the algorithm sort in-place, or does it need extra space ?  Stability  Is the algorithm “stable” (will it ever reverse the order of equivalent items?)  Performance on Nearly Sorted Data  Is the algorithm faster when given sorted (or nearly sorted) data ? (All items close to where they should be, or only a few out of order.) 7

8 Design decisions...  We could sort Lists ⇒ general and flexible but efficiency depends on how the List is implemented or just sort Arrays ⇒ less general efficiency is well defined NB: method toArray() converts any Collection to an array  We could require items to be Comparable i.e. any item can call compareTo(otherObj)...on another ("natural ordering") OR provide a Comparator i.e. the sorter can call compare(obj1, obj2)...on any two items. We will sort an Array, using a Comparator: public void …Sort(E[] data, int size, Comparator comp) number of items 8 comparator array

9

Selecting Sorts  Selection Sort (slow)  HeapSort (fast) search for minimum here 10

11  Insertion Sort (slow)  Shell Sort (pretty fast)  Merge Sort (fast) (Divide and Conquer) Inserting Sorts How would you describe this in words?

12

13 HeapSort Given an array of n items: (a) re-order them into a Heap from 0 to n-1: for i = (n-1)/2 down to 0 pushdown(i) now we have a heap! "heapify"

14 HeapSort Given an array of n items: (a) re-order them into a Heap from 0 to n-1: for i = (n-1)/2 down to 0 pushdown(i) (b) for pos = n -1 down to 0 treat 0 ⋯ pos-1 as a heap poll item and place into position pos etc etc ! Sorted → in-place dequeueing... "heapify" and so on!

15 HeapSort (a) Turn data into a heap (b) Repeatedly swap root with last item and push down public void heapSort(E[] data, int size, Comparator comp) { for (int I = (size-1)/2; I >= 0; i--) pushDown(i, data, size, comp); while (size > 0) { size--; swap(data, size, 0); pushDown(0, data, size, comp); } "heapify" in-place dequeueing

16 recursion Y X B X B M M A A Prints: X Y B A M

17 recursion Y X B X B M M A A Prints: X Y B A M

18 Programming review  == versus equals()  Assignment: copy versus reference ArrayList a = new ArrayList (); a.add(1); a.add(2); a.add(3); a.add(4); ArrayList b = a; b.set(1,100); printArr(a); printArr(b);