SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Garfield AP Computer Science
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
CS 171: Introduction to Computer Science II Quicksort.
CSE 373: Data Structures and Algorithms
CSSE221: Software Dev. Honors Day 23 Announcements: Announcements: Pass in yesterday’s capsule quiz Pass in yesterday’s capsule quiz Homework 7 electronic.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
Sorting and Asymptotic Complexity
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
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.
Array operations II manipulating arrays and measuring performance.
Sorting and Lower bounds Fundamental Data Structures and Algorithms Ananda Guna January 27, 2005.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Recursion, Complexity, and Sorting By Andrew Zeng.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
FASTER SORTING using RECURSION : QUICKSORT COMP 103.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
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.
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Sorting Algorithms Jyh-Shing Roger Jang ( 張智星 ) CSIE Dept, National Taiwan University.
Sorts Tonga Institute of Higher Education. Introduction - 1 Sorting – The act of ordering data Often, we need to order data.  Example: Order a list of.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
2011-T2 Lecture 21 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, and Peter Andreae, VUW.
More about costs: cost of “ensureCapacity”, cost of ArraySet, Binary Search 2014-T2 Lecture 12 School of Engineering and Computer Science, Victoria University.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
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,
1 Computer Algorithms Lecture 8 Sorting Algorithms Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
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.
Data Structures and Algorithms Lecture 17, 18 and 19 (Sorting) Instructor: Quratulain Date: 10, 13 and 17 November, 2009 Faculty of Computer Science, IBA.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Fast Sorting COMP
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
FASTER SORT using RECURSION : MERGE SORT COMP 103.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
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
CSE 143 Lecture 23: quick sort.
Description Given a linear collection of items x1, x2, x3,….,xn
Data Structures and Analysis (COMP 410)
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Topics Sequential Search on an Unordered File
8/04/2009 Many thanks to David Sun for some of the included slides!
CSE 332: Data Abstractions Sorting I
Presentation transcript:

SORTING 2014-T2 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean

RECAP  ArraySet algorithms and costs  Binary Search is O(log n) TODAY  Two algorithms for sorting a list:  Selection Sort  Insertion Sort Announcements:  Test is getting closer – keep up to date with your work so it’s not stressful next week. 2 RECAP-TODAY

Why Sort ?  we can find items in an array much faster if they are sorted finding1-in-a-billion takes as long as the ArraySet takes to find1-in-30  But... constructing a sorted array one item at a time is very slow : O(n 2 )  for 10 million items you need  50,000,000,000,000 steps at 10nS per step ⇒ 500,000 seconds = 58 days.  There are sorting algorithms that are much faster if you can sort whole array at once: O(n log (n) )  for 10 million items you need  250,000,000 steps ⇒ 2.5 seconds 3

Ways of sorting:  Selecting sorts:  Find the next largest/smallest item and put in place  Builds the correct list in order  Inserting sorts:  For each item, insert it into an ordered sublist  Builds a sorted list, but keeps changing it  Compare-and-Swap sorts:  Find two items that are out of order, and swap them  Keeps “improving” the list 4

Ways to rate 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.) 5

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

 Insertion Sort (slow)  Shell Sort (pretty fast)  Merge Sort (fast) (Divide and Conquer) Inserting Sorts

Compare and Swap Sorts  Bubble Sort (easy but terrible)  QuickSort (the fastest) (Divide and Conquer) things bubble up quickly, but bubble down slowly things bubble up quickly, but bubble down slowly and so on... 8

Other Sorts  Radix Sort (only works on some data)  Permutation Sort(very slow)  Random Sort(Generate and Test)

Coding it: some 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 10 comparator array

Selection Sort public void selectionSort(E[ ] data, int size, Comparator comp){ // for each position, from 0 up, find the next smallest item // and swap it into place for (int i=0; i<size-1; i++){ int minIndex = i; for (int j=i+1; j<size; j++) if (comp.compare(data[j], data[minIndex]) < 0) minIndex=j; swap(data, i, minIndex); }

Efficiency of Selection Sort  Cost:  step ?  best case: what is it ? cost:  worst case: what is it ? cost:  average case: what is it ? cost: 12

Selection Sort Analysis  Efficiency  worst-case: O(n 2 )  average case exactly the same.  Requirements on Data  Needs random-access data, but easy to modify for files  Needs compare and swap  Space Usage  in-place  Stability  ??  Performance on Nearly Sorted Data  No faster when given nearly sorted data 13

Insertion Sort public void insertionSort(E[] data, int size, Comparator comp){ // for each item, from 0, insert into place in the sorted region (0..i-1) for (int i=1; i<size; i++){ E item = data[i]; int place = i; while (place > 0 && comp.compare(item, data[place-1]) < 0){ data[place] = data[place-1]; place--; } data[place]= item; }

Efficiency of Insertion Sort  first: what is the step ?  best case: what is it ? cost:  worst case: what is it ? cost:  average case: what is it ? cost:  almost sorted cost: 15

Insertion Sort Analysis  Efficiency  worst-case: O(n 2 )  average case: half the cost of worst case.  Requirements on Data  Needs random-access data  Needs compare and swap  Space Usage  in-place  Stability  ??  Performance on Nearly Sorted Data  Very fast (O(n)) when given nearly sorted data 16

 InsertionSort, SelectionSort and BubbleSort are all slow (apart from Insertion on nearly-sorted lists)  Problem:  Selection compares every pair of items, ie. it ignores the results of previous comparisons.  Insertion (and Bubble) only compare adjacent items only move items one step at a time Doing better is possible, but we will need recursion... Why so slow? Can we do better? 17