CS100J Lecture 14 Previous Lecture

Slides:



Advertisements
Similar presentations
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
Advertisements

Discussion #33 Adjacency Matrices. Topics Adjacency matrix for a directed graph Reachability Algorithmic Complexity and Correctness –Big Oh –Proofs of.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
Simple Sorting Algorithms
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
1 Lecture 21 Introduction to Sorting I Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.  Brief.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
Comparison-Based Sorting & Analysis Smt Genap
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CS 100Lecture 261 CS100J Lecture 26 n Previous Lecture –Application of inheritance –“Higher-order" methods –Abstract classes –Visibility modifier: protected.
Discrete Maths: Invariant/2 1 Discrete Maths Objectives – –to show the use of induction for proving properties of code involving loops use induction.
Central Algorithmic Techniques Iterative Algorithms.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Arrays Chapter 7.
Sorting Chapter 14.
Chapter 9: Sorting and Searching Arrays
CS212: Data Structures and Algorithms
Sorting Mr. Jacobs.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Reasoning with invariants
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Simple Sorting Algorithms
CS 213: Data Structures and Algorithms
Data Structures and Algorithms
CS 3343: Analysis of Algorithms
10.3 Bubble Sort Chapter 10 - Sorting.
Searching and Sorting Linear Search Binary Search ; Reading p
Analysis of Algorithms CS 477/677
Algorithm design and Analysis
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Selection sort Given an array of length n,
Divide and Conquer (Merge Sort)
Lecture 11 Searching and Sorting Richard Gesick.
Sorting … and Insertion Sort.
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Parameter Passing in Java
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Sorting Chapter 8.
Simple Sorting Algorithms
Binary Search and Loop invariants
CIS265/506 Simple Sorting CIS265/506: Chapter 03 - Sorting.
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100J Lecture 15 Previous Lecture This Lecture Sorting
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
CS100A Lecture 15, 17 22, 29 October 1998 Sorting
Simple Sorting Algorithms
Searching and Sorting Hint at Asymptotic Complexity
Workshop for CS-AP Teachers
Sorting and Searching -- Introduction
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Linear and Binary Search
Simple Sorting Algorithms
CS100A Sections Dec Loop Invariant Review C Review and Example
Module 8 – Searching & Sorting Algorithms
Applications of Arrays
Presentation transcript:

CS100J Lecture 14 Previous Lecture Additional material on the use of arrays and alternative implementations of a class The use of auxiliary private methods searching This Lecture Sorting Loop invariants more Rules of Thumb Reading Lewis & Loftus Section 6.2 Savitch Section 6.4 CS 100 Lecture 14

Sorting Problem. Sort a given array that contains m+1 integers into non-decreasing order, i.e., rearrange the values in the array so they never decrease. Rule of Thumb. Write a precise specification /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . } Implementation Constraint: Sort array in place (in situ), i.e., do not use a second array. CS 100 Lecture 14

Inspiration from Example Rule of Thumb. Find inspiration from experience, e.g., sorting a hand in a card game. Rule of Thumb. Work sample data by hand. Be introspective. Ask yourself: “What am I doing?” 0 1 2 3 4 5 = m A CS 100 Lecture 14

Algorithm: Selection Sort 0 1 2 3 4 5 A 19 13 20 10 12 18 0 1 2 3 4 5 A 10 13 20 19 12 18 0 1 2 3 4 5 A 10 12 20 19 13 18 0 1 2 3 4 5 A 10 12 13 19 20 18 0 1 2 3 4 5 A 10 12 13 18 20 19 0 1 2 3 4 5 A 10 12 13 18 19 20 What am I doing? Swap smallest of A[0..m] with A[0]. Swap smallest of A[1..m] with A[1]. Swap smallest of A[2..m] with A[2]. . . . Swap smallest of A[m-1..m] with A[m-1]. CS 100 Lecture 14

Iterative Refinement Rule of Thumb. If you smell an iteration, write it down. Decide between definite iteration and indefinite iteration Write down an appropriate pattern for the iteration. Do not fill in the pattern yet. Example: /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( ________; __________; _________) } CS 100 Lecture 14

Loop Invariant Rules of Thumb 0 k m A ? ? where Characterize the state after an arbitrary number of iterations, either in English of in a diagram. Introduce a variable to record the subscript of each boundary expected to change independently during the iteration. 0 k m A finished ? ? where finished means that the array elements contain the correct final values, ? means that the array elements may or may not contain the correct final values, k contains the subscript of the first element in the ? section, and A[0..m] is a rearrangement of the original values A[0..m] CS 100 Lecture 14

Loop Invariant, cont. Rules of Thumb, continued 0=k m A 0 k m A ? Characterize the initial and final states, i.e., the state before the iteration begins and after the iteration is expected to stop. The characterization of the initial and final states should be special cases of the general characterization. Initial: Intermediate: Final: 0=k m A ? 0 k m A finished ? 0 m=k A finished CS 100 Lecture 14

Loop Invariant, cont. Rule of Thumb. Use the characterization to specify what the loop body must accomplish. Use the characterization to refine the initialization, termination condition, and increment of the loop. /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( int k = ______; k < _____; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ } CS 100 Lecture 14

Refine the Loop Body /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ /* Exchange A[k] and A[minLoc] */ } CS 100 Lecture 14

Refine One Subpart /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ /* Exchange A[k] and A[minLoc] */ } CS 100 Lecture 14

Refine Other Subpart /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { . . . for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ for ( _______; _______; ______) } /* Exchange A[k] and A[minLoc] */ CS 100 Lecture 14

Loop Invariant (for Inner Loop) Initial: Intermediate: Final: where A[minLoc] is smallest in A[k..i-1] All values in > are greater than A[minLoc] No value in >= is smaller than A[minLoc] All values in ? are unknown. minLoc 0 k i m A ? minLoc 0 k i m A ? > >= minLoc 0 k m i A >= > CS 100 Lecture 14

Final Function /* Sort A[0..m] into non-decreasing order. */ public void sort(int[] A, int m) { for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ minLoc = k; for (int i=k+1; i <= m; i++) if (A[i] < A[minLoc]) minLoc = i; /* Exchange A[k] and A[minLoc] */ int temp = A[k]; A[k] = A[minLoc]; A[minLoc] = temp; } CS 100 Lecture 14

Better Final Function /* Sort A into non-decreasing order. */ public void sort(int[] A) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ minLoc = k; for (int i=k+1; i <= m; i++) if (A[i] < A[minLoc]) minLoc = i; /* Exchange A[k] and A[minLoc] */ int temp = A[k]; A[k] = A[minLoc]; A[minLoc] = temp; } CS 100 Lecture 14

Rearrange Related Data at Same Time /* Given arrays A and B of equal length, sort A into non-decreasing order and rearrange B similarly. */ public void sort(int[] A, int[] B) { int m = A.length - 1; for ( int k = 0; k < m; k++ ) /* Given that A[0..k-1] is finished, finish A[0..k]; rearrange B similarly. */ int minLoc; // subscript of // smallest in A[k..m] /* Set minLoc so A[minLoc] is smallest in A[k..m]. */ minLoc = k; for (int i=k+1; i <= m; i++) if (A[i] < A[minLoc]) minLoc = i; /* Exchange A[k] and A[minLoc] */ int temp = A[k]; A[k] = A[minLoc]; A[minLoc] = temp; } /* Exchange B[k] and B[minLoc] */ int temp = B[k]; B[k] = B[minLoc]; B[minLoc] = temp; CS 100 Lecture 14