Sorting. RHS – SOC 2 Sorting Searching in sorted data is much faster (O(log(n)), than searching in unsorted data (O(n)). Being able to sort data efficiently.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Introduction to Algorithms Quicksort
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Garfield AP Computer Science
Lab class 10: 1. Analyze and implement the following merge-sorting program. //import java.lang.*; public class MergeSorter { /** * Sort the elements of.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Reference: Tremblay and Cheston: Section 5.1 T IMING A NALYSIS.
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Chapter 7: Sorting Algorithms
CMPS1371 Introduction to Computing for Engineers SORTING.
Sorting Algorithms: Merge and Quick. Lecture Objectives Learn how to implement the simple advanced sorting algorithms (merge and quick) Learn how to implement.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Chapter 7: Sorting Algorithms
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Quicksort.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Abstract Data Types (ADTs) Data Structures The Java Collections API
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.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fourteen: Sorting and Searching.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 13 – Sorting and Searching.
Linked Lists. RHS – SOC 2 Linked lists We can already store collec- tions of objects in arrays and array lists – why would we need other data structures…?
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2013 by John Wiley & Sons. All rights reserved. SORTING AND SEARCHING CHAPTER Slides by Rick Giles 14.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Sorting and Searching.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Sorting. RHS – SWC 2 Sorting Searching in sorted data is much faster than searching in unsorted data Being able to sort data efficiently is thus a quite.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
EFFICIENCY & SORTING II CITS Scope of this lecture Quicksort and mergesort Performance comparison.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 14 Sorting and Searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
SORTING AND ASYMPTOTIC COMPLEXITY Lecture 13 CS2110 – Fall 2009.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Using recursion for Searching and Sorting
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Sorting Mr. Jacobs.
Unit-2 Divide and Conquer
14 SORTING AND SEARCHING CHAPTER
Quicksort.
Workshop for CS-AP Teachers
CS203 Lecture 15.
Module 8 – Searching & Sorting Algorithms
Quicksort.
Presentation transcript:

Sorting

RHS – SOC 2 Sorting Searching in sorted data is much faster (O(log(n)), than searching in unsorted data (O(n)). Being able to sort data efficiently is thus a quite important ability But how fast can be sort data…?

RHS – SOC 3 Selection sort A very simple algorithm for sorting an array of n integers works like this: –Search the array from element 0 to element (n-1), to find the smallest element –If the smallest element is element i, then swap element 0 and element i –Now repeat the process from element 1 to element (n-1) –…and so on…

RHS – SOC 4 Selection sort

RHS – SOC 5 Selection sort

RHS – SOC 6 Selection sort

RHS – SOC 7 Selection sort

RHS – SOC 8 Selection sort

RHS – SOC 9 Selection sort

RHS – SOC 10 Selection sort

RHS – SOC 11 Selection sort

RHS – SOC 12 Selection sort How fast is selection sort? We scan for the smallest element n times –In scan 1, we examine n element –In scan 2, we examine (n-1) element –…and so on A total of n + (n -1) + (n – 2) +… examinations The sum is n(n + 1)/2

RHS – SOC 13 Selection sort The total number of examinations is equal to n(n + 1)/2 = (n 2 + n)/2 The run-time complexity of selection sort is therefore O(n 2 ) O(n 2 ) grows fairly fast…

RHS – SOC 14 Selection sort nn2n

RHS – SOC 15 Exercise 1 Download the project selectionSortInJava from the PSL website Examine the code – see how selection sort is implemented in Java The project contains two helper classes ArrayUtil (generates a random array of integers), and StopWatch (can measure the time needed to execute some code). Using these classes, the program measures how long it takes to sort an array using selection sort Try to run the program with various array sizes. For each run, write down the array size and the elapsed time. Make sure to try some array sizes that take several seconds to complete Enter the data into an Excel spreadsheet, plot a curve from the data, and see how the run time behaves when the array size increases

RHS – SOC 16 Merge sort Selection sort is conceptually very simple, but not very efficient… A different algorithm for sorting is merge sort Merge sort is an example of a divide-and- conquer algorithm It is also a recursive algorithm

RHS – SOC 17 Merge sort The principle in merge sort is to merge two already sorted arrays:

RHS – SOC 18 Merge sort

RHS – SOC 19 Merge sort Merging two sorted arrays is pretty simple, but how did the arrays get sorted…? Recursion to the rescue! Sort the two arrays simply by appying merge sort to them… If the array has length 1 (or 0), it is sorted

RHS – SOC 20 Merge sort public void sort() // Sort the array a { if (a.length <= 1) return; // Base case int[] a1 = new int[a.length/2];// Create two new int[] a2 = new int[a.length – a1.length];// arrays to sort System.arraycopy(a,0,a1,0,a1.length);// Copy data to System.arraycopy(a,a1.length,a2,0,a2.length);// the new arrays MergeSorter ms1 = new MergeSorter(a1);// Create two new MergeSorter ms2 = new MergeSorter(a2);// sorter objects ms1.sort();// Sort the two ms2.sort();// new arrays merge(a1,a2);// Merge the arrays }

RHS – SOC 21 Merge sort All that is left is the method for merging two arrays A little bit tedious, but as such trivial… Time needed to merge two arrays to the total length of the arrays, i.e to n We can now analyse the run-time com- plexity for merge sort

RHS – SOC 22 Merge sort Merge sort of an array of length n requires –Two merge sorts of arrays of length n/2 –Merging two arrays of length n/2 The running time T(n) then becomes: T(n) = 2×T(n/2) + n

RHS – SOC 23 Merge sort If we re-insert the expression for T(n) into itself m times, we get T(n) = 2 m ×T(n/2 m ) + mn If we choose m such that n = 2 m, we get T(n) = n×T(1) + mn = n + n×log(n)

RHS – SOC 24 Merge sort The run-time complexity of merge sort is therefore O(n log(n)) Many other sorting algorithms have this run-time complexity This is the fastest we can sort, except under very special circumstances Much better than O(n 2 )…

RHS – SOC 25 Merge sort nn log(n)n2n

RHS – SOC 26 Exercise 2 Download the project mergeSortInJava from the PSL website Examine the code – see how merge sort is implemented in Java (the project contains the same helper classes as the selectionSortInJava project – ArrayUtil and StopWatch) Try to run the program with various array sizes. For each run, write down the array size and the elapsed time. Make sure to try some array sizes that take several seconds to complete Enter the data into an Excel spreadsheet, plot a curve from the data, and see how the run time behaves when the array size increases Compare the results with the results obtained for selection sort – when do the curves for run time cross each other (if at all)?

RHS – SOC 27 Sorting in practice It does matter which sorting algorithm you use… …but do I have to code sorting algorithms myself? No! You can – and should – use sorting algorithms found in the Java library

RHS – SOC 28 Sorting in practice Sorting an array: Car[] cars = new Car[n]; … Arrays.sort(cars);

RHS – SOC 29 Sorting in practice Sorting an arraylist: ArrayList cars = new ArrayList (); … Collections.sort(cars);

RHS – SOC 30 Sorting in practice Why not code my own sorting algorithms? Sorting algorithms in Java library are better than anything you can produce… –Carefully debugged –Highly optimised –Used by thousands You cannot beat them

RHS – SOC 31 Sorting in practice In order to sort an array of data, we need to be able to compare the elements ”Larger than” should make sense for the elements in the array Easy for numeric types (>) What about types we define ourselves…?

RHS – SOC 32 Sorting in practice If a class T implements the Comparable interface, objects of type T can be compared: public interface Comparable { int compareTo(T other); }

RHS – SOC 33 Sorting in practice In the interface definition, T is a type parameter It is used the same way as we use an arraylist ArrayList : an arraylist holding elements of type Car

RHS – SOC 34 Sorting in practice In order for the sorting algorithms to work properly, an implementation of the interface must obey these rules: The call a.compareTo(b) must return: –A negative number if a < b –Zero if a = b –A positive number if a > b

RHS – SOC 35 Sorting in practice The implementation of compareTo must define a so-called total ordering: –Antisymmetric: If a.compareTo(b) ≤ 0, then b.compareTo(a) ≥ 0 –Reflexive: a.compareTo(a) = 0 –Transitive: If a.compareTo(b) ≤ 0 and b.compareTo(c) ≤ 0, then a.compareTo(c) ≤ 0

RHS – SOC 36 Sorting in practice public class Car implements Comparable {... // Here using weight as ordering criterion // public int compareTo(Car other) { if (getWeight() < other.getWeight()) return -1; if (getWeight() == other.getWeight()) return 0; return 1; }... }

RHS – SOC 37 Exercises Programming P14.12, P For P14.14, read about the Comparator interface in Advanced Topic 14.5, page