Download presentation
Presentation is loading. Please wait.
Published byInger Henriksen Modified over 6 years ago
1
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 s to Submitting inaccurate Peer Reviews is DISHONEST! Sharing Attendance Quiz Codes is CHEATING! For Today… 10.1-2 For Next Time… 10.3-4
2
Attendance Quiz #34 Sort
3
L09 - Pokémon
4
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.
5
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; 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; number of Key-Value pairs stored in the Map. */ virtual size_t size() const = 0; maximum number of Key-Value pairs that the Map can hold. */ virtual size_t max_size() const = 0; string representation of Key-Value pairs in Map. */ virtual std::string toString() const = 0; }; #endif // MAP_INTERFACE_H
6
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; 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; the number of elements contained by the Set. */ virtual size_t size() const = 0; return 1 if contains element equivalent to item, else 0. */ virtual size_t count(const T& item) const = 0; string representation of items in Set. */ virtual std::string toString() const = 0; }; #endif // SET_INTERFACE_H
7
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]; } 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.
8
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
9
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>>
10
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.
11
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.
12
Functions/Concepts NOT Covered
Sort Iterators Deletion of elements. Deep Copying Rehashing (grow and shrink) Find method At() access
13
10.1 Using C++ Sorting Functions
10.1, pgs
14
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.
15
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.
16
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.
17
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).
18
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>());
19
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.
20
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
21
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
22
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.
23
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
24
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
25
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
26
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
27
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
28
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
29
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
30
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
31
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
32
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
33
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
34
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
35
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
36
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
37
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
38
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
39
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
40
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
41
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
42
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
43
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
44
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
45
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
46
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
47
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
48
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
49
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
50
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
51
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
52
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
53
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
54
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
55
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
56
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
57
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
58
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
59
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
60
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
61
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
62
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
63
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
64
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
65
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
66
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
67
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
68
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
69
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
70
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
71
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
72
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
73
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
74
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
75
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
76
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
77
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
78
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)
79
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
80
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
81
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?
82
Code for Selection Sort
83
Code for Selection Sort
84
Code for Selection Sort
85
Code for Selection Sort
86
Code for Selection Sort
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.