The Selection Sort Mr. Dave Clausen La Cañada High School.

Slides:



Advertisements
Similar presentations
Selection Sort. Selection Sort Algorithm (ascending) 1.Find smallest element (of remaining elements). 2.Swap smallest element with current element (starting.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
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.
MATH 224 – Discrete Mathematics
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
The Quick Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen.
Selection Sorting Lecture 21. Selection Sort Given an array of length n, –In first iteration: Search elements 0 through n-1 and select the smallest Swap.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
CHAPTER 11 Sorting.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
Searching Arrays Linear search Binary search small arrays
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Lecture 08 Sorting. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. – We first looked at.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
The Insertion Sort Mr. Dave Clausen La Cañada High School.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Array operations II manipulating arrays and measuring performance.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching and Sorting, Template Functions, and Vectors ITK 169 Fall 2003.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
+ ARRAYS - SEARCHING - SORTING Dr. Soha S. Zaghloul updated by Rasha M. AL_Eidan 2015.
CSE 373 Data Structures and Algorithms
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
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.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
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 CS Sorting means... Sorting rearranges the elements into either ascending or descending order within the array. (we’ll use ascending order.)
3 – SIMPLE SORTING ALGORITHMS
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Sorting Arrays ANSI-C. Selection Sort Assume want to sort a table of integers, of size length, in increasing order. Sketch of algorithm: –Find position.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
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.
Sort Algorithm.
The Bubble Sort Mr. Dave Clausen La Cañada High School
CS212: Data Structures and Algorithms
Searching and Sorting Algorithms
Introduction to Search Algorithms
The Sequential Search (Linear Search)
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Mr. Dave Clausen La Cañada High School
Sorting.
Searching and Sorting 1-D Arrays
Sorting.
Ken Lambert & Doug Nance
Mr. Dave Clausen La Cañada High School
24 Searching and Sorting.
The Binary Search by Mr. Dave Clausen
Introduction to Sorting Algorithms
The Sequential Search (Linear Search)
The Sequential Search (Linear Search)
Mr. Dave Clausen La Cañada High School
Presentation transcript:

The Selection Sort Mr. Dave Clausen La Cañada High School

Objectives Understand and use the Selection Sort to sort data in a program.Understand and use the Selection Sort to sort data in a program. Understand and know Big-O notation for the Selection Sort.Understand and know Big-O notation for the Selection Sort. Mr. Dave Clausen2

3 The Selection Sort Description The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first element in the list. Next it finds the smallest of the remaining elements, and “swaps” it with the second element. Repeat this process until you compare only the last two elements in the list.

Mr. Dave Clausen4 The Selection Sort Algorithm For each index positionFor each index position i –Find the smallest data value in the array from positions through length - 1, where length is the number of data values stored. –Find the smallest data value in the array from positions i through length - 1, where length is the number of data values stored. –Exchange (swap) the smallest value with the value at position. –Exchange (swap) the smallest value with the value at position i.

Mr. Dave Clausen5 We start by searching for the smallest element in the List. A Selection Sort Example Smallest ?

Mr. Dave Clausen6 A Selection Sort Example Smallest ?

Mr. Dave Clausen7 A Selection Sort Example Smallest !

Mr. Dave Clausen8 A Selection Sort Example Swap

Mr. Dave Clausen9 A Selection Sort Example Swapped

Mr. Dave Clausen10 A Selection Sort Example After the smallest element is in the first position, we continue searching with the second element and look for the next smallest element. Smallest ?

Mr. Dave Clausen11 A Selection Sort Example In this special case, the next smallest element is in the second position already. Swapping keeps it in this position. Smallest !

Mr. Dave Clausen12 A Selection Sort Example Swapping keeps it in this position. Swapped

Mr. Dave Clausen13 A Selection Sort Example After the next smallest element is in the second position, we continue searching with the third element and look for the next smallest element. Smallest ?

Mr. Dave Clausen14 A Selection Sort Example Smallest !

Mr. Dave Clausen15 A Selection Sort Example Swap

Mr. Dave Clausen16 A Selection Sort Example Swapped

Mr. Dave Clausen17 A Selection Sort Example Smallest ?

Mr. Dave Clausen18 A Selection Sort Example Smallest ?

Mr. Dave Clausen19 A Selection Sort Example Smallest !

Mr. Dave Clausen20 A Selection Sort Example Swap

Mr. Dave Clausen21 A Selection Sort Example Swapped

Mr. Dave Clausen22 A Selection Sort Example The last two elements are in order, so no swap is necessary.

Mr. Dave Clausen23 What “Swapping” Means TEMP Place the first element into the Temporary Variable. 6

Mr. Dave Clausen24 What “Swapping” Means TEMP Replace the first element with the value of the smallest element. 6

Mr. Dave Clausen25 What “Swapping” Means TEMP Replace the third element (in this example) with the Temporary Variable. 6

Mr. Dave Clausen26 C + + Examples of The Selection Sort Sample C++ Program For Selection Sort: sel_sort.cpp Animated Examples: Visual1.exeVisual1.exe Visual1.cpp Visual1.cpp Visual1.exeVisual1.cpp Visual2.exeVisual2.exe Visual2.cpp Visual2.cpp Visual2.exeVisual2.cpp On the Net:

Mr. Dave Clausen27 C + + Code For Selection Sort void Selection_Sort (vector & v) { int min_index = 0; for (int index = 0; index < v.size( ) - 1; ++index) for (int index = 0; index < v.size( ) - 1; ++index){ min_index = Find_Minimum (v, index); min_index = Find_Minimum (v, index); if (min_index ! = index) if (min_index ! = index) Swap_Data ( v [index], v [min_index]) Swap_Data ( v [index], v [min_index]) }} // Selection_Sort

Mr. Dave Clausen28 C + + Code For Find Minimum int Find_Minimum (const vector & v, int first) { int min_index = first; for (int index = first + 1; index < v.size( ); ++index) for (int index = first + 1; index < v.size( ); ++index) if ( v[index] < v[min_index]) if ( v[index] < v[min_index]) min_index = index; return min_index; } // Find_Minimum

Mr. Dave Clausen29 C + + Code for Swap Procedure void Swap_Data (int &number1, int &number2) { int temp; int temp; temp = number1; temp = number1; number1 = number2; number1 = number2; number2 = temp; number2 = temp;} // End of Swap_Data function

Mr. Dave Clausen30 Pascal Code for Swap Procedure procedure Swap (var number1, number2: integer); var temp: integer; temp: integer;begin temp := number1; temp := number1; number1 := number2; number1 := number2; number2 := temp number2 := temp end; {Swap}

Mr. Dave Clausen31 Pascal Code For Selection Sort procedure SelectionSort (var IntArray: IntNumbers); var element, SmallIndex, index: integer; var element, SmallIndex, index: integer;begin for element := 1 to (MaxNum - 1) do for element := 1 to (MaxNum - 1) do begin begin SmallIndex := element; SmallIndex := element; for index := (element + 1) to MaxNum do for index := (element + 1) to MaxNum do if IntArray [index] < IntArray [SmallIndex] if IntArray [index] < IntArray [SmallIndex] then SmallIndex := index; then SmallIndex := index; Swap (IntArray [element], IntArray [SmallIndex]) Swap (IntArray [element], IntArray [SmallIndex]) end; end; end; {SelectionSort}

Mr. Dave Clausen32 BASIC Code For Selection Sort 8000 REM ################# 8010 REM Selection Sort 8020 REM ################# 8030 FOR ELEMENT = 1 TO MAXNUM ::SmallIndex = element 8050 ::FOR INDEX = (element + 1) TO MAXNUM 8060 ::::::IF N (INDEX) < N (SmallIndex) THEN SmallIndex = INDEX 8070 ::NEXT INDEX 8080 ::TEMP = N (element) 8090 ::N (element) = N (SmallIndex) 8100 ::N (SmallIndex) = TEMP 8110 NEXT ELEMENT 8120 RETURN

Mr. Dave Clausen33 Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system. Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Selection Sort is O(n 2 ), because it takes approximately n 2 passes to sort the elements.