Chapter 12 Sorting and searching. This chapter discusses n Two fundamental list operations. u Sorting u Searching n Sorted lists. n Selection/bubble sort.

Slides:



Advertisements
Similar presentations
CHAPTER 12. SEARCHING, SORTING AND THE MANIPULATION OF ELEMENTS IN AN ARRAY.
Advertisements

Binary Search lo Binary search. Given a key and sorted array a[], find index i such that a[i] = key,
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 14: Sorting and Searching.
Chapter 3 Brute Force Brute force is a straightforward approach to solving a problem, usually directly based on the problem’s statement and definitions.
CMSC 2021 Searching and Sorting. CMSC 2022 Review of Searching Linear (sequential) search Linear search on an ordered list Binary search.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
©Brooks/Cole, 2001 Chapter 13 Binary Files. ©Brooks/Cole, 2001 Figure 13-1.
Chapter 5 Trees PROPERTIES OF TREES 3 4.
CHAPTER 11 Sorting.
Midterm Exam Two Tuesday, November 25 st In class cumulative.
Understanding BubbleSort CS-502 (EMC) Fall Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern.
Searching and Sorting Arrays
Selection Sort
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter Complexity of Algorithms –Time Complexity –Understanding the complexity of Algorithms 1.
Building Java Programs Chapter 13 Searching reading: 13.3.
Chapter 16: Searching, Sorting, and the vector Type.
Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process.
Part 2.  Predefined  User defined  //exclusive to c++  / //c specific.
 DEFINE COMPUTER ? EXPLAIN CLASSIFICATION OF COMPUTER.  WHAT ARE INPUT AND OUTPUT DEVICES OF COMPUTER ? EXPALIN OUTPUT DEVICES.  WHAT ARE MEMORY AND.
Summary Algorithms Flow charts Bubble sort Quick sort Binary search Bin Packing.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
A PowerPoint about Algorithm’s. What is an algorithm? A step by step process of instruction.
Selection Sort
Ordered Lists and Sorted Lists Chapter 7. Ordered Lists Array version vs. Linked-list version.
Lecture 6 Analysis of Iterative Algorithms Sorting Algorithms.
Chapter 5 Algorithms (1) Introduction to CS 1 st Semester, 2012 Sanghyun Park.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
CSC 142 Q 1 CSC 142 Sorting [Reading: chapter 11].
Sorting and Searching Bubble Sort Linear Search Binary Search.
Chapter 16: Searching, Sorting, and the vector Type.
بسم الله الرحمن الرحيم شرح جميع طرق الترتيب باللغة العربية
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Chapter 7: Array.
Chapter 8 Arrays Objectives
Topics discussed in this section:
Sorting Data are arranged according to their values.
Chapter 19 Binary Search Trees.
Analysis of Bubble Sort and Loop Invariant
Mean: average 2. Median: middle number 3. Mode: most often
Selection sort Given an array of length n,
كلية المجتمع الخرج البرمجة - المستوى الثاني
Chapter 2: Digital Image Fundamentals
Chapter 8 Arrays Objectives
Chapter 2: Digital Image Fundamentals
Sorting Data are arranged according to their values.
برنامج التميز في خدمة عملاء السادة موظفي مكاتب المساعدة القانونية
Standard Version of Starting Out with C++, 4th Edition
Computer Science — An Overview J. Glenn Brookshear
Sorting.
Binary Search Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant.
C Arrays (2) (Chapter 6) ECET 264.
Algorithmic Complexity
Principles of Computing – UFCFA3-30-1
Given value and sorted array, find index.
Directions: On each slide you will find fish of different colors, sizes, and numbers. You will also find bubbles for grouping the fish. On some slides.
Searching and Sorting Arrays
Chapter 5 Algorithm Analysis.
Chapter 8 Arrays Objectives
Intro to Programming 2/23/2016 Review Sections 1-3
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Binary Search Counting
Selection Sort Fonte: Fondamenti di Informatica - A.Accattatis Selection Sort Fonte:
Principles of Computing – UFCFA3-30-1
Program: Mini Phone Book 2/11/2016
RANDOM NUMBERS SET # 1:
Presentation transcript:

Chapter 12 Sorting and searching

This chapter discusses n Two fundamental list operations. u Sorting u Searching n Sorted lists. n Selection/bubble sort. n Binary search. n Loop invariant.

Ordering lists n There must be a way of comparing objects to determine which should come first. There must be an ordering relation. n There must be an operator to compare the objects to put them in order. n The ordering is antisymmetric. (a b !<a) n The ordering is transitive. (a a<c) n The ordering is total with respect to some equivalence on the class. (a b or a==b, but only one; here == means equivalent with respect to the ordering)

Implementing comparison public boolean lessThan (Student s) s1.lessThan(s2) Returns true if s1 =s2. for i > 0, j < size(), get(i).lessThan(get(j)) implies i < j.

Selection Sort n Find the smallest element in the list, and put it in first. n Find the second smallest and put it second, etc.

