CS 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Analysis of Algorithms CS 477/677
Searching and Sorting Arrays
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
CSE 373 Data Structures and Algorithms
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
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
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
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.
1 Algorithms CSCI 235, Fall 2015 Lecture 11 Elementary Sorts.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
Searching Arrays Linear search Binary search small arrays
Sort Algorithm.
Searching and Sorting Arrays
CSCE 210 Data Structures and Algorithms
Chapter 9: Sorting and Searching Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS212: Data Structures and Algorithms
Sorting Mr. Jacobs.
Priority Queues A priority queue is an ADT where:
Searching.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
CS1010 Discussion Group 11 Week 8 – Searching and Sorting.
Recitation 13 Searching and Sorting.
Algorithms CSCI 235, Fall 2017 Lecture 13 Elementary Sorts II
Sorting Algorithms.
Data Structures 2018 Quiz Answers
10.3 Bubble Sort Chapter 10 - Sorting.
Analysis of Algorithms CS 477/677
Linear and Binary Search
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Sorting … and Insertion Sort.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Search,Sort,Recursion.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Sorting Chapter 8.
Sorting Example Bubble Sort
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Search,Sort,Recursion.
CS 2430 Object Oriented Programming and Data Structures I
Simple Sorting Algorithms
Searching.
Searching and Sorting Arrays
Analysis of Algorithms
Algorithms Sorting.
Workshop for CS-AP Teachers
Simple Sorting Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 13 Elementary Sorts II
Insertion Sort Array index Value Insertion sort.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Stacks, Queues, ListNodes
10.3 Bubble Sort Chapter 10 - Sorting.
Sorting Algorithms.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Presentation transcript:

CS 2430 Object Oriented Programming and Data Structures I Day1 on Thursday, before lab1

Quiz 4

Where is the LAST element? front rear rear - 1 front rear rear - 1 rear front rear – 1?

Quiz 4 public Object getLastElement() { if (count == 0) return null; int last; if (rear == 0) last = items.length - 1; else last = rear – 1; rear = last; count --; return items[last]; }

Quiz 4 public Object getLastElement() { if (count == 0) return null; rear –-; if (rear == -1) rear = items.length - 1; count --; return items[rear]; }

Without IF public Object getLastElement() { if (count == 0) return null; rear = (rear – 1) % items.length; // What do we get from (0 – 1) % items.length? // -1! count --; return items[rear]; }

Without IF public Object getLastElement() { if (count == 0) return null; rear = (rear – 1 + items.length) % items.length; count --; return items[rear]; }

public Object removeAndReturnLastElement(Queue theQ, int queueSize) { Queue tq = new Queue(queueSize); if (theQ.isEmpty()) return null; Object obj = theQ.remove(); // is obj the last? while (! theQ.isEmpty()) // No tq.add (obj); obj = theQ.remove(); } while (! tq.isEmpty()) theQ.add(tq.remove); return obj;

Exception throw new Exception(“F1: N”); // Not Output System.out.println(“In f1”); // Output

Output Caught in f2 Main bottom X: 3 Y: 2 In f2 In f1 (false) Caught in Main X: 4 Y: 3 (true) In f1 (false) Caught in Main X: 2 Y: 2 In f2

Generic Interface Comparable public interface Comparable <E> { public int compareTo ( E x ); } Return value: negative if (this) less than x 0 if (this) equal to x positive if (this) greater than x

Class Date public class Date implements Comparable<Date> { /** @param x an object of class Date */ @Override public int compareTo ( Date x ) . . . } A total ordering of all Date objects We can compare any two objects of Date and receive one of three possible results Should be consistent with equals, although not required

Sorting Algorithms Selection Sort Bubble sort Insertion Sort … Assume ascending (non-descending) order Date[] dates = new Date[MAX_SIZE]; int size; int[] a = new int[MAX_SIZE];

Selection Sort for i = 0 to (size - 2) Make sure a[i] is in sorted position Don’t go to (size – 1)! find index of the smallest element between a[i] and a[size - 1] swap a[i] and a[index] Must go to a[size – 1] to find the index! 12 7 34 15 5 22 14 21

Selection Sort for i = 0 to (size - 2) minIndex = i for j = i + 1 to (size – 1) if a[j] < a[minIndex] minIndex = j swap a[i], a[minIndex] Loop Invariant: After the ith iteration of the outer for loop, the first i elements (a[0] to a[i-1]) are in the correct locations.

Selection Sort 12 7 34 15 5 22 14 21 17 minIndex: 4 5 7 34 15 12 22 14 21 17 minIndex: 1 5 7 34 15 12 22 14 21 17 minIndex: 4 5 7 12 15 34 22 14 21 17 minIndex: 6 5 7 12 14 34 22 15 21 17 minIndex: 6 5 7 12 14 15 22 34 21 17 minIndex: 8 5 7 12 14 15 17 34 21 22 minIndex: 7 5 7 12 14 15 17 21 34 22 minIndex: 8 5 7 12 14 15 17 21 22 34 Don’t do it!

Bubble Sort for i = 0 to (size - 2) // Make sure a[i] is in sorted position for j = (size - 1) downto (i + 1) if a[j] < a[j - 1] then Swap a[j], a[j - 1]

First Pass: i = 0 12 7 34 15 5 22 14 21 17 12 7 34 15 5 22 14 17 21 12 7 34 15 5 22 14 17 21 12 7 34 15 5 14 22 17 21 12 7 34 15 5 14 22 17 21 12 7 34 5 15 14 22 17 21 12 7 5 34 15 14 22 17 21 12 5 7 34 15 14 22 17 21 5 12 7 34 15 14 22 17 21

First Pass of Selection Sort 12 7 34 15 5 22 14 21 17 minIndex: 4 Swap items[0] and items[4] 5 7 34 15 12 22 14 21 17

Bubble Sorting 12 7 34 15 5 22 14 21 17 i = 0 5 12 7 34 15 14 22 17 21 i = 1 5 7 12 14 34 15 17 22 21 i = 2 5 7 12 14 15 34 17 21 22 i = 3 5 7 12 14 15 17 34 21 22 i = 4 5 7 12 14 15 17 21 34 22 i = 5 5 7 12 14 15 17 21 22 34 i = 6 5 7 12 14 15 17 21 22 34 i = 7 5 7 12 14 15 17 21 22 34

Bubble Sort for i = 0 to (size - 2) for j = (size - 1) downto (i + 1) if a[j] < a[j - 1] then Swap a[j], a[j - 1] Loop Invariant: After ith iteration of the outer for loop, the first i elements (a[0] to a[i-1]) are in the correct locations.

Insertion Sort Inserting elements into array one at a time Keep the array sorted after each insertion First element: sorted Other elements: insert at the right position and move some elements Insert 12 12 Insert 7 7 12 Insert 34 7 12 34 Insert 15 7 12 15 34 Insert 5 5 7 12 15 34

Insertion Sort 12 7 34 15 5 22 14 21 17 Insert 7 and [0] to a[1] are sorted 7 12 34 15 5 22 14 21 17 Insert 34 and a[0] to a[2] are sorted 7 12 34 15 5 22 14 21 17 Insert 15 and a[0] to a[3] are sorted 7 12 15 34 5 22 14 21 17 Insert 5 and a[0] to a[4] are sorted 5 7 12 15 34 22 14 21 17 Insert 22 and a[0] to a[5] are sorted 5 7 12 15 22 34 14 21 17 Insert 14 and a[0] to a[6] are sorted 5 7 12 14 15 22 34 21 17 Insert 21 and a[0] to a[7] are sorted 5 7 12 14 15 21 22 34 17 a[0] to a[8] are sorted 5 7 12 14 15 17 21 22 34

Insertion Sort for i = 1 to (size - 1) // the inserted element j = i // is at index i while j > 0 and a[j] < a[j-1] Swap a[j], a[j - 1] j-- 7 12 15 34 5 7 12 15 5 34 7 12 5 15 34 7 5 12 15 34 5 7 12 15 34

Insertion Sort for i = 1 to (size - 1) j = i while j > 0 and a[j] < a[j-1] Swap a[j], a[j - 1] j-- Loop Invariant: After ith iteration of the outer for loop, top (i+1) elements (a[0] to a[i]) are sorted. (may not be in the final sorted locations)

Prog 4 Due Friday, November 9

Turn in on Friday in Class Bonus Points? Exercise Turn in on Friday in Class Bonus Points?