Sorting and Searching -- Introduction

Slides:



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

Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CHAPTER 11 Sorting.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
C++ Plus Data Structures
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
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.
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
Building Java Programs Chapter 13 Searching reading: 13.3.
Chapter 19 Searching, Sorting and Big O
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 8: Sorting and Searching Java Software Structures: Designing.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
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.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Data Structure Introduction.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CSCI 51 Introduction to Programming March 12, 2009.
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.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Sorting b Sorting is the process of arranging a list of items into a particular order b There must be some value on which the order is based b There.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
CSCI 51 Introduction to Programming March 10, 2009.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Searching and Sorting Searching algorithms with simple arrays
Chapter 9: Sorting and Searching Arrays
16 Searching and Sorting.
Sort & Search Algorithms
Growth of Functions & Algorithms
Chapter 9 Polymorphism.
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 13: Searching and Sorting
Searching and Sorting Linear Search Binary Search ; Reading p
Sorts.
Data Structures and Algorithms
Outline Late Binding Polymorphism via Inheritance
Lecture 11 Searching and Sorting Richard Gesick.
C++ Plus Data Structures
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Linear Search Binary Search Tree
24 Searching and Sorting.
Chapter 19 Searching, Sorting and Big O
Algorithms.
Module 8 – Searching & Sorting Algorithms
Arrays.
Sorting Algorithms.
Presentation transcript:

