Selection Sort Section 10.2 Code for Selection Sort (cont.) Code - - PowerPoint PPT Presentation

selection sort
SMART_READER_LITE
LIVE PREVIEW

Selection Sort Section 10.2 Code for Selection Sort (cont.) Code - - PowerPoint PPT Presentation

Selection Sort Section 10.2 Code for Selection Sort (cont.) Code for an Array Sort Code for an Array Sort Code for Selection Sort (cont.) Code for Selection Sort (cont.) Code for Selection Sort (cont.) Bubble Sort Section 10.3 Code for


slide-1
SLIDE 1

Section 10.2

Selection Sort

slide-2
SLIDE 2

Code for Selection Sort (cont.)

slide-3
SLIDE 3

Code for an Array Sort

slide-4
SLIDE 4

Code for an Array Sort

slide-5
SLIDE 5

Code for Selection Sort (cont.)

slide-6
SLIDE 6

Code for Selection Sort (cont.)

slide-7
SLIDE 7

Code for Selection Sort (cont.)

slide-8
SLIDE 8

Section 10.3

Bubble Sort

slide-9
SLIDE 9

Code for Bubble Sort

slide-10
SLIDE 10

Code for Bubble Sort

slide-11
SLIDE 11

Section 10.4

Insertion Sort

slide-12
SLIDE 12

Code for Insertion Sort

slide-13
SLIDE 13

Code for Insertion Sort

slide-14
SLIDE 14

Using iterator_traits to Determine the Data Type of an Element

 The following version of function insert follows the algorithm

more closely

typename std::iterator_traits<RI>::value_type next_val = *next_pos;  The statement above declares variable next_val and stores

the element referenced by next_pos in it

 The template class iterator_traits is defined in the

header <iterator>, and value_type represents the data type of the element referenced by the iterator next_pos

slide-15
SLIDE 15

Using iterator_traits to Determine the Data Type of an Element (cont.)

template<typename RI> void insert(RI first, RI next_pos) { typename std::iterator_traits<RI>::value_type next_val = *next_pos; // next_val is element to insert. while (next_pos != first && next_val < *(next_pos - 1)) { *next_pos = *(next_pos - 1);

  • -next_pos; // Check next smaller element.

} *next_pos = next_val; // Store next_val where it belongs. }

slide-16
SLIDE 16

Section 10.6

Shell Sort: A Better Insertion Sort

slide-17
SLIDE 17

Code for Shell Sort

slide-18
SLIDE 18

Code for Shell Sort

slide-19
SLIDE 19

Code for Shell Sort

slide-20
SLIDE 20

Section 10.7

Merge Sort

slide-21
SLIDE 21

Code for Merge

slide-22
SLIDE 22

Code for Merge

slide-23
SLIDE 23

Code for Merge Sort

slide-24
SLIDE 24

Code for Merge Sort

slide-25
SLIDE 25

Section 10.8

Heapsort

slide-26
SLIDE 26

Code for Heapsort

slide-27
SLIDE 27

Code for Heapsort (cont.)

slide-28
SLIDE 28

Code for Heapsort (cont.)

slide-29
SLIDE 29

Section 10.9

Quicksort

slide-30
SLIDE 30

Code for Quicksort

slide-31
SLIDE 31

Code for partition

slide-32
SLIDE 32

Code for Revised partition Function

slide-33
SLIDE 33

Code for Revised partition Function

slide-34
SLIDE 34

Section 10.10

Testing the Sort Algorithms

slide-35
SLIDE 35

Testing the Sort Algorithms

 We want to get some idea of the sorting algorithms’

relative performance when sorting the same container (array, vector, or deque)

 Use a variety of test cases

 small and large containers  containers with elements in random order  containers that are sorted already  containers with duplicate values

slide-36
SLIDE 36

Driver to Test Sort Algorithms

slide-37
SLIDE 37

Driver to Test Sort Algorithms