Sequential (Linear) Binary Selection** Insertion** Merge.

Slides:



Advertisements
Similar presentations
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Advertisements

IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
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.
Visual C++ Programming: Concepts and Projects
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
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Simple Sorting Algorithms
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Lecture 08 Sorting. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. – We first looked at.
Aalborg Media Lab 15-Jul-15 Polymorphism Lecture 12 Chapter 9.
Sequential (Linear) Binary Selection Insertion Merge.
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
EFFICIENCY & SORTING CITS1001. Listen to the sound of sorting Various algorithms Quicksort
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.
Chapter 14 Searching and Sorting Section 1 - Sequential or Linear Search Section 2 - Binary Search Section 3 - Selection Sort Section 4 - Insertion Sort.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Computer Science Searching & Sorting.
4/15: Searching Arrays Look at BubbleSort.java Searching arrays: the linear search Searching arrays: the binary search.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
Chapter 9 Searching and Sorting
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
SortingBigOh ASFA AP Computer Science A. Big-O refers to the order of an algorithm runtime growth in relation to the number of items I. O(l) - constant.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Comparison-Based Sorting & Analysis Smt Genap
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.
Sorting  Sorting Problem:  A list of items:  x1, x2, x3, …., xn  Arranging the list in ascending order  X1
3 – SIMPLE SORTING ALGORITHMS
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
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.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Chapter 9 – Part 2 Polymorphism: Sorting & Searching.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Searching and Sorting Searching algorithms with simple arrays
SEARCHING AND SORTING.
Chapter 9: Sorting and Searching Arrays
Chapter 9 Polymorphism.
Chapter 13: Searching and Sorting
Searching and Sorting Linear Search Binary Search ; Reading p
Sorts.
SORTING AND SEARCHING.
Data Structures and Algorithms
Outline Late Binding Polymorphism via Inheritance
24 Searching and Sorting.
Workshop for CS-AP Teachers
Sorting and Searching -- Introduction
Chapter 19 Searching, Sorting and Big O
Applications of Arrays
Arrays.
Presentation transcript:

Sequential (Linear) Binary Selection** Insertion** Merge

Selection sort swaps the current element with the lowest element from the remaining elements in the list. Selection Sort does not swap each time it finds elements out of position. Selection sort makes a complete pass while searching for the next item to swap. At the end of a pass once the item is located, one swap is made.

original pass pass pass pass Array length is 5. Number of passes = 5 – 1 = 4

public void selectionSort( int[] ray ) { for(int i=0; i< ray.length-1; i++) { int min = i; //min = location of lowest value for(int j = i+1; j< ray.length; j++) { if(ray[j] < ray[min]) min = j; //find location of lowest value } if( min != i) { int temp = ray[min]; ray[min] = ray[i]; ray[i] = temp; //put lowest value in pos i }

Recall that a class that implements the Comparable interface defines a compareTo method to determine the relative order of its objects We can use polymorphism to develop a generic sort for any set of Comparable objects The sorting method accepts as a parameter an array of Comparable objects That way, one method can be used to sort an array of People, or Books, or whatever

Selection sort is pretty effective for small lists, but pretty horrible if used on large lists Selection sort consists of two loops The outer loops run based on the number of items in the list The inner loop runs to find the items that need to be moved

The inner loop either locates the spot with the smallest value or the spot with the largest value (depending on whether you are sorting it in ascending or descending order) After the inner loop completes, a swap may occur if needed At most, selection sort will make one swap per pass A pass is one complete execution of the inner loop

The insertion sort first selects an item and moves items up or down based on the comparison to the selected item. The idea is to get the selected item in proper position by shifting items around in the list. This is analogous to the way some people pick up playing cards and order them in their hands.

original after pass after pass after pass after pass Blue signifies elements checked during the pass. They are sorted relative to each other. Highlighted element is the one inserted during the pass.

void insertionSort( int[] stuff) { for (int i=1; i< stuff.length; i++) { int val = stuff[i]; //item to insert int j=i; while(j>0 && val<stuff[j-1]) { stuff[j] = stuff[j-1]; j--; } stuff[j]=val; }

void insertionSort( String[] stuff ) { for (int i=1; i< stuff.length; i++) { String val = stuff[i]; //item to insert int j=i; while(j>0 && val.compareTo(stuff[j-1])<0) { stuff[j] = stuff[j-1]; j--; } stuff[j]=val; }

Insertion Sort is normally more efficient than a Selection Sort Insertion Sort continually sorts the left side of the list while gradually moving to the end. This type of sort is similar to how many people sort cards in their hands

public class Athlete implements Comparable { private String lastName, firstName; //constructor and other methods not listed public int compareTo(Object obj) { Athlete otherAthlete = (Athlete) obj; if (lastName.compareTo(otherAthlete.lastName) 0) return 1; return 0; }

ArrayList list; list=new ArrayList (); list.add(new Athlete(“Bob”,”Smith”)); list.add(new Athlete(“Tom”,”Jones”)); list.add(new Athlete(“Sue”,”Adams”)); list.add(new Athlete(“Joe”,”Bass”)); list.add(new Athlete(“Sara”,”Weiss”)); Collections.sort(list); for(Athlete athlete : list ) System.out.println(athlete); OUTPUT Sue Adams Joe Bass Tom Jones Bob Smith Sara Weiss Athlete object class needs to implement Comparable Have to redefine compareTo() method toString() method will make the print work as desired

Classwork Read over the “Notes – Comparable” handout and answer the Comprehension Questions –On a separate sheet or typed up Begin working on Lab 10.1 – Polymorphic Sorting –Use Listings 10.8 – from your textbook as a guide (pg 507 – 510) –Due Wednesday at midnight (will have tomorrow in class to work on it)