© 2004 Pearson Addison-Wesley. All rights reserved October 19, 2007 Sorting ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Intro to CIT 594
Sorting. Sorting Considerations We consider sorting a list of records, either into ascending or descending order, based upon the value of some field of.
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Simple Sorting Algorithms
Midterm Exam Two Tuesday, November 25 st In class cumulative.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L18 (Chapter 23) Algorithm.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
Summer 2009 Math 1431 and Math 1432 begin. What to do… Watch the orientation video from the spring online classes. Please note that the spring classes.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Seattle Preparatory School Advanced Placement Computer Science Seattle Preparatory School Advanced Placement Computer Science LESSON 62 FEBRUARY 12, 2015.
Upcoming schedule F 11/12 hw#4 due F 11/19 hw#5 due F 11/25 midterm #2.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
COP 3530: Data Structures, Algorithms, & Applications Instructor: Kristian Linn Damkjer.
Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process.
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.
Introduction to Algorithms Jiafen Liu Sept
Searching and Sorting Topics Sequential Search on an Unordered File
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
© 2004 Pearson Addison-Wesley. All rights reserved October 17, 2007 Searching (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
CS1101: Programming Methodology
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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.
3 – SIMPLE SORTING ALGORITHMS
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Sorting.
Final Exam Tuesday, December 22nd 2:00 - 3:50pm room 102 Warren Weaver Hall.
© 2004 Pearson Addison-Wesley. All rights reserved March 10, 2006 Sorting ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Course Info Instructor U.T. Nguyen Office: CSEB Office hours: Tuesday, 14:30-15:30 Thursday, 12:00-12:45 By.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 26 Sorting.
CMSC201 Computer Science I for Majors Lecture 25 – Final Exam Review Prof. Katherine Gibson Prof. Jeremy Dixon.
Sorting Lower Bound 4/25/2018 8:49 PM
CprE 185: Intro to Problem Solving (using C)
CMSC201 Computer Science I for Majors Lecture 27 – Final Exam Review
Searching and Sorting Topics Sequential Search on an Unordered File
Sorting October 20, 2006 ComS 207: Programming I (in Java)
Searching and Sorting Topics Sequential Search on an Unordered File
CprE 185: Intro to Problem Solving (using C)
UMBC CMSC 104 – Section 01, Fall 2016
Searching and Sorting Topics Sequential Search on an Unordered File
CprE 185: Intro to Problem Solving (using C)
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Sorting: part 1 Barak Obama on sorting, bubble sort, insertion sort,
Chapter 19 Searching, Sorting and Big O
Presentation transcript:

© 2004 Pearson Addison-Wesley. All rights reserved October 19, 2007 Sorting ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor: Alexander Stoytchev

© 2004 Pearson Addison-Wesley. All rights reserved Midterm 2 Next Tuesday (Oct 23, 2007) Location: Curtiss Hall, room 127 (classroom) Time: 6:30pm – 7:45pm Try to be there at least 10 minutes early. If you need extra time you can have it but this is **NOT** a 3 hour exam!

© 2004 Pearson Addison-Wesley. All rights reserved Midterm Format Covers Sections 1-5 & 7 Also, Searching and Sorting Format same as Midterm 1

© 2004 Pearson Addison-Wesley. All rights reserved Midterm Format (last semester) True/False(10 x 1p each = 10p) Short answer( 5 x 2p each = 10p) Code Snippets( 5 x 3p each = 15p) Other Stuff( 3 x 5p each = 15p) Program 1(15p) Program 2(15p) Program 3(15p) Program 4(20p) TOTAL (130p)

© 2004 Pearson Addison-Wesley. All rights reserved Midterm Format You don’t need to get all 130 points to get an A 100 is a 100 You must get at least 65 points in order to pass this exam

© 2004 Pearson Addison-Wesley. All rights reserved Midterm Format Drop Deadline is next Friday (Oct 26) I cannot guarantee that all exams will be graded by then. If you believe that you did not do well please ask me to grade your exam first at the time when you are submitting it.

© 2004 Pearson Addison-Wesley. All rights reserved Quick review of last lecture

© 2004 Pearson Addison-Wesley. All rights reserved Binary Search At each step it splits the remaining array elements into two groups Therefore, it is faster than the linear search Works only on an already SORTED array Thus, there is a performance penalty for sorting the array [

© 2004 Pearson Addison-Wesley. All rights reserved [ Example: Successful Binary Search

© 2004 Pearson Addison-Wesley. All rights reserved Example: BinarySearch.java

© 2004 Pearson Addison-Wesley. All rights reserved

Analysis of Searching Methods For an array of size n Sequential Search (Average-Case) n/2 Sequential Search (Worst-Case) n Binary Search (Average-Case) log(n)/2 Binary Search (Worst-Case) log(n)

Sorting Not in the Textbook

© 2004 Pearson Addison-Wesley. All rights reserved Insertion Sort [ 0

© 2004 Pearson Addison-Wesley. All rights reserved [ Example: Insertion Sort

© 2004 Pearson Addison-Wesley. All rights reserved Animations for Insertion Sort [

© 2004 Pearson Addison-Wesley. All rights reserved Animations of Sorting Algoritms

© 2004 Pearson Addison-Wesley. All rights reserved Swapping Array Elements [

© 2004 Pearson Addison-Wesley. All rights reserved Java code // Swap a[i] with the smallest element int temp = a[i]; a[i] = a[minIndex]; a[minIndex] = temp;

© 2004 Pearson Addison-Wesley. All rights reserved Example: InsertionSort.java

© 2004 Pearson Addison-Wesley. All rights reserved Selection Sort [

© 2004 Pearson Addison-Wesley. All rights reserved Example: Selection Sort [

© 2004 Pearson Addison-Wesley. All rights reserved Example: SelectionSort.java

© 2004 Pearson Addison-Wesley. All rights reserved Bubble Sort [ 0

© 2004 Pearson Addison-Wesley. All rights reserved [ Example: Bubble Sort

© 2004 Pearson Addison-Wesley. All rights reserved Example: BubbleSort.java

© 2004 Pearson Addison-Wesley. All rights reserved Analysis: all three run in O(n 2 ) time [

© 2004 Pearson Addison-Wesley. All rights reserved Analysis There are faster sorting algorithms  Heap sort  Quick sort  Merge Sort We will not cover those but feel free to study them on your own. They run in O(n log n) time.

© 2004 Pearson Addison-Wesley. All rights reserved O(n log n) sorting algorithms [

© 2004 Pearson Addison-Wesley. All rights reserved THE END