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

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.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Searching and Sorting Algorithms Based on D. S
The Quick Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen.
Data Structures & Algorithms CHAPTER 3 Sorting Ms. Manal Al-Asmari.
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.
1 Sorting II: Bubble Sort and Selection Sort CSC326 Information Structure Spring 2009.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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
Array operations II manipulating arrays and measuring performance.
Applications of Arrays (Searching and Sorting) and Strings
Computer Science Searching & Sorting.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
CSE 373 Data Structures and Algorithms
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Chapter 14: Searching and Sorting
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
3 – SIMPLE SORTING ALGORITHMS
Sorting Algorithms: Selection, Insertion and Bubble.
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])
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
The Selection Sort Mr. Dave Clausen La Cañada High School.
1 CS 132 Spring 2008 Chapter 10 Sorting Algorithms.
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.
Searching and Sorting Arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
CS212: Data Structures and Algorithms
Sorting Mr. Jacobs.
Data Structures I (CPCS-204)
Chapter 7 Single-Dimensional Arrays
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
The Sequential Search (Linear Search)
Sorting Algorithms: Selection, Insertion and Bubble
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.
Selection sort Given an array of length n,
Outline Late Binding Polymorphism via Inheritance
Lecture 11 Searching and Sorting Richard Gesick.
Sorting.
Searching and Sorting 1-D Arrays
Sorting.
Ken Lambert & Doug Nance
Mr. Dave Clausen La Cañada High School
24 Searching and Sorting.
Data Structures for Java William H. Ford William R. Topp
Searching and Sorting Arrays
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
The Binary Search by Mr. Dave Clausen
Sorting and Searching -- Introduction
Chapter 19 Searching, Sorting and Big O
Introduction to Sorting Algorithms
The Sequential Search (Linear Search)
The Sequential Search (Linear Search)
Module 8 – Searching & Sorting Algorithms
Mr. Dave Clausen La Cañada High School
Presentation transcript:

Mr. Dave Clausen La Cañada High School 11/19/2018 The Selection Sort Mr. Dave Clausen La Cañada High School Mr. D. Clausen

The Selection Sort Description 11/19/2018 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 Clausen Mr. D. Clausen

The Selection Sort Algorithm 11/19/2018 The Selection Sort Algorithm For each index position i 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 i. Mr. Dave Clausen Mr. D. Clausen

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

A Selection Sort Example Smallest ? Mr. Dave Clausen

A Selection Sort Example Smallest ! Mr. Dave Clausen

A Selection Sort Example Swap Mr. Dave Clausen

A Selection Sort Example Swapped Mr. Dave Clausen

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

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

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

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

A Selection Sort Example Smallest ! Mr. Dave Clausen

A Selection Sort Example Swap Mr. Dave Clausen

A Selection Sort Example Swapped Mr. Dave Clausen

A Selection Sort Example Smallest ? Mr. Dave Clausen

A Selection Sort Example Smallest ? Mr. Dave Clausen

A Selection Sort Example Smallest ! Mr. Dave Clausen

A Selection Sort Example Swap Mr. Dave Clausen

A Selection Sort Example Swapped Mr. Dave Clausen

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

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

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

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

Python Source Code: Selection Sort Mr. Dave Clausen

Python Code for Swap Procedure Mr. Dave Clausen

Java Source Code: Selection Sort public static void selectionSort(int[] list) { for (int index = 0; index < list.length - 1; index ++) int minIndex = findMinimum(list, index ); if (minIndex != index ) swap(list, index , minIndex); } Mr. Dave Clausen

Java Code For Find Minimum public static int findMinimum(int[] list, int first) { int minIndex = first;   for (int index = first + 1; index < list.length; index ++) if (list[index] < list[minIndex]) minIndex = index ; return minIndex; } Mr. Dave Clausen

Java Code for Swap Procedure public static void swap(int[] list, int x, int y) { int temp = list[x]; list[x] = list[y]; list[y] = temp; } Mr. Dave Clausen

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

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

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

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

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

BASIC Code For Selection Sort 8000 REM ################# 8010 REM Selection Sort 8020 REM ################# 8030 FOR ELEMENT = 1 TO MAXNUM - 1 8040 ::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 Clausen

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(n2), because it takes approximately n2 passes to sort the elements. Mr. Dave Clausen