Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)"— Presentation transcript:

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

2 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

3 Objectives (continued) Visual C++ Programming 3  Learn about the insertion sort technique  Analyze the complexity and efficiency of a sorting algorithm (big-O)

4 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

5 Visual C++ Programming 5

6 6

7 7

8 8

9 9

10 10

11 Visual C++ Programming 11

12 Sorting Strategies Visual C++ Programming 12  This chapter investigates three methods of sorting data  Selection sort  Bubble sort  Insertion sort

13 Visual C++ Programming 13

14 Visual C++ Programming 14

15 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

16 Visual C++ Programming 16

17 Visual C++ Programming 17

18 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

19 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

20 Visual C++ Programming 20

21 Visual C++ Programming 21

22 Visual C++ Programming 22

23 Visual C++ Programming 23

24 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

25 Visual C++ Programming 25

26 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

27 Visual C++ Programming 27

28 Visual C++ Programming 28

29 Visual C++ Programming 29

30 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

31 Visual C++ Programming 31

32 Visual C++ Programming 32

33 Visual C++ Programming 33

34 Visual C++ Programming 34

35 Visual C++ Programming 35

36 Visual C++ Programming 36

37 Visual C++ Programming 37

38 Visual C++ Programming 38

39 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

40 Visual C++ Programming 40

41 Visual C++ Programming 41

42 Visual C++ Programming 42

43 Visual C++ Programming 43

44 Comparing Sorting Algorithms Visual C++ Programming 44  Worst case scenario  Selection sort Outer loop x inner loop is O(n 2 ) comparisons Outer loop is O(n) exchanges  Bubble sort Outer loop x inner loop is O(n 2 ) comparisons Outer loop x inner loop is O(n 2 ) exchanges  Insertion sort Outer loop x inner loop is O(n 2 ) comparisons Outer loop x inner loop is O(n 2 ) exchanges

45 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

46 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


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

Similar presentations


Ads by Google