Sorting Applications 1 / 9

Similar documents
Priority Queues 1 / 15

Analysis of Algorithms 1 / 18

Symbol Tables 1 / 15

COS 226 Algorithms and Data Structures Fall Midterm Solutions

cs2010: algorithms and data structures

About this exam review

Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE

Basic Data Structures 1 / 24

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: CS6402- Design & Analysis of Algorithm Year/Sem : II/IV UNIT-I INTRODUCTION

Lecture 6 Sorting and Searching

Priority Queues. 04/10/03 Lecture 22 1

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

07 B: Sorting II. CS1102S: Data Structures and Algorithms. Martin Henz. March 5, Generated on Friday 5 th March, 2010, 08:31

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Parallel Sorting Algorithms

Outline. 1 Hashing. 2 Separate-Chaining Symbol Table 2 / 13

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation (see videos)

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

Algorithms Interview Questions

The beautiful binary heap.

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING ADVANCED DATA STRUCTURES AND ALGORITHMS EXAM EXAMINATION JUNE 2014

Searching in General

ELEMENTARY SEARCH ALGORITHMS

1. AVL Trees (10 Points)

Sorting. Chapter 12. Objectives. Upon completion you will be able to:

having any value between and. For array element, the plot will have a dot at the intersection of and, subject to scaling constraints.

UCS-406 (Data Structure) Lab Assignment-1 (2 weeks)

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

PRIORITY QUEUES AND HEAPS

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

3.1 Symbol Tables. API sequential search binary search ordered operations

CS61BL. Lecture 5: Graphs Sorting

Bachelor Level/ First Year/ Second Semester/ Science Full Marks: 60 Computer Science and Information Technology (CSc. 154) Pass Marks: 24

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

CS210 Study Guide Swami Iyer. 1 Programming Model (Algorithms 1.1) 2. 2 Data Abstraction (Algorithms 1.2) 4

Example Algorithms. CSE 2320 Algorithms and Data Structures Alexandra Stefan. University of Texas at Arlington. Last updated 9/7/2016

The Algorithm Design Manual

Prelim 2, CS2110. SOLUTION

Faster Sorting Methods

CS 171: Introduction to Computer Science II. Quicksort

COS 226 Algorithms and Data Structures Fall Midterm

Today: Sorting algorithms: why? Insertion Sort. Inversions. CS61B Lecture #26. Last modified: Wed Oct 24 13:43: CS61B: Lecture #26 1

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

ITI Introduction to Computing II

ITI Introduction to Computing II

COMP : Trees. COMP20012 Trees 219

Modularity. Modular program development. Language support for modularity. Step-wise refinement Interface, specification, and implementation

CSE 373 Data Structures and Algorithms. Lecture 15: Priority Queues (Heaps) III

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Algorithms. Algorithms 3.1 SYMBOL TABLES. API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE

Minimum Spanning Trees

Sorting and Searching

Data Structures and Algorithms 1

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation

COS 226 Midterm Exam, Spring 2009

Chapter 7 Sorting. Terminology. Selection Sort

Greedy Approach: Intro


Algorithms: Design & Practice

PRIORITY QUEUES AND HEAPS

Algorithms and Data Structures CS-CO-412

Sorting and Searching

University of Waterloo CS240, Winter 2010 Assignment 2

Computer Science 385 Design and Analysis of Algorithms Siena College Spring Lab 8: Greedy Algorithms Due: Start of your next lab session

1/ COP 3503 FALL 2012 SHAYAN JAVED LECTURE 16. Programming Fundamentals using Java

Graphs and Network Flows ISE 411. Lecture 7. Dr. Ted Ralphs

CSE 431/531: Algorithm Analysis and Design (Spring 2018) Greedy Algorithms. Lecturer: Shi Li

Systems I: Programming Abstractions

Stacks, Queues (cont d)

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

CS/COE 1501

Algorithms. Algorithms 2.4 PRIORITY QUEUES. API and elementary implementations binary heaps heapsort event-driven simulation

PRIORITY QUEUES AND HEAPSORT

CSE 421 Greedy Alg: Union Find/Dijkstra s Alg

COS 226 Lecture 19: Minimal Spanning Trees (MSTs) PFS BFS and DFS examples. Classic algorithms for solving a natural problem

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

Algorithm Analysis Graph algorithm. Chung-Ang University, Jaesung Lee

CSE 2123: Collections: Priority Queues. Jeremy Morris

(a) Write code to do this without using any floating-point arithmetic. Efficiency is not a concern here.

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

COT 6405: Analysis of Algorithms. Giri Narasimhan. ECS 389; Phone: x3748

R16 SET - 1 '' ''' '' ''' Code No: R

Deliverables. Quick Sort. Randomized Quick Sort. Median Order statistics. Heap Sort. External Merge Sort

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

COMP Data Structures

Data Structures and Algorithms

