CS261 Data Structures. Ordered Array Dynamic Array Implementation

Similar documents
CS261 Data Structures. Ordered Bag Dynamic Array Implementa;on

CS 261: Data Structures. Sorted Dynamic Array Bag and Set

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

Data Structures and Algorithms for Engineers

Search Lesson Outline

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Sorting and Searching -- Introduction

ITEC2620 Introduction to Data Structures

CS 171: Introduction to Computer Science II. Quicksort

Sorting. Dr. Baldassano Yu s Elite Education

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

LECTURE 17. Array Searching and Sorting

C/C++ Programming Lecture 18 Name:

COMP Data Structures

We can use a max-heap to sort data.

Topic 14 Searching and Simple Sorts

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Sorting and Searching

Computational Geometry

CS302 - Data Structures using C++

Sorting/Searching and File I/O. Sorting Searching Reading for this lecture: L&L

3/20/2015. Chapter 19 Sorting and Searching SELECTION SORT INSERTION SORT MERGE SORT THE QUICKSORT ALGORITHM MERGE SORT VS SELECTION SORT

CSE 530A. B+ Trees. Washington University Fall 2013

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS261 Data Structures. Iterator ADT Dynamic Array and Linked List

Topics. Sorting. Sorting. 1) How can we sort data in an array? a) Selection Sort b) Insertion Sort

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

ECE 122. Engineering Problem Solving Using Java

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

Data structures and libraries

Floating Point Arithmetic

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Week 6. Data structures

Lecture 6 Sorting and Searching

CS 261 Data Structures. Big-Oh Analysis: A Review

CS171 Midterm Exam. October 29, Name:

Lecture 6: Divide-and-Conquer

Divide and Conquer Sorting Algorithms and Noncomparison-based

Binary Search Trees. EECS 214, Fall 2018

Computational Geometry

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Announcement. Submit assignment 3 on CourSys Do not hand in hard copy Due Friday, 15:20:00. Caution: Assignment 4 will be due next Wednesday

Merge Sort Goodrich, Tamassia Merge Sort 1

Agenda. We ve done Growth of functions Asymptotic Notations (O, o,,!, ) Now Binary search as an example Recurrence relations, solving them

Module 08: Searching and Sorting Algorithms

Recall from Last Time: Big-Oh Notation

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

Programming II (CS300)

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Chapter 2: Basic Data Structures

Analyzing Complexity of Lists

Merge Sort fi fi fi 4 9. Merge Sort Goodrich, Tamassia

Spring 2017 B-TREES (LOOSELY BASED ON THE COW BOOK: CH. 10) 1/29/17 CS 564: Database Management Systems, Jignesh M. Patel 1

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

Recursion. Chapter 11

CSE 214 Computer Science II Searching

Tail Recursion: working from the beginning towards the end.

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Programming II (CS300)

Sorting and Selection

Chapter 3: Arithmetic for Computers

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Chapter 10 Sorting and Searching Algorithms

Search Trees. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Binary Search Trees

Unit-2 Divide and conquer 2016

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Recursion. Chapter 11. Chapter 11 1

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

CS 310 Advanced Data Structures and Algorithms

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Cpt S 122 Data Structures. Sorting

CP222 Computer Science II. Searching and Sorting

Analysis of Algorithms. CS 1037a Topic 13

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Q1 Q2 Q3 Q4 Q5 Q6 Total

Sorting Pearson Education, Inc. All rights reserved.

CS 106 Introduction to Computer Science I

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

Recursion. Chapter 5

Sorting race.

Big- (O): c.) binary search O(logn)

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

Lecture 15: Algorithms. AP Computer Science Principles

Comparison of x with an entry in the array

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

Module 10. Recursion. Adapted from Absolute Java, Rose Williams, Binghamton University

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Introduction to Hashing

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.

looking ahead to see the optimum

Practical Session #11 - Sort properties, QuickSort algorithm, Selection

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Lecture 27: (PROVISIONAL) Insertion Sort, Selection Sort, and Merge Sort 10:00 AM, Nov 8, 2017

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Transcription:

CS261 Data Structures Ordered Array Dynamic Array Implementation

Recall Arrays Operations Add O(1+) for dynamic array Contains O(n) Remove O(n) What if our application needs to repeatedly call Contains to search for items (e.g. a phone book application)? O(n) would be too expensive.

Ordered Array Abstraction Same operations as Array Add Contains Remove Property: elements in sorted order This gives us a chance to search more efficiently

Applications of Ordered Collections So search can be sped up with ordering We ll go into detail later What about other set operations? Merging ordered collections? Maintain order! Set operations such as union, intersection, etc.

