Monday, April 2, 2018 Announcements… For Today… For Next Time…

Slides:



Advertisements
Similar presentations
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Advertisements

. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Sorting Chapter 10.
C++ for Engineers and Scientists Third Edition
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
SORTING Chapter 8 CS Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following.
Chapter 10: Sorting1 Sorting. Chapter 10: Sorting2 Chapter Outline How to use standard sorting functions in How to implement these sorting algorithms:
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Arrays Chapter 7.
Lecture 36 OOP The STL Standard Template Library
Prof. U V THETE Dept. of Computer Science YMA
Motivation for Generic Programming in C++
Sorting Dr. Yingwu Zhu.
Chapter 16: Searching, Sorting, and the vector Type
C++ Templates.
Chapter 14 Templates C++ How to Program, 8/e
C++ Standard Library.
Standard Template Library (STL)
Data Structures and Algorithms
Monday, February 26, 2018 Announcements… For Today…
Monday, March 19, 2018 Announcements… For Today… For Next Time…
Starting Out with C++ Early Objects Eighth Edition
Wednesday, February 28, 2018 Announcements… For Today…
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Friday, February 16, 2018 Announcements… For Today… 4.5-, pgs. 252
Collections Intro What is the STL? Templates, collections, & iterators
9.5 Implementation Considerations for the hash_map
Sorting Chapter 10.
Sorting Chapter 8.
Chapter 9 – Sets and Maps 9.1 Associative Container
10.6 Shell Sort: A Better Insertion
10.8 Heapsort 10.9 Quicksort Chapter 10 - Sorting.
18 – Sequential Containers
10.3 Bubble Sort Chapter 10 - Sorting.
C++ Templates L03 - Iterator 10 – Iterator.
10 – Iterators C++ Templates 4.6 The Iterator pgs
Wednesday, April 11, 2018 Announcements… For Today…
L09 - Pokémon.
Associative Structures
Lab 03 - Iterator.
C++ STL Vector Container
L09 - Pokémon.
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
14 – Sequential Containers
Sorting Dr. Yingwu Zhu.
Sorting Chapter 8 CS 225.
Sorting Chapter 8.
Engineering Problem Solving with C++, Etter
C++ Templates L03 - Iterator 10 – Iterator.
Sorting Chapter 10.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Sorting Dr. Yingwu Zhu.
An Introduction to STL.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
14 – Sequential Containers
Lab 03 – Linked List.
5.3 Implementing a Stack Chapter 5 – The Stack.
SPL – PS1 Introduction to C++.
Chapter 9 – Sets and Maps 9.1 Associative Container
10.3 Bubble Sort Chapter 10 - Sorting.
Standard Template Library
Presentation transcript:

Monday, April 2, 2018 Announcements… For Today… For Next Time… Sort Announcements… Midterm Exam II thru Tomorrow (w/fee). Zipped file contains ONLY .cpp, .h files Direct emails to cs235ta@cs.byu.edu Submitting inaccurate Peer Reviews is DISHONEST! Sharing Attendance Quiz Codes is CHEATING! For Today… 10.1-2 For Next Time… 10.3-4

Attendance Quiz #34 Sort

L09 - Pokémon

Template Specialization in C++ Sort Templates in C++, are a feature so that we write code once and use it for any data type including user defined data types. A function sort() can be written to sort any data type items. A class stack can be created that can stack of any data type. What if we want a different code for a particular data type? Consider a project that needs a map class for many different data types. A generic map is templated for a key data type and a value data type. While the key data type might be used in all mappings, different mapped value types might require different implementations. Is it possible to use different code for different value data types? It is possible in C++ to get a special behavior for a particular data type - this is called template specialization.

MapInterface.h Sort //**** YOU MAY NOT MODIFY THIS DOCUMENT ****/ #ifndef MAP_INTERFACE_H #define MAP_INTERFACE_H #include <string> template <typename K, typename V> class MapInterface { public: static const int HashTableSize = 31; static const int BonusHashTableSize = 7; MapInterface() {} virtual ~MapInterface() {} /** Random read/write access operator. */ virtual V& operator[](const K& key) = 0; /** @return: the number of elements removed from the Map. */ virtual size_t erase(const K& key) = 0; /** Removes all items from the Map. */ virtual void clear() = 0; /** @return: number of Key-Value pairs stored in the Map. */ virtual size_t size() const = 0; /** @return: maximum number of Key-Value pairs that the Map can hold. */ virtual size_t max_size() const = 0; /** @return: string representation of Key-Value pairs in Map. */ virtual std::string toString() const = 0; }; #endif // MAP_INTERFACE_H

