1 / 46

Visual C++ Programming: Concepts and Projects

Visual C++ Programming: Concepts and Projects. Chapter 9A: Sorting (Concepts). Objectives. In this chapter, you will: 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

driskellj
Download Presentation

Visual C++ Programming: Concepts and Projects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual C++ Programming: Concepts and Projects Chapter 9A: Sorting (Concepts)

  2. Objectives In this chapter, you will: • 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 Programming with Visual C++

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

  4. Exchanging Data Values in an Array • Two ways to put data into order • 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 Programming with Visual C++

  5. Exchanging Data Values in an Array (continued) Programming with Visual C++

  6. Exchanging Data Values in an Array (continued) • Swapping requires a temporary variable and three steps Programming with Visual C++

  7. Exchanging Data Values in an Array (continued) • The code for a Swap()method Programming with Visual C++

  8. Exchanging Data Values in an Array (continued) Programming with Visual C++

  9. Exchanging Data Values in an Array (continued) Programming with Visual C++

  10. Exchanging Data Values in an Array (continued) Programming with Visual C++

  11. Exchanging Data Values in an Array (continued) Programming with Visual C++

  12. Sorting Strategies • This chapter investigates three methods of sorting data • Selection sort • Bubble sort • Insertion sort • All of these use nested loops Programming with Visual C++

  13. Sorting Strategies (continued) Programming with Visual C++

  14. Sorting Strategies (continued) • All of the sorting methods • Make a series of passes through the elements of an array • After each pass, one value has been moved to its correct, final position • The sorted portion of the array grows as the unsorted portion shrinks Programming with Visual C++

  15. Sorting Strategies (continued) Programming with Visual C++

  16. The Selection Sort • 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 Programming with Visual C++

  17. The Selection Sort (continued) Programming with Visual C++

  18. The Selection Sort (continued) • The outer loop executes n-1 times • 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 Programming with Visual C++

  19. The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value • Variables needed for the selection sort Programming with Visual C++

  20. The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) • The most important variable is smallIndex • It stores the location (index value) of the smallest value identified up to that point • smallIndex starts out set to 0, meaning that the smallest element so far is in array element [0] • If another array element contains a value smaller than element [0], then smallIndex is set to the location of that element Programming with Visual C++

  21. The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) • The inner loop accesses each element in the unsorted portion of the array • If the value stored in an element is less than the value stored in element [smallIndex], then assign the subscript to smallIndex Programming with Visual C++

  22. The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) Programming with Visual C++

  23. The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) • When the inner loop is finished: • Swap the following two values • The value stored in the first element in the unsorted portion of the array • The value stored in element [smallIndex] Programming with Visual C++

  24. The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) Programming with Visual C++

  25. The Bubble Sort • This 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 Programming with Visual C++

  26. The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements • 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 Programming with Visual C++

  27. The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements (continued) Programming with Visual C++

  28. The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements (continued) Programming with Visual C++

  29. The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements (continued) • Bubble sort code Programming with Visual C++

  30. The Insertion Sort • Analogous to the method used to arrange cards in a hand • Strategy: sort from left to right • 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 Programming with Visual C++

  31. The Insertion Sort (continued) Programming with Visual C++

  32. The Insertion Sort (continued) • Algorithm for the insertion sort of cards • Sorting moves from left to right Programming with Visual C++

  33. Programming with Visual C++

  34. The Insertion Sort (continued) Programming with Visual C++

  35. Programming with Visual C++

  36. Programming with Visual C++

  37. The Insertion Sort (continued) Programming with Visual C++

  38. The Insertion Sort (continued) • Insertion sort algorithm Programming with Visual C++

  39. The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment • The outer loop uses loop control variable i • Select the first value in the unsorted portion of the array • Temporarily “remove it from the list” by making a copy Programming with Visual C++

  40. The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment (continued) • Array element from which the unsorted item was “removed” into temp can now be considered vacant Programming with Visual C++

  41. The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment (continued) • 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 “removed from the list” (temp): • Then assign the value to the next highest element in the array (slide it down the array, opening up a vacancy) Programming with Visual C++

  42. The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment (continued) • The inner loop • If the value in the sorted portion is NOT greater than the one “removed from the list” (temp) : • Then assign the value to the vacancy • data[vacant] = temp; Programming with Visual C++

  43. The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment (continued) Programming with Visual C++

  44. Comparing Sorting Algorithms • 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 Programming with Visual C++

  45. Summary • 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 Programming with Visual C++

  46. Summary (continued) • 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 Programming with Visual C++

More Related