Total No. of Questions :09] [Total No. of Pages : 02. II/IV B.Tech. DEGREE EXAMINATIONS, NOV/DEC First Semester CSE/IT DATA STRUCTURES USING C


Quantitative Finance COURSE NUMBER: 22:839:615 COURSE TITLE: Special Topics Oriented Programming 2

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

Practical Session 10 - Huffman code, Sort properties, QuickSort algorithm, Selection

HEAPS & PRIORITY QUEUES

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Algorithm Analysis Advanced Data Structure. Chung-Ang University, Jaesung Lee

QUICKSORT TABLE OF CONTENTS

Transcription:

Sorting Applications 1 / 9

Outline 1 Sorting Various Types of Data 2 Which Sorting Algorithm Should I Use? 3 Reductions 4 A Brief Survey of Sorting Applications 2 / 9

Sorting Various Types of Data Our implementations sort arrays of Comparable objects, which allows us to use Java s callback mechanism to sort arrays of objects of any type that implements the Comparable interface The sorting approach we are using is known in the classical literature as pointer sorting, so called because we process references to items and do not move the data itself An array might not remain sorted if a client is allowed to change the values of keys after the sort, so it is wise to ensure that key values do not change by using immutable keys 3 / 9

Sorting Various Types of Data There are many applications where we want to use different orders for the objects we are sorting, and the Comparator interface makes this possible package edu. princeton.cs. algs4 ; import java. util. Comparator ; public class Insertion { public static void sort ( Object [] a, Comparator c) { int N = a. length ; for ( int i = 1; i < N; i ++) { for ( int j = i; j > 0 && less (c, a[j], a[j - 1]); j - -) { exch (a, j, j - 1); private static boolean less ( Comparator c, Object v, Object w) { return c. compare (v, w) < 0; In typical applications, items have multiple instance variables that might need to serve as sort keys Insertion. sort (a, new Transaction. WhenOrder ()); Insertion. sort (a, new Transaction. HowMuchOrder ()); 4 / 9

Sorting Various Types of Data The Transaction data type with alternate orderings package edu. princeton.cs. algs4 ; import java. util. Arrays ; import java. util. Comparator ; public class Transaction implements Comparable < Transaction > { private final String who ; private final Date when ; private final double amount ; public static class WhoOrder implements Comparator < Transaction > { public int compare ( Transaction v, Transaction w) { return v. who. compareto (w. who ); public static class WhenOrder implements Comparator < Transaction > { public int compare ( Transaction v, Transaction w) { return v. when. compareto (w. when ); public static class HowMuchOrder implements Comparator < Transaction > { public int compare ( Transaction v, Transaction w) { if (v. amount < w. amount ) { return -1; if (v. amount > w. amount ) { return +1; return 0; 5 / 9

Sorting Various Types of Data A sorting method is stable if it preserves the relative order of equal keys in the array Example Chicago 09:00:00 Phoenix 09:00:03 Houston 09:00:13 Chicago 09:00:59 Houston 09:01:10 Chicago 09:03:13 Seattle 09:10:11 Seattle 09:10:25 Phoenix 09:14:25 Chicago 09:19:32 Chicago 09:19:46 Chicago 09:21:05 Seattle 09:22:43 Seattle 09:22:54 Chicago 09:25:52 Chicago 09:35:21 Seattle 09:36:14 Phoenix 09:37:44 Sorted by time Chicago 09:25:52 Chicago 09:03:13 Chicago 09:21:05 Chicago 09:19:46 Chicago 09:19:32 Chicago 09:00:00 Chicago 09:35:21 Chicago 09:00:59 Houston 09:01:10 Houston 09:00:13 Phoenix 09:37:44 Phoenix 09:00:03 Phoenix 09:14:25 Seattle 09:10:25 Seattle 09:36:14 Seattle 09:22:43 Seattle 09:10:11 Seattle 09:22:54 Sorted by location (unstable) Chicago 09:00:00 Chicago 09:00:59 Chicago 09:03:13 Chicago 09:19:32 Chicago 09:19:46 Chicago 09:21:05 Chicago 09:25:52 Chicago 09:35:21 Houston 09:00:13 Houston 09:01:10 Phoenix 09:00:03 Phoenix 09:14:25 Phoenix 09:37:44 Seattle 09:10:11 Seattle 09:10:25 Seattle 09:22:43 Seattle 09:22:54 Seattle 09:36:14 Sorted by location (stable) Insertion and merge sorts are stable, but selection, shell, quick, and heap sorts are not 6 / 9

Which Sorting Algorithm Should I Use? Performance characteristics of sorting algorithms algorithm stable? in-place? running time space selection no yes N 2 1 insertion yes yes between N and N 2 1 shell no yes N log N?, N 6/5? 1 merge yes no N log N N quick no yes N log N lg N 3-way quick no yes between N and N log N lg N heap no yes N log N 1 In some performance-critical applications, the focus may be on sorting numbers, and in such cases, we can develop efficient versions of our sorting programs by replacing Comparable with the primitive type name, and redefining less() or just replacing calls to less() with code like a[i] < a[j] Java s primary system sort method java.util.arrays.sort() represents a collection of methods, and uses 3-way quick sort for primitive-type methods and merge sort for reference-type methods 7 / 9

Reductions We can use sorting algorithms to solve other problems a technique in algorithm design known as reduction Examples Duplicates: finding duplicates in a collection of keys Median: finding the median (the value with the property that half the keys are no larger and half the keys are no smaller) of a collection of keys 8 / 9

A Brief Survey of Sorting Applications Search for information: keeping data in sorted order makes it possible to efficiently search through it using binary search Combinatorial search: a classic paradigm in artificial intelligence and in coping with intractable problems is To define: a set of configurations with well-defined moves from one configuration to the next; a priority for each move; a start configuration; and a goal configuration And then apply the well-known A algorithm, which uses a priority queue, to solve the problem Graph algorithms: priority queues play a fundamental role in graph-search algorithms, such as Kruskal s and Dijkstra s algorithms, where we proceed from item to item along edges Huffman compression: a classic data compression algorithm that makes use of a priority queue String processing: algorithms that process strings, which are of critical importance in modern applications in cryptography and genomics, are often based on sorting 9 / 9