SetInterface.h Sort //**** YOU MAY NOT MODIFY THIS DOCUMENT ****/ #ifndef SET_INTERFACE_H #define SET_INTERFACE_H #include <string> template<typename T> class SetInterface { public: SetInterface() {} virtual ~SetInterface() {} /** Inserts item into the set. */ virtual bool insert(const T& item) = 0; /** @return: the number of elements removed from the Set. */ virtual size_t erase(const T& item) = 0; /** Removes all items from the set. */ virtual void clear() = 0; /** @return: the number of elements contained by the Set. */ virtual size_t size() const = 0; /** @return: return 1 if contains element equivalent to item, else 0. */ virtual size_t count(const T& item) const = 0; /** @return: string representation of items in Set. */ virtual std::string toString() const = 0; }; #endif // SET_INTERFACE_H

The map key is generic, but the value is specialized. Map.h Sort #ifndef MAP_H #define MAP_H #include "SetInterface.h" #include "MapInterface.h" // Generic template for map<key,value> template<typename K, typename V> class Map : public MapInterface<K, V> {}; #endif // MAP_H // Specialized template for map<key,Set<string>> template<typename K> class Map<K, Set<string>> : public MapInterface<K,Set<string>> { private: Set<string>* keys; public: Map() {} Map(K k, Set<string> v) {} ~Map() {} /** Random read/write access operator. */ virtual Set<string>& operator[](const K& key) { return keys[0]; } /** @return: the number of elements removed from the Map. */ virtual size_t erase(const K& key) { return 0; } /** Removes all items from the Map. */ virtual void clear() { return; } . . . }; // Specialized template for map<key,string> template<typename K> class Map<K, string> : public MapInterface<K, string> { private: string* keys; public: Map() {} Map(K k, string v) {} ~Map() {} /** Random read/write access operator. */ virtual string& operator[](const K& key) { return keys[0]; } The map key is generic, but the value is specialized.

Pokémon Maps and Sets  Sort Map<string,string> pokemon; pair<string,string> Charmander:fire Bulbasaur:grass Squirtle:water . . . Snorlax:normal key Map<string,string> moves; pair<string,string> flamethrower:fire brick_break:fighting razor_lear:grass . . . body_slam:normal key Pokemon: Charmander fire Bulbasaur grass Squirtle water . . . Snorlax normal Moves: flamethrower fire brick_break fighting razor_leaf grass . . . body_slam normal fighting:rock,ice,normal flying:fighting,grass ground:rock,fire,electric . . . Map<string,Set<string>> effectivities; key pair<string,set<string>> normal:  Effectivities: fighting: rock ice normal flying: fighting grass ground: rock fire electric . . . normal: fighting:flying flying:rock,electric ground:flying,grass . . . Map<string,Set<string>> ineffectivities; key pair<string,set<string>> normal:rock Ineffectivities: fighting: flying flying: rock electric ground: flying grass . . . normal: rock

pokemon.cpp pokemon: Map<K, string> moves: Map<K, string> Sort #include <iostream> #include <string> using namespace std; #include "Set.h" #include "Map.h" int main(int argc, char* argv[]) { Map<string, string> pokemon; Map<string, string> moves; Map<string, Set<string>> effectivities; Map<string, Set<string>> ineffectivities; pokemon["Charmander"] = "fire"; ineffectivities["fire"].insert("fire"); int action = ineffectivities["fire"].count("fire"); cout << endl << "pokemon: " << pokemon; cout << endl << "moves: " << moves; cout << endl << "effectivities: " << effectivities; cout << endl << "ineffectivities: " << ineffectivities; return 0; } pokemon: Map<K, string> moves: Map<K, string> effectivities: Map<K, Set<string>> ineffectivities: Map<K, Set<string>>

Battles Sort Battles will be much simpler than they are in the actual games. Each Pokémon has a single type (fire, water, rock). Each move (attack) is a single type. The pokemon map specifies the Pokémon type. The move map specifies the attack type. (For example "flamethrower" is attack type "fire" and Charmander uses "fire", hence Charmander can attack using a "flamethrower".) The effectivities map specifies which attack types are more effective. (For example, water is super effective against fire.) The ineffectivities map specifies which attack types are less effective. (For example, fire is not very effective against water.) Any move type that is neither strong nor weak against a Pokémon of a certain type is considered effective. (For example, rock is regularly effective against water because it has no advantage or disadvantage against it.) In a single battle, two Pokémon are chosen and each Pokémon chooses one move to attack the other one with. Whichever Pokémon attacks with the more effective move wins the battle. If the two moves have equal effectiveness, then the battle ends in a tie.