Selection Sort (cont.) n Find the smallest. n Interchange it with the first. n Find the next smallest. n Interchange it with the second.

Selection Sort (cont.) n Find the next smallest. n Interchange it with the third. n Find the next smallest. n Interchange it with the fourth.

Selection Sort (cont.) n To interchange items, we must store one of the variables temporarily.

Analysis of selection sort n If there are n elements in the list, the outer loop is performed n-1 times. The inner loop is performed n-first times. i.e. time= 1, n-1 times; time=2, n-2 times; … time=n-2, 1 times. n (n-1)x(n-first) = (n-1)+(n-2)+…+2+1 = (n 2 -n)/2 n As n increases, the time to sort the list goes up by this factor (order n 2 ).

Bubble sort n Make a pass through the list comparing pairs of adjacent elements. n If the pair is not properly ordered, interchange them. n At the end of the first pass, the last element will be in its proper place. n Continue making passes through the list until all the elements are in place.

Pass 1

Pass 1 (cont.)

Pass 2

Pass 3 &4

Analysis of bubble sort n This algorithm represents essentially the same number of steps as the selection sort. n If make a pass through the list without interchanging, then the list is ordered. This makes the algorithm fast if it is mostly ordered.

Binary search n Assumes an ordered list. n Look for an item in a list by first looking at the middle element of the list. n Eliminate half the list. n Repeat the process.

Binary search

private int itemIndex (Student item, StudentList list) The proper place for the specified item on the specified list, found using binary search. require: list is sorted in increasing order ensure: 0 <= result <= list.size() for 0 <= i < result list.get(i) < item for result <= i < list.size() list.get(i) >= item

Binary search (cont.) private int itemIndex (Student item, StudentList list) { int low; //lowest index considered int high; //highest index considered int mid; //middle between high and low low =0 high = list.size() -1; while (low <= high) { mid = (low+high)/2; if (list.get(mid).lessThan(item)) low = mid+1; else high = mid-1; } return low; }

Binary search (cont.)

Completing the search /** * Uses binary search to find where and if an element * is in a list. * require: * item != null * ensure: *if item == no element of list * indexOf(item, list) == -1 *else * item == list.get(indexOf(item, list)), * and indexOf(item, list) is the smallest * value for which this is true */ public int indexOf(Student item, StudentList list){ int i = itemIndex(item, list); if (i<list.size() && list.get(i).equals(item)) return i; else return -1; }

Sequential/linear search public int indexOf (Student obj) { int i; int length; length = this.size(); i = 0; while (i < length && !obj.equals(get(i))) i = i+1; if ( i < length) return i; else return -1;// item not found }

Relative efficiency

Loop invariant n A loop invariant is a condition that remains true as we repeatedly execute the loop body, and captures the fundamental intent in iteration. n partial correctness: the assertion that a loop is correct if it terminates. n total correctness: the assertion that a loop is both partially correct, and terminates. n loop invariant: a condition that is true at the start of execution of a loop and remains true no matter how many times the body of the loop is performed.

Back to binary search 1.private int itemIndex (Student item, StudentList list) { 2.low =0 3.high = list.size() -1; 4.while (low <= high) { 5.mid = (low+high)/2; 6. if (list.get(mid).lessThan(item)) 7. low = mid+1; 8. else 9. high = mid-1; 10.} 11.return low; 12.} n At line 6, we can conclude 0 <= low <= mid <= high < list.size()

The key invariant for 0 <= i < low list.get(i) < item for high < i < list.size() list.get(i) >= item n This holds true at all four key places (a, b, c, d). u It is vacuously true for indexes less than low or greater than high (a) u We assume it holds after merely testing the condition (b) and (d) u If the condition holds before executing the if statement and the list is sorted in ascending order, it will remain true after executing the if statement (condition c).

The key invariant n We are guaranteed that for 0 <= i < mid list.get(i) < item After the assignment, low equals mid+1 and so for 0 <= i < low list.get(i) < item n This is true before the loop body is done: for high < i < list.size( list.get(i) >= item

Partial correctness: If the loop body is not executed at all, and point (d) is reached with low == 0 and high == -1. If the loop body is performed, at line 6, low <= mid <= high. low <= high becomes false only if mid == high and low is set to mid + 1 or low == mid and high is set to mid - 1 In each case, low == high + 1 when the loop is exited.

Partial correctness: n The following conditions are satisfied on loop exit low == high + 1 for 0 <= i <= low list.get(i) < item for high < i < list.size() list.get(i) >= item n which implies for 0 <= i < low list.get(i) < item for low <= i < list.size() list.get(i) >= item

Loop termination When the loop is executed, mid will be set to a value between high and low. The if statement will either cause low to increase or high to decrease. This can happen only a finite number of times before low becomes larger than high.

Weve covered n Sorting u selection sort u bubble sort n Searching u Sequential/linear search u binary search n Verifying correctness of iterations u partial correctness u loop invariant u key invariant u termination

Glossary

Glossary (cont.)