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.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Back to Sorting – More efficient sorting algorithms.
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.
Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
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.
HST 952 Computing for Biomedical Scientists Lecture 9.
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:
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.
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.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting and Asymptotic Complexity
Abstract Data Types (ADTs) Data Structures The Java Collections API
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
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.
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.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
CompSci 100e 11.1 Sorting: From Theory to Practice l Why do we study sorting?  Because we have to  Because sorting is beautiful  Example of algorithm.
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.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
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
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.
Sorting CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
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 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
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.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
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.
Quicksort 1.
Algorithm design and Analysis
Unit-2 Divide and Conquer
Quicksort.
Quicksort.
Workshop for CS-AP Teachers
CS203 Lecture 15.
Module 8 – Searching & Sorting Algorithms
Quicksort.
Presentation transcript:

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 important ability But how fast can be sort data…?

RHS – SWC 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 – SWC 4 Selection sort

RHS – SWC 5 Selection sort

RHS – SWC 6 Selection sort

RHS – SWC 7 Selection sort

RHS – SWC 8 Selection sort

RHS – SWC 9 Selection sort

RHS – SWC 10 Selection sort

RHS – SWC 11 Selection sort

RHS – SWC 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 – SWC 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 – SWC 14 Selection sort nn2n

RHS – SWC 15 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 – SWC 16 Merge sort The principle in merge sort is to merge two already sorted arrays:

RHS – SWC 17 Merge sort

RHS – SWC 18 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 – SWC 19 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 – SWC 20 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 – SWC 21 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 – SWC 22 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 – SWC 23 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 – SWC 24 Merge sort nn log(n)n2n

RHS – SWC 25 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 – SWC 26 Sorting in practice Sorting an array: Car[] cars = new Car[n]; … Arrays.sort(cars);

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

RHS – SWC 28 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 – SWC 29 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 – SWC 30 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 – SWC 31 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 – SWC 32 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 – SWC 33 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 – SWC 34 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; }... }