Searching and sorting arrays

Slides:



Advertisements
Similar presentations
Recursive binary search
Advertisements

For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
Pointers.
Dynamically allocating arrays
Switch statement.
Binary search.
Command-line arguments
Throwing exceptions.
Pointer arithmetic.
Console input.
Dangling pointers.
This.
Sorted arrays.
Dynamically allocating arrays within structures
Break statements.
Keywords.
Linked Lists.
Wild pointers.
The comma as a separator and as an operator
Selection sort.
Bucket sort.
The ternary conditional operator
Dynamically allocating structures
Memory leaks.
Pushing at the back.
Bit-wise and bit-shift operators
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Templated Linked Lists
Repetitious operations
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Dynamically allocating arrays
Throwing exceptions.
Selection sort.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Pointer arithmetic.
Class variables and class functions
Operator overloading.
The std::string class.
Templates.
Insertion sort.
Sorted arrays.
Sorting algorithms.
Issues with classes.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Selection sort.
Protecting pointers.
Data structures: class
An array class: constructor and destructor
Constructors.
This.
Recursive binary search
Algorithms and templates
Presentation transcript:

Searching and sorting arrays

Outline In this lesson, we will: Add member functions for search and sorting the array class Understand why this is a reasonable approach

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 );

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 );

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

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; };

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

References [1] No references?

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 https://www.rbg.ca/ for more information.

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.