1 Arrays 2: Sorting and Searching. 2 0. Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment.

Slides:



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

One Dimensional Arrays
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Advanced Array Manipulation
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
CPS120: Introduction to Computer Science Searching and Sorting.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
CSE 373: Data Structures and Algorithms
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
Analysis of Algorithms CS 477/677
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
CSC 211 Data Structures Lecture 15
Sorting HKOI Training Team (Advanced)
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 19: Searching and Sorting Algorithms
Computer Science Searching & Sorting.
Searching and Sorting Chapter Sorting Arrays.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CSCI 51 Introduction to Programming March 12, 2009.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
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 and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
ALGORITHMS.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or.
Data Structures Arrays and Lists Part 2 More List Operations.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
CSCI 51 Introduction to Programming March 10, 2009.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Chapter 9: Sorting and Searching Arrays
Introduction to Search Algorithms
10.3 Bubble Sort Chapter 10 - Sorting.
Algorithm design and Analysis
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Describing algorithms in pseudo code
Search,Sort,Recursion.
Data Structures (CS212D) Week # 2: Arrays.
Sorting Chapter 8.
Search,Sort,Recursion.
Presentation transcript:

1 Arrays 2: Sorting and Searching

2 0. Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment 6, due 11/20, based on material covered today.

3 1. Review of array basics. §What is an array? §A contiguous series of homogeneous elements. §What does contiguous mean? § Pros and cons? §What does homogeneous mean? § Pros and cons?

4 The bounds of an array. §C# defaults to 0-based arrays. If we code float [] Amounts = new float [100]; §we get 100 consecutive floats from Amounts[0] to Amounts[99]. It is important to remember this so we avoid a “bounds error”. §Suppose we code Amounts[I] = 83000; §What if I = -2 or 100?

5 Review of the list concept. §The list is the occupied portion of the array. §The array is the physical container. It may be empty, full, or partially full. §E.g. Amounts, which can contain 100 elements, may only be occupied from Amounts[0] to Amounts[56]. §The list consists of only the valid data values. This does not include “garbage” that happens to be in memory beyond the list.

6 Why does the list / array distinction matter? §We should only process the valid data (list). §Otherwise we may e.g. include garbage values in the Total of all entries, update both valid and garbage data, sort garbage into valid data and search in the garbage! §This is computer science, not the FBI!

7 List processing. §While it is proper to talk of loading an array, thereafter it is helpful to speak of processing the list: §We should ensure that our algorithm only processes the valid data values, e.g. from Amounts[0] to Amounts[56].

8 2.Sorting lists. §To sort information means to arrange it in some order. §It may be a single item, arranged in ascending or descending order e.g. opening weekend profits for movies from best to worst. §Usually it is a collection of data (a record) sorted in order of a key field.

9 Why sort lists? §1) Shows highest / lowest values at top / bottom. §2) May discover important groupings e.g. a lot of people called Patel, Smith or Slarteebartfast. §3) Easier to search. §Why is a dictionary easier to search than a random listing of words and their definitions?

10 The Bubble Sort. §Idea: §Repeatedly pass through a list, comparing adjacent elements. §If the elements are out of order, swap. §Large values “bubble”, one swap at a time, to the end of the list.

11 Example. §Pass 1: §EBBB §BEEE §FCCC §CFDD §DDFA §AAAF §Pass 2: §BBB §CCC §EDD §DEA §AAE §FFF

12 Example. §Pass 3:Pass 4: B C A A C D E F §Pass 5: § A § B § C § D § E § F

13 Why is it called the “bubble” sort? §After the first pass, it is guaranteed that the largest element (F) is sorted. §After the second pass, it is guaranteed that the second largest element (E) is sorted. §So, if we aren’t lucky, the maximum number of passes for N elements is….

14 Why is it called the “bubble” sort? §N-1. §Why? §If all the elements larger than the smallest are correctly placed, the smallest element must already be correctly placed. §What about the maximum number of comparisons within one pass, comparing each adjacent pair?

15 Why is it called the “bubble” sort? §Also N-1. Why? §So in worse case, if there are N elements, takes N-1 * N-1 or O (N 2 ) comparisons and swaps to sort N elements. §However, the algorithm can be optimized. §(1) If the list is sorted in fewer than N-1 passes, how could we know?

16 Optimization. §Use a boolean Swap flag: set to false before each pass; only set to true if a swap occurs. §So, if it stays false, there were no swaps, so the list must be sorted already. §(2) Do we always need to compare every adjacent pair? §No, because some parts are already sorted.

17 Optimization. §Can continue to do comparisons up until the last element that was swapped, e.g. § BB § CC § AA § E D } Last § D E } Change § F F

18 See BUBSORT.CS §This code shows an optimized Bubble Sort (which is a bit like saying a Turbo tortoise). §Note how arrays are passed as parameters. §Note how the Load function keeps track of the size of the list. §Look at the Bubble Sort logic.

19 Logic of Bubble Sort. §Note the use of a nested loop. §The outer loop controls the passes, running until the first pass with no swaps. §The inner loop controls the comparisons within each pass, running until the last change made on the previous pass. §Note the Swap logic. Why are Temporary variables needed?

20 Swapping. §Suppose we want to swap E and B. §We can’t say move E to B, then B to E, because the first move destroys B. §So? §1) Move E to Temp. §2) Move B to E. §3) Move Temp to B.

21 Limitation of the Bubble Sort. §The Bubble Sort is slow. §But why? §Can only swap adjacent elements. §Consider the worst case scenario: §Index Value §[0] 999 §.. §[999] 0

22 What is the problem? §It will take 999 comparisons and swaps to place the Value 999 at the correct location [999]. Shuffling is very slow. §What would help solve this problem?

23 The Shell Sort. §Donald Shell suggested that we compare elements separated by a larger gap. §Start with a gap half the size of the list. Keep passing through until there is a pass with no swaps, then halve the gap. §Why does this help? §See diagram and Shell sort code.

24 Efficiency. §Studies find no significant difference between the Shell Sort and Bubble Sort for small lists e.g. <= 10 elements. §But for 100 / 1000 / 10000, increasing advantage to use Shell Sort. §The Shell Sort is estimated to be an §O (N 1.25 ) algorithm.

25 Searching Lists. §If a list is unsorted, we are forced to do a serial or linear search: §I = 0; §Found = false; §while (!Found && I < ListSize) { § if (SearchElement != List[I]) § I++; § else § Found = true; §} // end while

26 But this can be tedious… §A serial search is O (N), like loading an array. §Only Forrest Gump would search a dictionary this way! §Why is this silly? §How would we search a dictionary?

27 The Binary Search (for sorted lists). §Use 3 indexes Start, Stop and Middle. §Compare SearchElement to Element[Middle] §if (SearchElement < List[Middle]) § Stop = Middle -1; §else §if (SearchElement > List[Middle] ) § Start = Middle + 1 §else § Found = true;

28 More technically: §Start = 0; §Stop = ListLength-1; §Found = false; §while ((Start <= Stop) && (!Found)) { § Middle = (Start + Stop) / 2; § if (SearchElement < List[Middle]) § Stop = Middle -1; § else if (SearchElement > List[Middle]) § Start = Middle + 1; § else { § Found = true; § Location = Middle } §} // end while

29 Example: §A B C D E F G §[0] [1] [2] [3] [4] [5] [6] §Start Middle Stop §E.g. Find ‘B’ §E.g. Find ‘E’.