Top Banner
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
46

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Dec 16, 2015

Download

Documents

Phoebe Horn
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS

Chapter 9ASorting (Concepts)

Page 2: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Objectives

Visual C++ Programming

2

Create and use a Swap() method to exchange data values in arrays

Use nested loops to sort elements in an array

Implement a selection sort algorithm Learn about the bubble sort technique

Page 3: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Objectives (continued)

Visual C++ Programming

3

Learn about the insertion sort technique Analyze the complexity and efficiency of

a sorting algorithm (big-O)

Page 4: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Exchanging Data Values in an Array

Visual C++ Programming

4

Data ordered from lowest to highest values is in ascending order

Data ordered from highest to lowest is in descending order

To sort data you must be able to exchange (swap) values

Swapping requires a temporary variable and three steps

Page 5: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

5

Page 6: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

6

Page 7: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

7

Page 8: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

8

Page 9: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

9

Page 10: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

10

Page 11: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

11

Page 12: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Sorting Strategies

Visual C++ Programming

12

This chapter investigates three methods of sorting data Selection sort Bubble sort Insertion sort

Page 13: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

13

Page 14: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

14

Page 15: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

The Selection Sort

Visual C++ Programming

15

Start with an unsorted array Find the smallest value in the unsorted

portion of the array Swap the smallest value with the first

value in the unsorted portion of the array

Repeat the process until all of the values in the array have been processed

Page 16: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

16

Page 17: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

17

Page 18: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

The Selection Sort (continued)

Visual C++ Programming

18

The outer loop executes n-1 times Tasks

Assign the location of the first element in the unsorted portion of the list to smallIndex

Find the location of the smallest element in the unsorted array

Swap the smallest element with the first unsorted value

Page 19: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value

Visual C++ Programming

19

The inner loop accesses each element in the unsorted portion of the array If the value stored in an element is less

than that stored in element smallIndex then assign the subscript to smallIndex

When finished, exchange the value stored in the first element in the unsorted portion of the array with that stored in element smallIndex

Page 20: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

20

Page 21: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

21

Page 22: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

22

Page 23: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

23

Page 24: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

The Bubble Sort

Visual C++ Programming

24

The strategy uses multiple swaps within the inner loop

It is analogous to the effervescence of bubbles in a glass Bubbling action seems to rise from the

bottom to the top Data value exchanges decrease from the

bottom of the array up as the sorting process executes

Page 25: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

25

Page 26: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements

Visual C++ Programming

26

The outer loop executes n-1 times The inner loop compares the values

stored in each adjacent pair of elements in the unsorted portion of the array If the value in the first element of the pair

is greater than the value stored in the second element of the pair then the two values are swapped

Page 27: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

27

Page 28: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

28

Page 29: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

29

Page 30: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

The Insertion Sort

Visual C++ Programming

30

Analogous to the method used to arrange cards in a hand

Strategy: Look at the first value in the unsorted

portion of the array compare it to all values in the sorted

portion of the array Insert the value into its correct position

Page 31: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

31

Page 32: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

32

Page 33: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

33

Page 34: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

34

Page 35: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

35

Page 36: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

36

Page 37: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

37

Page 38: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

38

Page 39: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment

Visual C++ Programming

39

The outer loop executes n-1 times Select the first value in the unsorted

portion of the array The inner loop

Start with the last element in the sorted portion of the array

If the value in the sorted portion is greater than the one to be inserted then assign it to the next highest element in the array (slide it down the array opening up a vacancy)

Else, insert the element into the vacant position

Page 40: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

40

Page 41: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

41

Page 42: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

42

Page 43: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Visual C++ Programming

43

Page 44: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Comparing Sorting Algorithms

Visual C++ Programming

44

Worst case scenario Selection sort

Outer loop x inner loop is O(n2) comparisons Outer loop is O(n) exchanges

Bubble sort Outer loop x inner loop is O(n2) comparisons Outer loop x inner loop is O(n2) exchanges

Insertion sort Outer loop x inner loop is O(n2) comparisons Outer loop x inner loop is O(n2) exchanges

Page 45: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Summary

Visual C++ Programming

45

Data may be sorted in ascending or descending order

Sorting frequently requires data value exchanges (swapping)

Sorting strategies covered in this chapter• Selection sort• Bubble sort• Insertion sort

Page 46: VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)

Summary (continued)

Visual C++ Programming

46

• The ascending order selection sort• Searches for the smallest value in the unsorted

portion of the array and swaps it with the first value

• The ascending order bubble sort• Compares values stored in adjacent pairs of

unsorted elements and swaps them if the first is larger than the second

• The ascending order insertion sort• Compares each unsorted value to the sorted

portion of the array and inserts it into position after sliding other elements down