visual c++ programming: concepts and projects chapter 9a sorting (concepts)

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

Upload: phoebe-horn

Post on 16-Dec-2015

230 views

Category:

Documents


10 download

TRANSCRIPT

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS

Chapter 9ASorting (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

Objectives (continued)

Visual C++ Programming

3

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

a sorting algorithm (big-O)

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

Visual C++ Programming

5

Visual C++ Programming

6

Visual C++ Programming

7

Visual C++ Programming

8

Visual C++ Programming

9

Visual C++ Programming

10

Visual C++ Programming

11

Sorting Strategies

Visual C++ Programming

12

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

Visual C++ Programming

13

Visual C++ Programming

14

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

Visual C++ Programming

16

Visual C++ Programming

17

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

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

Visual C++ Programming

20

Visual C++ Programming

21

Visual C++ Programming

22

Visual C++ Programming

23

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

Visual C++ Programming

25

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

Visual C++ Programming

27

Visual C++ Programming

28

Visual C++ Programming

29

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

Visual C++ Programming

31

Visual C++ Programming

32

Visual C++ Programming

33

Visual C++ Programming

34

Visual C++ Programming

35

Visual C++ Programming

36

Visual C++ Programming

37

Visual C++ Programming

38

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

Visual C++ Programming

40

Visual C++ Programming

41

Visual C++ Programming

42

Visual C++ Programming

43

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

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

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