Fast Merge 5 9 10 12 17 1 8 11 20 32 5 9 10 12 17 1 8 11 20 32 1 5 9 10 12 17 1 8 11 20 32 1 5 5 9 10 12 17 1 8 11 20 32 1 5 8 5 9 10 12 17 1 8 11 20 32 1 5 8 9 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10

Fast Merge (cont.) 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 12 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 12 17 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 12 17 20 5 9 10 12 17 1 8 11 20 32 1 5 8 9 10 11 12 17 20 32

Set Operations: Similar to Merge You can quickly merge two ordered arrays into a new ordered array What is its complexity? O(n) Set operations (intersection, union, difference, subset) are similar to merge Try these on your own (See Chapter 9)

Fast Set Intersection 5 9 10 12 20 1 9 12 13 32 5 9 10 12 20 1 9 12 13 32 5 9 10 12 20 1 9 12 13 32 5 9 10 12 20 1 8 12 13 32 9 5 9 10 12 20 1 8 12 13 32 9 5 9 10 12 20 1 8 12 13 32 9 12......

Efficient Search for Ordered Arrays We ve already encountered the idea of searching an ordered list by repeatedly cutting it in half. The formal name for this process is binary search Each step cuts region containing the value in half Starting with n items, how many times can I cut in half before reaching a set of size one?

Binary Search: O(log n) A O(log n) search is much much faster than an O(n) search (for large n) Log 2 1,000,000 ~ 20 Log of largest unsigned integer value in 32-bit machines: log 2 (4294967295) is 32 Instead of 4 billion comparisons, you only need 32

Binary Search What are the requirements for performing a binary search? Random access to the elements Elements are already in sorted order

Binary Search Ordered Array: Intuition Compute the middle index Check for the value at that index If found the value, done, return the index If not found If value is less than value at the index, repeat with left half of array Else repeat with right half of array If value is not in array we would like to return index where it should be inserted to maintain order

Binary Search: Recursive Implementation /* this is an initial call into the recursive procedure */ int _binarysearch(type * data, int size, TYPE val) { return recbinarysearch(data,0,size-1, val); }

Binary Search: Recursive Implementation int _recbinarysearch(type * data, int low, int high, TYPE val) { int mid; if (low <= high) { mid = (low + high) / 2; if (EQ(data[mid],val)) return mid; if (LT(data[mid],val)) return _recbinarysearch(data, mid+1, high, val); else return _recbinarysearch(data, low, mid-1, val); } return low; }

Binary Search: Non-Recursive Version int _binarysearch(type * data, int size, TYPE val) { int low = 0; int high = size; int mid; while (low < high) { mid = (low + high) / 2; if (EQ(data[mid],val)) return mid; if (LT(data[mid],val)) low = mid + 1; else high = mid; } return low; }

Binary Search Ordered Array: Return Value If value is found, returns index of that value If value is not found, returns position where it can be inserted without violating ordering NOTE: returned index can be larger than a legal index

Summary Searching DynArr and LinkedLists are O(N) on average Binary Search provides O(log N) search but requires that We have random access to data (ie. data is in an array) The data is ordered This means, of course, that we can only do efficient binary search on an array (NOT a linked list)

} Bug?? In 2006, a bug was found by Google http://googleresearch.blogspot.com/2006/06/ Where is it? low + high can yield overflow when both are large How can we fix it? mid = low + ((high-low) / 2) int _binarysearch(type * data, int size, TYPE val) { int low = 0; int high = size; int mid; while (low < high) { mid = (low + high) / 2; if (EQ(data[mid],val)) return mid; if LT(data[mid],val)) low = mid + 1; else high = mid; } return low;

How can we maintain sorted order when adding elements? Option 1: Use a sorting algorithm? Option 2: Linear search for proper location, then insert? Option 3: Binary search for proper location, then insert?

Option 1: Sort upon add Add 1 item Put it at end: O(1) sort the items O(NlogN) Overall complexity of Adding 1 item: O(1 + NlogN) = O(NlogN)

Option 2: Linear search, insert Add 1 item Linear search to proper location Insert and shift later items ahead O(N) operations Overall complexity of 1 Insert: O(N)

Option 3: Binary Search Add 1 items _binary Search to find location: O(logN) insert at that location: O(N) Total Complexity of N Inserts: O(logN + N) = O(N) Faster than linear search option in practice (despite both being O(NN)) Binary search is a faster way to find insertion point

Which operation is now faster? Using a dynamic array for an ordered array, which of the following operations is made faster or slower by using a binary search? add(element) contains(element) remove(element) Which option (1-3) is best? sort linear search binary search

Your Turn Now that we have _binarysearch, how do the following change? addbag containsbag removebag Complete Worksheet #26