Sorting and Searching -- Introduction Two common programming tasks are sorting a list of items and searching for an item in a list Chapter 13 focuses on: Selection Sort Insertion Sort A generic sort for objects Linear Search Binary Search Hashing Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Sorting Sorting is the process of arranging a list of items into a particular order(순서대로 배치) There are many algorithms for sorting a list of items These algorithms vary in efficiency We will examine two specific algorithms: Selection Sort and Insertion Sort Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Selection Sort The approach of Selection Sort: select one value and put it in its final place in the sort list repeat for all other values An expanded version: scan the list to find the smallest value put it in the first position find the next smallest value put it in the second position repeat until all values are placed Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Selection Sort An example: original: 3 9 6 1 2 smallest is 1: 1 9 6 3 2 smallest is 2: 1 2 6 3 9 smallest is 3: 1 2 3 6 9 smallest is 6: 1 2 3 6 9 See Selection_Sort_Test.java Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Example: Selection_Sort_Test public class Selection_Sort_Test { public static void main (String[] args) { int[] numbers = {3, 9, 6, 1, 2}; Selection_Sort.sort (numbers); for (int index = 0; index < numbers.length; index++) System.out.println (numbers[index]); } // method main } // class Selection_Sort_Test Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Example: Selection_Sort_Test class Selection_Sort { public static void sort (int[] numbers) { int min, temp; for (int index = 0; index < numbers.length-1; index++) { min = index; for (int scan = index+1; scan < numbers.length; scan++) if (numbers[scan] < numbers[min]) min = scan; // swap the values temp = numbers[min]; numbers[min] = numbers[index]; numbers[index] = temp; } } // method sort } // class Selection_Sort Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. 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 An expanded version: consider the first item to be a sorted sublist (of one item) insert the second item into the sorted sublist, shifting items as necessary to make room to insert the new addition insert the third item into the sorted sublist (of two items), shifting as necessary repeat until all values are inserted into their proper position Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Insertion Sort An example: original: 3 9 6 1 2 insert 9: 3 9 6 1 2 insert 6: 3 6 9 1 2 insert 1: 1 3 6 9 2 insert 2: 1 2 3 6 9 See Insertion_Sort_Test.java Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Example: Insertion Sort public class Insertion_Sort_Test { public static void main (String[] args) { int[] numbers = {3, 9, 6, 1, 2}; Insertion_Sort.sort (numbers); for (int index = 0; index < numbers.length; index++) System.out.println (numbers[index]); } // method main } // class Insertion_Sort_Test Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. class Insertion_Sort { public static void sort (int[] numbers) { for (int index = 1; index < numbers.length; index++) { int key = numbers[index]; int position = index; // shift larger values to the right while (position > 0 && numbers[position-1] > key) { numbers[position] = numbers[position-1]; position--; } numbers[position] = key; } // method sort } // class Insertion_Sort Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Comparing Sorts Both Selection and Insertion Sorts are similar in efficiency Outer loops that scan all elements, and inner loops that compare the value of the outer loop with almost all values in the list That is approximately n2 number of comparisons for a list of size n We therefore say that these sorts are of order n2 Other sorts are more efficient: order n log2 n Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Sorting Objects Integers have an inherent order But the order of a set of objects must be defined by the person defining the class Recall that a Java interface can be used as a type name and guarantees that a particular class has implemented particular methods We can use this to develop a generic sort for a set of objects See Object_Sort_Test.java Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Searching The goal is to find a particular target value In an unsorted list, a linear search is a simple technique Scan through the list one item at a time in order looking for the target value 7, -3, 7, 2, 8, -1, 3, 2, 5, 6, 7 If the list is sorted, not all items need to be examined As soon as you encounter a value greater than the target value, you know the target is not in the list Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Example: Linear Search public class Linear_Search_Test { public static void main (String[] args) { int numbers[] = {7, -3, 7, 2, 8, -1, 3, 2, 5, 6, 7}; System.out.println ("The index of 6 is " + Linear_Search.search (numbers, 6)); } // method main } // class Linear_Search_Test Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Example: Linear Search class Linear_Search { public static int search (int[] numbers, int target) { int index = 0; while (index < numbers.length) { if (target == numbers[index]) return index; // target found index++; } return -1; // target not found } // method search } // class Linear_Search Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Binary Search If the list is sorted, we can use a binary search to find the target value Strategy: examine the item in the middle of the list, eliminating half of the items from consideration The target value will either be found, or will be in one half or the other Continue the process Example : Search 9 [2, 3, 5, 7, 7, 8, 9] [7, 8, 9] [9] Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Example: Binary Search public class Binary_Search_Test { public static void main (String[] args) { int[] numbers = {2, 3, 5, 7, 7, 8, 9}; System.out.println ("The index of 5 is " + Binary_Search.search (numbers, 5)); } // method main } // class Binary_Search_Test Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Example: Binary Search class Binary_Search { public static int search (int[] numbers, int target) { int index; int left = 0, right = numbers.length - 1; while (left <= right) { index = (left + right) / 2; if (target == numbers[index]) return index; // target found if (target > numbers[index]) left = index + 1; else right = index - 1; } return -1; // target not found } // method search } // class Binary_Search Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Recursive Binary Search The binary search algorithm can be implemented recursively It has the same general strategy as the iterative version Each recursive call narrows the search section See Binary_Search_Test2.java Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Example: Recursive Binary Search public class Binary_Search_Test2 { public static void main (String[] args) { int[] numbers = {2, 3, 5, 7, 7, 8, 9}; System.out.println ("The index of 5 is " + Binary_Search2.search (numbers, 5)); } // method main } // class Binary_Search_Test2 Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. class Binary_Search2 { public static int search (int[] numbers, int target) { int left = 0, right = numbers.length - 1; return recursive_search (numbers, target, left, right); } // method search private static int recursive_search (int[] numbers, int target, int left, int right) { int index = (left + right) / 2; if (left > right) return -1; // target not found else if (target == numbers[index]) // target found return index; else if (target > numbers[index]) return recursive_search (numbers, target, index+1, right); else return recursive_search (numbers, target, left, index-1); } // method recursive_search } // class Binary_Search2 Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Hashing Hashing is another technique for storing and finding information A hash function produces an integer hash code for any given item The hash code is scaled to a hash index relative to the size of the hash table The item is stored in hash table at the hash index Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.

Copyright 1997 by John Lewis and William Loftus. All rights reserved. Hashing Collisions may occur, resulting in a list of values at a particular index Therefore a small search may be necessary Because hashing is based on calculations, it is simple and efficient Storing and searching involve the same amount of effort Chapter 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved.