37
Section 10.2 Selection Sort

Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Embed Size (px)

Citation preview

Page 1: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Section 10.2

Selection Sort

Page 2: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Selection Sort (cont.)

Page 3: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for an Array Sort

Page 4: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for an Array Sort

Page 5: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Selection Sort (cont.)

Page 6: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Selection Sort (cont.)

Page 7: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Selection Sort (cont.)

Page 8: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Section 10.3

Bubble Sort

Page 9: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Bubble Sort

Page 10: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Bubble Sort

Page 11: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Section 10.4

Insertion Sort

Page 12: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Insertion Sort

Page 13: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Insertion Sort

Page 14: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

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

Page 15: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

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.

}

Page 16: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Section 10.6

Shell Sort: A Better Insertion Sort

Page 17: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Shell Sort

Page 18: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Shell Sort

Page 19: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Shell Sort

Page 20: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Section 10.7

Merge Sort

Page 21: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Merge

Page 22: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Merge

Page 23: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Merge Sort

Page 24: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Merge Sort

Page 25: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Section 10.8

Heapsort

Page 26: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Heapsort

Page 27: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Heapsort (cont.)

Page 28: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Heapsort (cont.)

Page 29: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Section 10.9

Quicksort

Page 30: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Quicksort

Page 31: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for partition

Page 32: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Revised partitionFunction

Page 33: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Code for Revised partitionFunction

Page 34: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Section 10.10

Testing the Sort Algorithms

Page 35: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

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

Page 36: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Driver to Test Sort Algorithms

Page 37: Selection Sort - ksuweb.kennesaw.eduksuweb.kennesaw.edu/~rguo/2015_Summer/CS3424/Slides/Lecture5online.pdfShell Sort: A Better Insertion Sort. Code for Shell Sort. Code for Shell Sort

Driver to Test Sort Algorithms