3 The Array Data Structure

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
CS4413 Divide-and-Conquer
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
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.
6-1 CM0551 Week 6 The Array Data Structure Properties of arrays and subarrays – recap. Sorting: insertion, quick-sort and their time and space complexity.
Data Structures 5 Sorting Prof A Alkhorabi Overview Why Sorting? Simple Sorting – Selection & Insertion Sorts Sorting Performance Shell Sort Quicksort.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting III 1 An Introduction to Sorting.
Quicksort.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
Quicksort
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Data Structure Introduction.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
CS 367 Introduction to Data Structures Lecture 11.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
Binary Tree Data Structures Binary trees and binary search trees. Searching. Insertion. Deletion. Traversal. Implementation of sets using BSTs.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Searching and Sorting Searching algorithms with simple arrays
Chapter 23 Sorting Jung Soo (Sue) Lim Cal State LA.
Analysis of Algorithms CS 477/677
Sorts, CompareTo Method and Strings
Sorting Mr. Jacobs.
Searching.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
The Tree Data Structure
Recitation 13 Searching and Sorting.
Quicksort
Chapter 7 Sorting Spring 14
Teach A level Computing: Algorithms and Data Structures
Data Structures and Algorithms
Divide and Conquer – and an Example QuickSort
Quicksort 1.
Chapter 4: Divide and Conquer
Algorithm design and Analysis
Advanced Sorting Methods: Shellsort
Quick Sort (11.2) CSE 2011 Winter November 2018.
Unit-2 Divide and Conquer
Data Structures Review Session
Sorting … and Insertion Sort.
24 Searching and Sorting.
Sub-Quadratic Sorting Algorithms
Chapter 4.
EE 312 Software Design and Implementation I
CSE 332: Data Abstractions Sorting I
CS 3343: Analysis of Algorithms
Quicksort.
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Searching.
Topic: Divide and Conquer
Data Structures & Algorithms
Quicksort.
CSE 332: Sorting II Spring 2016.
CS203 Lecture 15.
Divide and Conquer Merge sort and quick sort Binary search
Quicksort.
Advanced Sorting Methods: Shellsort
Presentation transcript:

3 The Array Data Structure Properties of arrays and subarrays. Insertion. Deletion. Searching: linear and binary search. Merging. Sorting: selection-, insertion-, merge-, and quick-sort. © 2001, D.A. Watt and D.F. Brown

Properties of arrays in general An array is a sequence of indexed components: array indices low+1 a low+2 high–1 high low high–2 array components The length of the array (its number of components) is fixed when the array is constructed. Each array component has a fixed and unique index. The indices range from a lower bound to an upper bound. Any array component can be efficiently accessed (inspected or updated) using its index, in O(1) time.

Properties of arrays in Java A Java array’s components are either values of some stated primitive type, or objects of some stated class. A Java array of length n has lower bound 0 and upper bound n–1. A Java array is itself an object. It is allocated dynamically by “new T[n]”. n–1 length 1 n–2 a n T[] class tag array components

Example 1: Java primitive array Code to create, initialize, and inspect an array of integers: int[] primes = {2, 3, 5, 7, 11, 13}; for (int i = 0; i < primes.length; i++) System.out.println(primes[i]); primes 5 length 1 4 2 3 7 11 6 int[] class tag 13

Example 2: Java object array (1) Suppose that a Date object has fields y, m, d. Code to create an array of Date objects: Date[] hols = new Date[3]; length hols 3 Date[] class tag 1 2

Example 2 (2) Code to update the array of Date objects: hols[0] = new Date(2002, 1, 1); hols[1] = new Date(2001, 5, 1); hols[2] = new Date(2001, 12, 25); length 1 hols 3 Date[] class tag 2 2002 Date 5 2001 y m d 12 25

Subarrays A subarray is a sequence of consecutive components that forms a part of a larger array. Notation: let a[l…r] be the subarray consisting of components a[l], …, a[r]. Subarray notation is used here, but not supported by Java. 8 1 7 a 2 3 4 5 6 9 subarray a[1…3] subarray a[6…9] Length of subarray a[l…r] is r – l + 1.

