Sorted arrays.

Slides:



Advertisements
Similar presentations
Throwing and catching exceptions
Advertisements

Recursive binary search
Searching an array.
For loops.
Templates.
Introduction to classes
Default values of parameters
The structured programming theorem
Pointers.
Dynamically allocating arrays
Switch statement.
Binary search.
Do-while loops.
Command-line arguments
Throwing exceptions.
Pointer arithmetic.
Console input.
Dynamically allocating arrays within structures
Break statements.
Keywords.
Linked Lists.
Logical operators.
The comma as a separator and as an operator
Selection sort.
Bucket sort.
The ternary conditional operator
Dynamically allocating structures
Conditional statements
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.
Recursive functions.
Class variables and class functions
Operator overloading.
The std::string class.
Dynamic allocation of arrays
Templates.
Insertion sort.
Sorted arrays.
Sorting algorithms.
Counting sort.
Selection sort.
Searching and sorting arrays
Protecting pointers.
Data structures: class
An array class: constructor and destructor
Recursive binary search
The structured programming theorem
Recursive functions.
Algorithms and templates
Presentation transcript:

Sorted arrays

Outline In this lesson, we will: Define an ordering on various types Define when an array is sorted Learn how to determine if an array is sorted Learn how to determine if a sub-range of an array is sorted

Ordered types Suppose that the entries of an array can be ordered: Integer and floating-point data types are both ordered: 3 < 7 –54.2718 < 1.85928 Characters are ordered based on their ascii value: '3' < '.' '.' < 'A' 'A' < 'K' 'S' < '_' '_' < 'b' 'r' < 'z' Additionally, '␢' is less than all printable characters

Ordered strings Strings can be sorted by comparing the corresponding characters 'Aardvark' < 'Aardwolf' 'MacDonald' < 'Macanroy' 'Zarnett' < 'ant' 'the' < 'therefore' If two strings are not the same length, pad the shorter one with blanks Some systems try to be more intelligent: 1. First topic.pdf 1. First topic.pdf 2. Second topic.pdf 10. Tenth topic.pdf 10. Tenth topic.pdf 11. Eleventh topic.pdf 11. Eleventh topic.pdf 2. Second topic.pdf Summary.pdf Summary.pdf Note that '3' < '.' and '.' < 'A'

Sorted arrays An array is said to be sorted if each succeeding entry is greater than or equal to the preceding entry In some cases, we know an array is sorted Every time an entry is placed into the an array or an entry is removed, the remaining entries are moved to maintain order Given an array, can we determine if it is sorted?

Is an array sorted? The algorithm is: Compare the 1st and 2nd entries If they are out of order, the array is not sorted Compare the 2nd and 3rd entries Compare the 3rd and 4th entries Compare the second-last and last entries If we have not found any pairs out of order, the array is sorted

Is an array sorted? As for C++ arrays: Thus, we are comparing The first entry is array[0] The last entry is array[capacity - 1] Thus, we are comparing array[0] and array[1] array[1] and array[2] array[2] and array[3] ⋮ array[capacity - 2] and array[capacity - 1]

Is an array sorted? Based on this, here is a flow chart

Is an array sorted? Here is a slight variation: k goes from 1 up to but not including the capacity

Is an array sorted? Our function will use a for loop: template <typename T> bool is_sorted( T const array[], std::size_t const capacity ) { for ( std::size_t k{1}; k < capacity; ++k ) { } return true;

Is an array sorted? Inside the loop, we compare consecutive entries: template <typename T> bool is_sorted( T const array[], std::size_t const capacity ) { for ( std::size_t k{1}; k < capacity; ++k ) { if ( array[k - 1] > array[k] ) { return false; } return true;

Is an array sorted? Like our linear search: If you double the capacity of an array, it will take twice as long to determine if the array is sorted If you halve the capacity, it will take half as long Make the capacity 10 times larger, it will take 10 times as long to run Again, we must compare every entry, for only two entries may be out of order int array[20]{2, 15, 23, 39, 42, 57, 63, 71, 80, 95, 103, 119, 125, 122, 139, 148, 150, 163, 183, 222};

Is a sub-array sorted? As with a linear search on a sub-array, we will verify that the sub-array is sorted from the index begin up to but not including the index end: template <typename T> bool is_sorted( T const array[], std::size_t const begin, std::size_t const end ) { for ( std::size_t k{begin + 1}; k < end; ++k ) { if ( array[k - 1] > array[k] ) { return false; } return true;

Is a sub-array sorted? Again, this follows the C++ approach to array indexing The entire array can be searched either by is_sorted( some_array, capacity ); is_sorted( some_array, 0, capacity ); Indeed, we could re-implement the first function as a call to the second: template <typename T> bool is_sorted( T const array[], std::size_t const capacity ) { // Call the more general 'is_sorted' function return is_sorted( array, 0, capacity ); }

Summary Following this lesson, you now Understand the definition of a sorted list Know how to program an algorithm for determining if an array is sorted Understand that there is a philosophy of a programming language that you should adhere to

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.