Charmander: flamethrower Squirtle: water_gun Sort Charmander is a Pokémon of type fire. pokemon map (Charmander -> fire) Squirtle a Pokémon of type water. pokemon map (Squirtle -> water) In the battle, Charmander uses flamethrower (fire) and Squirtle uses water_gun (water). moves map (flamethrower -> fire) Fire type moves are effective against grass and ice, but are ineffective against rock, fire, and water. effectivities map (fire -> grass,ice) ineffectivities map (fire -> rock,fire,water) Hence, Charmander (of type fire) using a flamethrower (fire) is ineffective against Squirtle (of type water) using a water_gun (water) Water type moves are effective against ground, rock, and fire, but are ineffective against water and grass. effectivities map (water-> ground,rock,fire) ineffectivities map (water-> water,grass) Hence, Squirtle (of type water) using a water_gun (water) is effective against Charmander (of type fire). Because Charmander's move was ineffective and Squirtle's move was effective, Squirtle wins the battle.

Functions/Concepts NOT Covered Sort Iterators Deletion of elements. Deep Copying Rehashing (grow and shrink) Find method At() access

10.1 Using C++ Sorting Functions 10.1, pgs. 570-572

Chapter Objectives Sort To learn how to use the standard sorting functions in algorithm.h. To learn how to implement the following sorting algorithms: selection sort bubble sort insertion sort Shell sort merge sort heapsort quicksort To understand the difference in performance of these algorithms, and which to use for small, medium, and large arrays. Sorting entails arranging data in decreasing (or non-increasing) or increasing (or non-decreasing) order Familiarity with sorting algorithms is an important programming skill. The study of sorting algorithms provides insight into problem solving techniques such as divide and conquer. the analysis and comparison of algorithms which perform the same task.

Using C++ Sorting Methods The Standard C++ Library (in algorithm.h) provides two sorting functions. Each function uses a pair of random-access iterators (typename RI): template<typename RI> void sort(RI first, RI last); template<typename RI, typename Compare> void sort(RI first, RI last, Compare comp); The first iterator references the first element of the sequence to be sorted. The second iterator references one past the last element in the sequence.

Using C++ Sorting Methods Because these functions require random-access iterators, they can be used only with vectors, deques, and ordinary C pointers (for sorting arrays). The list container supports only bidirectional iterators, so it provides its own sort member function. The actual type of random-access iterator passed to function sort depends on whether we are sorting an array, vector, or deque. The compiler attempts to match the actual types of the arguments to the template parameters. If we use the second version of function sort, we must also pass an object that implements a comparison function.

Using C++ Sorting Methods If array items stores a collection of 16 integers, the array is sorted by: sort(items, items + 16); This statement sorts only the first half of the array, leaving the rest untouched: sort(items, items + 8); This function call sorts the array in descending order: sort(items, items + 16, greater<int>()); where greater<int> implements the comparison operator> for integers (defined in library functional).

Using C++ Sorting Methods The following statements sort the elements of vector v and deque d: sort(v.begin(), v.end()); sort(d.begin(), d.end()); The following statement shows how to sort the elements in vector v in descending order by using the comparison function greater<int>(): sort(v.begin(), v.end(), greater<int>());

Using C++ Sorting Methods There are also two functions named stable_sort, which are similar to the sort functions. The primary difference is that elements that are equal may not retain their relative ordering when sort is used, but they will retain their relative ordering when stable_sort is used. In other words, if there are two occurrences of an item, the one that is first in the unsorted array is guaranteed to be first in the sorted array only if stable_sort is used. The sort function is slightly faster than stable_sort, so it should be used whenever the relative ordering of equal elements is unimportant.

Using C++ Sorting Methods Given the following Compare_Person function class: struct Compare_Person { bool operator()(const Person& p1, const Person& p2) return (p1.family_name < p2.family_name) || (p1.family_name == p2.family_name) && (p1.given_name < p2.given_name); } If people_list is a vector of Person objects, then: sort(people_list.begin(), people_list.end(), Compare_Person()); sorts the elements in people_list in ascending order based on their names. The client program must include header file "People.h", which defines class Person and struct Compare_Person

10.2, pgs. 572-577 10.2 Selection Sort Analysis of Selection Sort Code for Selection Sort Using Iterators Code for an Array Sort 10.2, pgs. 572-577

