25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CSE Lecture 3 – Algorithms I
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CS0007: Introduction to Computer Programming Array Algorithms.
HST 952 Computing for Biomedical Scientists Lecture 9.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
C++ Plus Data Structures
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 19 Searching, Sorting and Big O
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Copyright Prentice Hall Modified by Sana odeh, NYU
Chapter 19 Searching and Sorting
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Sorts, CompareTo Method and Strings
Chapter 9: Sorting and Searching Arrays
16 Searching and Sorting.
19 Searching and Sorting.
Searching and Sorting Algorithms
Sorting Mr. Jacobs.
Growth of Functions & Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Warmup What is an abstract class?
Chapter 20 Searching and Sorting
Design and Analysis of Algorithms
Chapter 13: Searching and Sorting
10.3 Bubble Sort Chapter 10 - Sorting.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Linear and Binary Search
Algorithm design and Analysis
Unit-2 Divide and Conquer
Chapter 8 Search and Sort
Chapter 5 Arrays Introducing Arrays
Lecture 11 Searching and Sorting Richard Gesick.
MSIS 655 Advanced Business Applications Programming
C++ Plus Data Structures
Searching and Sorting 1-D Arrays
24 Searching and Sorting.
Data Structures (CS212D) Week # 2: Arrays.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Chapter 4.
Chapter 23 Searching and Sorting
Searching/Sorting/Searching
Sorting and Searching -- Introduction
Chapter 19 Searching, Sorting and Big O
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Module 8 – Searching & Sorting Algorithms
ADT LIST So far we have seen the following operations on a “list”:
Presentation transcript:

25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified by L.Lilien are © 2006-2009 Leszek T. Lilien. Permision to use for non-commercial purposes slides added by L.Lilien’s will be gladly granted upon a written (e.g., emailed) request.

25.2 Searching Algorithms Linear Search Big O notation Searches each element in an array sequentially Has O(n) time The worst case is that every element must be checked to determine whether the search item exists in the array Big O notation One way to describe the efficiency of a search Measures the worst-case run time for an algorithm O(1) is said to have a constant run time O(n) is said to have a linear run time O(n2) is said to have a quadratic run time

Outline (1 of 2) Fill int array with random numbers LinearArray.cs (1 of 2) Fill int array with random numbers Iterate through array sequentially Compare each array element with search key Return index if search key is found Return -1 if search key is not found

Print out each element in the array Outline Print out each element in the array LinearArray.cs (2 of 2)

Outline (1 of 3) Create a new LinearArray of size 10 LinearSearch Test.cs (1 of 3) Create a new LinearArray of size 10 Output array elements Prompt user for a search key Slide modified by L. Lilien

Outline Execute linear search on the array for the search key (2 of 3) LinearSearch Test.cs (2 of 3) Output the appropriate message based on the linear search’s result Continue prompting user for another search key

Outline Complete output: LinearSearch Test.cs (3 of 3)

**READ LATER** Performance Tip 25.1 Sometimes the simplest algorithms perform poorly. Their virtue is that they are easy to program, test and debug. Sometimes more complex algorithms are required to realize maximum performance.

25.2 Searching Algorithms (Cont.) Binary Search Requires that the array be sorted For this example, assume the array is sorted in ascending order The first iteration of this algorithm tests the middle element If this matches the search key, the algorithm ends If the search key is less than the middle element, the algorithm continues with only the first half of the array The search key cannot match any element in the second half of the array If the search key is greater than the middle element, the algorithm continues with only the second half of the array The search key cannot match any element in the first half of the array

25.2 Searching Algorithms (Cont.) Binary Search -- Cont. Each iteration tests the middle value of the remaining portion of the array Called a subarray If the search key does not match the element, the algorithm eliminates half of the remaining elements The algorithm ends either by finding an element that matches the search key or reducing the subarray to zero size Its time complexity is O(log n) — logarithmic run time More efficient than the linear search algorithm with O(n)

Output of the BinarySearchTest program Next 3 slides show class BinaryArray used next in the BinarySearchTest program Output of the BinarySearchTest program In addition to “real” final output, it also produces “educational” output (such as intermediate results and asterisks) Shows two searches (for values: 72 and 13) Then, -1 is entered by the user to quit

Outline (1 of 3) Fill int array with random integers BinaryArray.cs (1 of 3) Fill int array with random integers Sort the array of random numbers in ascending order (requirement of the binary search) Declare local variables to store the indexes of the array for the binary search Calculate the middle, low, and high end index of the array Initializes location to -1 which is the value returned if element is not found

Outline BinaryArray.cs (2 of 3) Tests whether the value in the middle element is equal to searchElement If found, assigns middle to location Eliminate the appropriate half of the remaining values in the array Determine the new middle position of the remaining array Loop until low is greater than high or searchElement is found Return result

Return a range of numbers in the array Outline BinaryArray.cs (3 of 3) Return a range of numbers in the array

Outline (1 of 3) Create an ordered array of random numbers BinarySearch Test.cs (1 of 3) Create an ordered array of random numbers Prompt user for a search key