Sorted arrays A (sub)array is sorted if its components are in ascending order, i.e., each component is less than or equal to the component on its right. The meaning of the comparison “x is less than y” (or “y is greater than x”) must be defined for each data type. Meaning of less for numbers: x is numerically less than y, i.e., x < y. Conventional meaning of less for strings: x precedes y lexicographically. E.g.: “bat” is less than “bath”, which is less than “bay”.

Interface java.util.Comparable Java provides: public interface Comparable { public int compareTo (Object that); // Return a negative integer if this object is less than that, // or zero if this object is equal to that, // or a positive integer if this object is greater than that. } The compareTo method captures the notion of less (greater) for objects. If a class implements Comparable, it must implement compareTo in accordance with its “contract”.

Insertion (1) Problem: Given a (sub)array a[left…right], insert a value val at a[ins]. If necessary, shift values right to make way for it. Array insertion algorithm: To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate.

Insertion (2) Animation: To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate. The left = 0 1 2 3 4 5 6 = right fat cat sat on the mouse a val ins To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate. The left = 0 1 2 3 4 5 6 = right fat cat sat on the mouse a val ins To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse mat fat a val ins To insert val at index ins in a[left…right] (where left  ins  right): 1. Copy a[ins…right–1] into a[ins+1…right]. 2. Copy val into a[ins]. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse fat a val ins

Insertion (3) Analysis (counting copies): Let n = right – left + 1 be the length of the array. Step 2 performs 1 copy. Step 1 performs between 0 and n–1 copies, say (n–1)/2 copies on average. Average no. of copies = (n – 1)/2 + 1 = n/2 + 1/2 Time complexity is O(n).

Deletion (1) Problem: Given a (sub)array a[left…right], delete the value in a[del]. If necessary, shift values left to fill the gap. (Assume that left  del  right.) Array deletion algorithm: To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate.

Deletion (2) Animation: To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse a del To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse a del To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate. The left = 0 1 2 3 4 5 6 = right fat cat sat on the mouse a del To delete the value at index del in a[left…right] (where left  del  right): 1. Copy a[del+1…right] into a[del…right–1]. 2. Make a[right] unoccupied. 3. Terminate. The left = 0 1 2 3 4 5 6 = right cat sat on the mouse a del

Deletion (3) Analysis (counting copies): Let n = right – left + 1 be the length of the array. Step 1 performs between 0 and n–1 copies. Average no. of copies = (n – 1)/2 = n/2 – 1/2 Time complexity is O(n).

Searching Problem: Given a (sub)array a[left…right], find which (if any) component equals a given target value. Choice of algorithms: linear search (unsorted or sorted array) binary search (sorted array).

known not to equal target Linear search (1) Linear search algorithm: To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. Loop invariant: left+1 a p right–1 left p–1 right known not to equal target still to be searched

Invariants An invariant is a logical statement that always holds at a particular step in an algorithm (or program). A loop invariant is an invariant that holds at every iteration of a loop. Invariants can be expressed: in logical notation (e.g., 0  i < n) as a schematic diagram (as above).

Linear search (2) Animation (successful search): To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 2 3 4 5 8 = right cat pig cow fox lion tiger a target goat dog 6 7 p

Linear search (3) Animation (unsuccessful search): To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right 2 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right 3 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right 2 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right 3 p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right p To find which (if any) component of a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 2. Terminate with answer none. rat left = 0 1 cat pig dog a target 2 = right p

Linear search (4) Analysis (counting comparisons): Let n = right – left + 1 be the length of the array. If the search is unsuccessful, step 1.1 is repeated n times. No. of comparisons = n If the search is successful, step 1.1 is repeated between 1 and n times. Average no. of comparisons = (n + 1)/2 In either case, time complexity is O(n).

Linear search (5) Implementation in Java: static int linearSearch (Object target, Object[] a, int left, int right) { // Find which (if any) component of a[left…right]equals // target. for (int p = left; p <= right; p++) { if (target.equals(a[p])) return p; } return NONE; // –1, say }

Linear search (6) If the (sub)array is known to be sorted, linear search can be speeded up in the unsuccessful case: To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. For p = left, …, right, repeat: 1.1. If target equals a[p], terminate with answer p. 1.2. If target is less than a[p], terminate with answer none. 2. Terminate with answer none. However, binary search is much better!

Binary search (1) Assume that the (sub)array is sorted. Consider searching a dictionary for a target word. Bad idea: Look at page 1, then page 2, etc., until you find the page containing the target word. That is linear search! Better idea: Choose a page near the middle. If the target word happens to be on that middle page, we’re finished. If the target word is less (greater) than the words on that middle page, focus on the pages before (after) that middle page, and repeat. That is binary search.

Binary search (2) Binary search algorithm: To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none.

Binary search (3) Loop invariant: a known to be less than target left r l–1 a right r+1 known to be less than target still to be searched known to be greater than target

Binary search (4) Animation (successful search): To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a target rat tiger 6 7 l 8 r m

Binary search (5) Animation (unsuccessful search): To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l 8 r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l 8 r To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l 8 r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m To find which (if any) component of the sorted (sub)array a[left…right] equals target: 1. Set l = left, and set r = right. 2. While l  r, repeat: 2.1. Let m be an integer about midway between l and r. 2.2. If target equals a[m], terminate with answer m. 2.3. If target is less than a[m], set r = m–1. 2.4. If target is greater than a[m], set l = m+1. 3. Terminate with answer none. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig dingo a target rat tiger 6 7 l r m

Binary search (6) Analysis (counting comparisons): Let n be the length of the array. Assume that steps 2.2–4 perform a single comparison. If the search is unsuccessful, these steps are repeated as often as we must halve n to reach 0: No. of comparisons = floor(log2 n) + 1 If the search is successful, these steps are repeated at most that many times: Max. no. of comparisons = floor(log2 n) + 1 In either case, the time complexity is O(log n).

Binary search (7) Implementation in Java: static int binarySearch (Comparable target, Comparable[] a, int left, int right) { // Find which (if any) component of the sorted (sub)array // a[left…right] equals target. int l = left, r = right; while (l <= r) { int m = (l + r)/2; int comp = target.compareTo(a[m]); if (comp == 0) return m; else if (comp < 0) r = m - 1; else l = m + 1; } return NONE; }

Comparison of searching algorithms No. of comparisons Time complexity Linear search (unsorted array) ~ n/2 successful n unsuccessful O(n) Linear search (sorted array) ~ n/2 Binary search ~ log2 n O(log n)

Merging (1) Problem: Given two sorted arrays, make a third sorted array containing copies of all components of the two original two arrays.

Merging (2) Array merging algorithm: To merge a1[l1…r1] and a2[l2…r2] into a3[l3…r3] (where both a1 and a2 are sorted): 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. 3. If i  r1, copy a1[i…r1] into a3[k…r3]. 4. If j  r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate.

Merging (3) Loop invariant: a1 already merged still to be merged a2 j l2 r2 j–1 a2 already merged still to be merged k l3 k–1 a3 r3 copied from a1 and/or a2 unoccupied

Merging (4) Animation: To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: … 3. If i  r1, copy a1[i…r1] into a3[k…r3]. 4. If j  r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate. cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: … 3. If i  r1, copy a1[i…r1] into a3[k…r3]. 4. If j  r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate. cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: … 3. If i  r1, copy a1[i…r1] into a3[k…r3]. 4. If j  r2, copy a2[j…r2] into a3[k…r3]. 5. Terminate. cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i To merge a1[l1…r1] and a2[l2…r2] into a3[l3…]: 1. Set i = l1, set j = l2, and set k = l3. 2. While i  r1 and j  r2, repeat: 2.1. If a1[i] is less than or equal to a2[j]: 2.1.1. Copy a1[i] into a3[k], then increment i and k. 2.2. If a1[i] is greater than or equal to a2[j]: 2.2.1. Copy a2[j] into a3[k], then increment j and k. … cow l1 = 0 1 = r1 goat a1 cat l2 = 0 1 2 3 4 = r2 dog fox lion tiger a2 l3 = 0 4 5 a3 6 k j i

Merging (5) Analysis (counting copies): Let n1 and n2 be the lengths of a1 and a2. Let n = n1 + n2. Each component of a1 is copied once, and each component of a2 is copied once. No. of copies = n1 + n2 = n Time complexity is O(n).

Merging (6) Analysis (counting comparisons): Let n1 and n2 be the lengths of a1 and a2. Let n = n1 + n2. Assume that steps 2.1–2 perform a single comparison. Steps 2.1–2 are repeated at most n–1 times. Max. no. of comparisons = n – 1 Time complexity is again O(n).

Merging (7) Implementation in Java: static void merge ( Comparable[] a1, int l1, int r1, Comparable[] a2, int l2, int r2, Comparable[] a3, int l3) { // Merge a1[l1…r1] and a2[l2…r2] into a3[l3…] // (where both a1 and a2 are sorted). int i = l1, j = l2, k = l3; while (i <= r1 && j <= r2) { int comp = a1[i].compareTo(a2[j]); if (comp <= 0) a3[k++] = a1[i++]; else a3[k++] = a2[j++]; }

Merging (8) Implementation (continued): while (i <= r1) a3[k++] = a1[i++]; while (j <= r2) a3[k++] = a2[j++]; }

Remedial Mathematics Summing arithmetic series An arithmetic series is a sequence of numbers with a fixed difference between consecutive numbers. E.g.: 2, 3, 4, 5 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 Observe: The average of the first and last numbers is the average of the whole arithmetic series. To sum an arithmetic series, multiply the length of the series by the average of the first and last numbers.

Remedial Mathematics Example: summing a series Sum the series 1, 2, …, n–1, n (where n  0). Length of series = n Average of first and last numbers = (n + 1)/2 Sum = n(n + 1)/2 Testing: n = 3 n(n + 1)/2 = 34/2 = 6 1 + 2 + 3 = 6 n = 4 n(n + 1)/2 = 45/2 = 10 1 + 2 + 3 + 4 = 10

Sorting Problem: Given an unsorted array of data, rearrange the data into ascending order. This is important because sorted data can be searched and merged efficiently. Choice of algorithms: selection sort insertion sort merge-sort quick-sort shell-sort, radix sort, etc. (not covered here).

Selection sort (1) Idea: Find the least value in the array, swap it into the leftmost component (where it belongs), and then forget the leftmost component. Do this repeatedly.

Selection sort (2) Selection sort algorithm: To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. Loop invariant: left+1 a l right–1 left l–1 right lesser values (sorted) greater values (unsorted)

Selection sort (3) Animation: To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger a l rat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger 6 a l rat pig 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger a l rat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger a l rat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger 6 a l rat pig 7 8 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion tiger a l rat pig 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 7 a l rat tiger 6 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 8 a l rat tiger 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 8 a l rat tiger 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 7 a l rat tiger 6 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 7 a l rat tiger 6 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 6 a l rat tiger 7 8 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a l goat dog 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a l goat dog 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7 8 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow pig fox rat lion tiger a l goat dog 6 7 8 p To sort a[left…right] into ascending order: 1. For l = left, …, right–1, repeat: 1.1. Set p such that a[p] is the least of a[l…right]. 1.2. If p  l, swap a[p] and a[l]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox rat lion tiger a l goat pig 6 7

Selection sort (4) Analysis (counting comparisons): Let n = right – left + 1 be the length of the array. Step 1.1 performs right–l comparisons. This is repeated with l = left, …, right–2, right–1. No. of comparisons = (right–left) + … + 2 + 1 = (n–1) + … + 2 + 1 = (n – 1)n/2 = (n2 – n)/2 Time complexity is O(n2).

Selection sort (5) Implementation in Java: static void selectionSort ( Comparable[] a, int left, int right) { // Sort a[left…right] into ascending order. for (int l = left; l < right; l++) { int p = l; Comparable least = a[p]; for (int k = l+1; k <= right; k++){ int comp = a[k].compareTo(least); if (comp < 0) { p = k; least = a[p]; } } if (p != l) { a[p] = a[l]; a[l] = least; } } }

Insertion sort (1) Idea: We can sort a file of values by successively reading each value and inserting it into its correct position in an array. Use the same idea to sort an array of values in place.

Insertion sort (2) Insertion sort algorithm: To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. Loop invariant: left+1 a r right–1 left r–1 right inserted (sorted) still to be inserted

Insertion sort (3) Animation: To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox lion pig rat tiger 7 a r goat dog 6 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox lion pig rat tiger 6 a r goat dog 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox lion pig rat tiger 6 a r goat dog 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. 7 r cat left = 0 1 2 3 4 5 8 = right cow fox goat lion pig rat a tiger dog 6 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox goat lion pig rat 8 a r tiger dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a rat tiger 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 9 a r rat tiger 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig 8 a r rat tiger 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox lion pig rat tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right fox pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right fox pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right fox pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right fox pig cat rat lion tiger a r goat dog 6 7 To sort a[left…right] into ascending order: 1. For r = left+1, …, right, repeat: 1.1. Let val = a[r]. 1.2. Insert val into its correct sorted position in a[left…r]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a r goat dog 6 7

Insertion sort (4) Analysis (counting comparisons): Let n = right – left + 1 be the length of the array. Step 1.2 performs between 1 and r – left comparisons, say (r – left + 1)/2 comparisons on average. This is repeated with r = left+1, left+2, …, right. Average no. of comparisons = 2/2 + 3/2 + … + n/2 = (n – 1)(n + 2)/4 = (n2 + n – 2)/4 Time complexity is O(n2).

Merge-sort (1) Idea for sorting an array: Divide the array into two subarrays of about equal length. Sort the subarrays separately. Merge the sorted subarrays. This is an application of the divide-and-conquer strategy. To solve a “hard” problem: Break the problem down into two or more “easier” sub-problems. Solve these sub-problems separately. Combine their answers.

Merge-sort (2) Merge-sort algorithm: To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate.

Merge-sort (3) Animation: To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat dog goat a lion tiger 6 7 m b To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. left = 0 1 2 3 4 5 8 = right a 6 7 cat cow dog fox goat lion pig rat tiger To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat dog goat a lion tiger 6 7 m To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. left = 0 1 2 3 4 5 8 = right a 6 7 m cat cow dog fox goat lion pig b rat tiger To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 m To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. If left < right: 1.1. Let m be an integer about midway between left and right. 1.2. Sort a[left…m] into ascending order. 1.3. Sort a[m+1…right] into ascending order. 1.4. Merge a[left…m] and a[m+1…right] into auxiliary array b. 1.5. Copy all components of b into a[left…right]. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow fox pig rat lion tiger a goat dog 6 7 m

Merge-sort (4) Analysis (counting comparisons): Let n = right–left+1 be the length of the array. Let the total no. of comparisons required to sort n values be comps(n). The left subarray’s length is about n/2, so step 1.2 takes about comps(n/2) comparisons to sort it. Similarly, step 1.3 takes about comps(n/2) comparisons to sort the right subarray. Step 1.4 takes about n–1 comparisons to merge the subarrays.

Merge-sort (5) Analysis (continued): Therefore: comps(n)  2 comps(n/2) + n – 1 if n > 1 comps(n) = 0 if n  1 Solution: comps(n)  n log2n Time complexity is O(n log n). Space complexity is O(n), since step 1.4 needs an auxiliary array of length n.

Quick-sort (1) Idea: Choose any value from the array (called the pivot). Then partition the array into three subarrays such that: the left subarray contains only values less than (or equal to) the pivot; the middle subarray contains only the pivot; the right subarray contains only values greater than (or equal to) the pivot. Finally sort the left subarray and the right subarray separately. This is another application of the divide-and-conquer strategy.

Quick-sort (2) Quick-sort algorithm: To sort a[left…right] into ascending order: 1. If left < right: 1.1. Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate.

Quick-sort (3) Invariants: After step 1.1: a left p–1 right less than or equal to pivot (unsorted) pivot greater than or equal to pivot (unsorted) After step 1.3: p+1 a p left p–1 right less than or equal to pivot (sorted) pivot greater than or equal to pivot (sorted)

Quick-sort (4) Animation: To sort a[left…right] into ascending order: 1. If left < right: 1.1. Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a rat tiger 6 7 p To sort a[left…right] into ascending order: 1. If left < right: 1.1. Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox goat lion pig a rat tiger 6 7 To sort a[left…right] into ascending order: 1. If left < right: 1.1. Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate. cat left = 0 1 2 3 4 5 8 = right cow dog fox pig rat lion a tiger goat 6 7 p To sort a[left…right] into ascending order: 1. If left < right: 1.1. Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. If left < right: 1.1. Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate. fox left = 0 1 2 3 4 5 8 = right cow pig cat rat lion tiger a goat dog 6 7 To sort a[left…right] into ascending order: 1. If left < right: 1.1. Partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]. 1.2. Sort a[left…p–1] into ascending order. 1.3. Sort a[p+1…right] into ascending order. 2. Terminate. cow left = 0 1 2 3 4 5 8 = right cat dog fox pig rat lion a tiger goat 6 7 p

Quick-sort (5) Analysis (counting comparisons): Let n be the no. of values to be sorted. Let the total no. of comparisons required to sort n values be comps(n). Step 1.1 takes about n–1 comparisons to partition the array. (NB: partition, not sort!)

Quick-sort (6) In the best case, the pivot turns out to be the median value in the array. So the left and right subarrays both have length about n/2. Then steps 1.2 and 1.3 take about comps(n/2) comparisons each. Therefore: comps(n)  2 comps(n/2) + n – 1 if n > 1 comps(n) = 0 if n  1 Solution: comps(n)  n log2n Best-case time complexity is O(n log n).

Quick-sort (7) In the worst case, the pivot turns out to be the smallest value. So the right subarray has length n–1 whilst the left subarray has length 0. Then step 1.3 performs comps(n–1) comparisons, but step 1.2 does nothing at all. Therefore: comps(n)  comps(n–1) + n – 1 if n > 1 comps(n) = 0 if n  1 Solution: comps(n)  (n2 – n)/2 Worst-case time complexity is O(n2). The worst case arises if the array is already sorted!

Quick-sort (8) Implementation in Java: static void quickSort ( Comparable[] a, int left, int right) { // Sort a[left…right] into ascending order. if (left < right) { int p = partition(a, left, right); quickSort(a, left, p-1); quickSort(a, p+1, right); } }

Quick-sort (9) Partitioning algorithm: To partition a[left…right] such that a[left…p–1] are all less than or equal to a[p], and a[p+1…right] are all greater than or equal to a[p]: 1. Let pivot be the value of a[left], and set p = left. 2. For r = left+1, …, right, repeat: 2.1. If a[r] is less than pivot: 2.1.1. Copy a[r] into a[p], a[p+1] into a[r], and pivot into a[p+1]. 2.1.2. Increment p. 3. Terminate with answer p. Note that other (and better) partitioning algorithms exist.

Quick-sort (10) Loop invariant: a left p–1 r–1 r right less than or equal to pivot (unsorted) pivot greater than or equal to pivot (unsorted) still to be partitioned

Quick-sort (11) Implementation in Java: static int partition ( Comparable[] a, int left, int right) { // Partition a[left…right] such that // a[left…p-1] are all less than or equal to a[p], and // a[p+1…right] are all greater than or equal to a[p]. // Return p. Comparable pivot = a[left]; int p = left;

Quick-sort (12) Implementation (continued): for (int r = left+1; r <= right; r++) { int comp = a[r].compareTo(pivot); if (comp < 0) { a[p] = a[r]; a[r] = a[p+1]; a[p+1] = pivot; p++; } } return p; }

Comparison of sorting algorithms No. of comparisons No. of copies Time complexity Space complexity Selection sort ~ n2/2 ~ 2n O(n2) O(1) Insertion sort ~ n2/4 Merge-sort ~ n log2n ~ 2n log2n O(n log n) O(n) Quick-sort ~ n log2n ~ n2/2 ~ 2n/3 log2n 0 O(n log n) O(n2) O(log n) O(n) best worst