Selection Sort Selection sort is relatively easy to understand. It sorts an array by making several passes through the array, selecting a next smallest item in the array each time and placing it where it belongs in the array. For simplicity, we illustrate all the sorting algorithms using an array of integer values.

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 n 5 fill pos_min

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill n 5 fill pos_min

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min n 5 fill pos_min 4

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min n 5 fill pos_min 4

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min n 5 fill 1 pos_min 4

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill n 5 fill 1 pos_min 2 pos_min

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill n 5 fill 1 pos_min 2 pos_min

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill n 5 fill 2 pos_min pos_min

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill pos_min n 5 fill 2 pos_min 4

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill pos_min n 5 fill 2 pos_min 4

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill pos_min n 5 fill 3 pos_min 4

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill pos_min n 5 fill 3 pos_min

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill pos_min n 5 fill 3 pos_min

Trace of Selection Sort n = number of elements in the array for fill = 0 to n – 2 do Set pos_min to the subscript of a smallest item in the subarray starting at subscript fill Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 n 5 fill 3 pos_min

Trace of Selection Sort Refinement 5 fill pos_min next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20

Trace of Selection Sort Refinement 5 fill pos_min next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill

Trace of Selection Sort Refinement 5 fill pos_min next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min

Trace of Selection Sort Refinement 5 fill pos_min next 1 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min next

Trace of Selection Sort Refinement 5 fill pos_min next 1 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min next

Trace of Selection Sort Refinement 5 fill pos_min next 2 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min next

Trace of Selection Sort Refinement 5 fill pos_min next 2 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min next

Trace of Selection Sort Refinement 5 fill pos_min 2 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill next pos_min

Trace of Selection Sort Refinement 5 fill pos_min 2 next 3 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min next

Trace of Selection Sort Refinement 5 fill pos_min 2 next 3 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min next

Trace of Selection Sort Refinement 5 fill pos_min 2 next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min next

Trace of Selection Sort Refinement 5 fill pos_min 2 next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill pos_min next

Trace of Selection Sort Refinement 5 fill pos_min 4 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 35 65 30 60 20 fill next pos_min

Trace of Selection Sort Refinement 5 fill pos_min 4 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill next pos_min

Trace of Selection Sort Refinement 5 fill 1 pos_min 4 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill next pos_min

Trace of Selection Sort Refinement 5 fill 1 pos_min next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 1 pos_min next 2 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 1 pos_min next 2 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 1 pos_min 2 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill next pos_min

Trace of Selection Sort Refinement 5 fill 1 pos_min 2 next 3 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 1 pos_min 2 next 3 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 1 pos_min 2 next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 1 pos_min 2 next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 65 30 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 1 pos_min 2 next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 2 pos_min next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 2 pos_min next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 2 pos_min next 3 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 2 pos_min next 3 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 2 pos_min 3 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill next pos_min

Trace of Selection Sort Refinement 5 fill 2 pos_min 3 next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 2 pos_min 3 next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill pos_min next

Trace of Selection Sort Refinement 5 fill 2 pos_min 4 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 65 60 35 fill next pos_min

Trace of Selection Sort Refinement 5 fill 2 pos_min 4 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill next pos_min

Trace of Selection Sort Refinement 5 fill 3 pos_min 4 next for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill next pos_min

Trace of Selection Sort Refinement 5 fill 3 pos_min next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill pos_min next

Trace of Selection Sort Refinement 5 fill 3 pos_min next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill pos_min next

Trace of Selection Sort Refinement 5 fill 3 pos_min next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill pos_min next

Trace of Selection Sort Refinement 5 fill 3 pos_min next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65 fill pos_min next

Trace of Selection Sort Refinement 5 fill 3 pos_min next 4 for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill 1 2 3 4 20 30 35 60 65

Analysis of Selection Sort for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill This loop is performed n-1 times

Analysis of Selection Sort for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill There are n-1 exchanges

Analysis of Selection Sort for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill This comparison is performed (n – 1 - fill) times for each value of fill and can be represented by the following series: (n-1) + (n-2) + ... + 3 + 2 + 1

Analysis of Selection Sort for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill  

Analysis of Selection Sort for fill = 0 to n – 2 do Initialize pos_min to fill for next = fill + 1 to n – 1 do if the item at next is less than the item at pos_min Reset pos_min to next Exchange the item at pos_min with the one at fill For very large n we can ignore all but the significant term in the expression, so the number of comparisons is O(n2) exchanges is O(n) An O(n2) sort is called a quadratic sort

Sort the following array in descending order using selection sort. 1 2 3 4 5 6 7 8 26 54 93 17 77 31 44 55 20 How many exchanges?

Code for Selection Sort

Code for Selection Sort

Code for Selection Sort

Code for Selection Sort

Code for Selection Sort