Outline Perform binary search on the search key (2 of 3) BinarySearch Test.cs (2 of 3) Output the appropriate result Prompt user for another search key

Outline BinarySearch Test.cs (3 of 3) Complete output:

25.3 Sorting Algorithms Selection Sort The first iteration selects the smallest element in the array and swaps it with the first element The second iteration selects the second-smallest item and swaps it with the second element Continues until the last iteration selects the second-largest element and swaps it with the second-to-last index Leaves the largest element in the last index Note: After the ith iteration, the smallest i items of the array will be sorted in increasing order in the first i elements of the array Is a simple, but inefficient, sorting algorithm: O(n2)

Output of the SelectionSortTest program Next 3 slides show class SelectionSort used next in the SelectionSortTest program Output of the SelectionSortTest program In addition to “real” final output, it also produces “educational” output (intermediate results, asterisks and underlines)

Outline SelectionSort.cs (1 of 3) Store the index of the smallest element in the remaining array Iterate through the whole array data.Length – 1 times Initializes the index of the smallest element to the current item

Outline Determine the index of the remaining smallest element Place the smallest remaining element in the next spot SelectionSort.cs (2 of 3) Swap two elements

Outline SelectionSort.cs (3 of 3)

Outline Create an array of random integers (1 of 2) SelectionSort Test.cs (1 of 2) Perform selection sort on the array

Outline SelectionSort Test.cs (2 of 2)

25.3 Sorting Algorithms (Cont.) Insertion Sort The first iteration takes the second element in the array and swaps it with the first element if it is less than the first element The second iteration looks at the third element and inserts it in the correct position with respect to the first two elements All three elements will be in order Note: At the ith iteration of this algorithm, the first i elements in the original array will be sorted Another simple, but inefficient, sorting algorithm: O(n2)

Output of the InsertionSortTest program Next 3 slides show class InsertionSort used next in the InsertionSortTest program Output of the InsertionSortTest program In addition to “real” final output, it also produces “educational” output (intermediate results, asterisks and underlines)

Outline InsertionSort.cs (1 of 3) Holds the element to be inserted while the order elements are moved Iterate through data.Length - 1 items in the array Stores the value of the element that will be inserted in the sorted portion of the array

Outline Keep track of where to insert the element Loop to locate the correct position to insert the element InsertionSort.cs (2 of 3) Moves an element to the right and decrement the position at which to insert the next element Inserts the element in place after pass 1: 12 27* 36 28 33 92 11 93 59 62 -- -- after pass 2: 12 27 36* 28 33 92 11 93 59 62 -- -- -- after pass 3: 12 27 28* 36 33 92 11 93 59 62 -- -- -- --

Outline (3 of 3) InsertionSort.cs after pass 1: 12 27* 36 28 33 92 11 93 59 62 -- -- after pass 2: 12 27 36* 28 33 92 11 93 59 62 -- -- -- after pass 3: 12 27 28* 36 33 92 11 93 59 62 -- -- -- --

Outline Create an array of random integers (1 of 2) InsertionSort Test.cs (1 of 2) Perform insertion sort on the array

Outline InsertionSort Test.cs (2 of 2)

**READ LATER** 25.3 Sorting Algorithms (Cont.) Merge Sort Sorts an array by splitting it into two equal-sized subarrays Sort each subarray and merge them into one larger array With an odd number of elements, the algorithm still creates two subarrays One subarray will have one more element than the other Merge sort is an efficient sorting algorithm: O(n log n) Conceptually more complex than selection sort and insertion sort

**READ LATER** Outline MergeSortTest.cs (2 of 3)

**READ LATER** Outline MergeSortTest.cs (3 of 3)

**READ LATER** Outline (1 of 5) MergeSort.cs (1 of 5) The arguments are the beginning and ending indices of the array to be sorted

**READ LATER** Outline Test base case: if size of the array is 1, the array is already sorted MergeSort.cs (2 of 5) Calculate the middle elements Recursively call SortArray on the two halves of the array Combine the two sorted arrays into one larger sorted array

**READ LATER** Outline Temporary local variables to store the indices of the array for merging MergeSort.cs (3 of 5) Loop until the application reaches the end of either subarray Test which element at the beginning of the arrays is smaller Places the smaller element into position in the combined array

**READ LATER** Outline Test whether left array has reached its end Fill the combined array with the remaining elements of the right array MergeSort.cs (4 of 5) If the right array has reached its end, fill the combined array with the remaining elements of the left array Copy the combined array into the original array

**READ LATER** Outline MergeSort.cs (5 of 5)

**READ LATER** Outline Create an array of random integers (1 of 3) MergeSortTest.cs (1 of 3) Perform merge sort on the array

**READ LATER** Outline MergeSortTest.cs (2 of 3)

**READ LATER** Outline MergeSortTest.cs (3 of 3)

Fig. 25.12 | Searching and sorting algorithms with Big O values.

Fig. 25.13 | Number of comparisons for common Big O notations.

End of Ch. 25 (Searching and Sorting)