1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.

Slides:



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

Garfield AP Computer Science
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CPS120: Introduction to Computer Science Searching and Sorting.
Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
Algorithm An algorithm is a step-by-step set of operations to be performed. Real-life example: a recipe Computer science example: determining the mode.
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.
CHAPTER 11 Sorting.
C++ Plus Data Structures
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching and Sorting Arrays
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
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.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Do Now Take out ch6 test answers – be ready to hand it in Pick a leader in each group of up to 3 students; Leader will retrieve a whiteboard, marker, and.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Chapter 9 Searching and Sorting
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
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.
ArrayList is a class that implements the List interface. The List interface is a blueprint for its “implementor” classes. There is another implementor.
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
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.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Searching and Sorting Arrays
Searching and Sorting Algorithms
Chapter 9 Polymorphism.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Searching & Sorting "There's nothing hidden 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.
Chapter 13: Searching and 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.
CSc 110, Spring 2017 Lecture 39: searching.
Data Structures and Algorithms
Outline Late Binding Polymorphism via Inheritance
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
C++ Plus Data Structures
24 Searching and Sorting.
Searching and Sorting Arrays
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Sorting and Searching -- Introduction
Sorting Algorithms.
Presentation transcript:

1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick Sort –Merge Sort Introduction to Project 3 Reading for this lecture: L&C

2 Searching Searching is the process of finding a target element within a group of items called the search pool The target may or may not be in the search pool We want to perform the search efficiently, minimizing the number of comparisons Let's look at two classic searching approaches: linear search and binary search We'll implement the searches with polymorphic Comparable objects

3 Linear Search A linear search begins at one end of the search pool and examines each element Eventually, either the item is found or the end of the search pool is encountered On average, it will search half of the pool This algorithm is O(n) for finding a single element or determining it is not found

4 Binary Search A binary search assumes the list of items is sorted Each pass eliminates a large part of the search pool with one comparison (“20 questions game”) A binary search examines the middle element of the list -- if the target is found, the search is over The process continues by comparing the target to the middle of the remaining viable candidates Eventually, the target is found or there are no remaining viable candidates (and the target has not been found)

5 Recursive Implementation When we studied binary search in CS110, we used a loop and narrowed the range of the portion of the array we were searching Now that we understand recursion, we can study a recursive implementation: boolean found = binarySearch (array, 0, data.length – 1, element); public boolean binarySearch (T[] data, int min, int max, T target)

6 Binary Search Recursive implementation: { boolean found = false; int mid = (min + max) / 2; if (data[mid].compareTo(target) == 0) found = true; else if (data[mid].compareTo(target) > 0) { if (min <= mid – 1) found = binarySearch(data, min, mid-1, target); } else if (mid + 1 <= max) found = binarySearch(data, mid+1, max, target); return found; }

7 Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) –Sorting a list of test scores in ascending numeric order –Sorting a list of people alphabetically by last name Sequential sorts O(n 2 ): Selection, Insertion, Bubble Logarithmic sorts O(nlogn): Quick, Merge Again, we'll implement the sorts with polymorphic Comparable objects.

8 Selection Sort The approach of Selection Sort: –Select a value and put it in its final place in the list –Repeat for all other values In more detail: –Find the smallest value in the list –Switch it with the value in the first position –Find the next smallest value in the list –Switch it with the value in the second position –Repeat until all values are in their proper places

9 Selection Sort An example: original: smallest is 1: smallest is 2: smallest is 3: smallest is 6: Each time, the smallest remaining value is found and exchanged with the element in the "next" position to be filled

10 Insertion Sort The approach of Insertion Sort: –Pick any item and insert it into its proper place in a sorted sublist –Repeat until all items have been inserted In more detail: –Consider the first item to be a sorted sublist (of one item) –Insert the second item into the sorted sublist, shifting the first item as needed to make room to insert the new addition –Insert the third item into the sorted sublist (of two items), shifting items as necessary –Repeat until all values are inserted into their proper positions

11 Insertion Sort An example: insert 9: insert 6: insert 3: insert 2: finished:

12 Bubble Sort Bubble sort algorithm sorts the values by repeatedly comparing neighboring elements It swaps their position if they are not in order Each pass through the algorithm moves the largest value to its final position A pass may also reposition other elements May be more efficient if looping is stopped when no changes were made on last pass

13 Bubble Sort An example of one pass: Original: Swap 9 & 6: Swap 9 & 8: No swap: Swap 12 & 3: Swap 12 & 1: Swap 12 & 7: Each pass moves largest to last position Each pass can iterate one less time than last

14 Quick Sort The quick sort algorithm is a “divide and conquer” algorithm It compares the data values to a partition element while partitioning into two sub-lists Then, it recursively sorts the sub-lists on each side of the partition element The recursion base case is a list containing only 1 element (which is inherently sorted) A simplistic choice of the partition element is the first element, but that may not be best

15 Quick Sort Initial Data (Select first element as the partition element) Move all data below partition element value to the left Move all data above partition element value to the right Split Point (Only a coincidence that it is in middle) Swap first element with element at the split point Partition element is now in its correct position Next Pass

16 Merge Sort Merge sort is another “divide and conquer” algorithm with a different division strategy –Cut the array of data in half –Sort the left half (recursively calling itself) –Sort the right half (recursively calling itself) –Merge the two sorted halves of the array The merge process for two arrays that are already sorted is only O(n) and we perform O(log n) merges

17 Merge Sort All comparisons are in the merge process Copy all elements into a workspace array Copy them back into the original array –If there are elements remaining in both halves, compare current elements of each and copy one –If there are no elements remaining in left half, copy remainder of right half –If there are no elements remaining in right half, copy remainder of left half