Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorted arrays.

Similar presentations


Presentation on theme: "Sorted arrays."— Presentation transcript:

1 Sorted arrays

2 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

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

4 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'

5 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?

6 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

7 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]

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

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

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

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

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

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

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

15 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

16 References [1] No references?

17 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.

18 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.


Download ppt "Sorted arrays."

Similar presentations


Ads by Google