Download presentation
Presentation is loading. Please wait.
1
Searching and sorting arrays
2
Outline In this lesson, we will:
Add member functions for search and sorting the array class Understand why this is a reasonable approach
3
Searching and sorting We have now introduced an array class:
class Array { std::size_t capacity; double *p_data; }; We have also introduced a number of searching and sorting functions on standard arrays: template <typename T> std::size_t linear_search( T const array[], std::size_t const begin, std::size_t const end, T const &sought_value );
4
Searching and sorting There are two approaches we could use to incorporate searching and sorting into our array class: Write functions that // Search for the sought value in the entries from // array.at( begin ) to array.at( end - 1 ) std::size_t linear_search( Array array, std::size_t const begin, std::size_t const end, T const &sought_value ); // Sort entries from array.at( begin ) to array.at( end - 1 ) void insertion_sort( Array array, std::size_t const end );
5
Searching and sorting Problems with this approach:
The functions are independent of the array class The operations to be performed should be part of any array class Each time the at(…) member function is called, there is a lot of overhead with checking the range Solution: make these member functions
6
Searching and sorting As member functions, this is a much cleaner approach: class Array { public: // Constructors, destructors, other member functions... std::size_t linear_search( std::size_t const begin, std::size_t const end, double const &sought_value ) const; void insertion_sort( std::size_t const begin, std::size_t const end ); private: std::size_t capacity; double *a_data; };
7
Summary Following this lesson, you now
Know how to add searching and sorting to the array class Understand the benefits: Searching and sorting is The parameters need only be checked once
8
References [1] No references?
9
Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.
10
Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.