Download presentation
Presentation is loading. Please wait.
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' < 'K' < '_' < 'b' < 'r' < '~' 032 sp 033 ! 034 " 035 # 036 $ 037 % 038 & 039 ' 040 ( 041 ) 042 * 043 + 044 , 045 - 046 . 047 / 048 0 049 1 050 2 051 3 052 4 053 5 054 6 055 7 056 8 057 9 058 : 059 ; 060 < 061 = 062 > 063 ? 064 @ 065 A 066 B 067 C 068 D 069 E 070 F 071 G 072 H 073 I 074 J 075 K 076 L 077 M 078 N 079 O 080 P 081 Q 082 R 083 S 084 T 085 U 086 V 087 W 088 X 089 Y 090 Z 091 [ 092 \ 093 ] 094 ^ 095 _ 096 ` 097 a 098 b 099 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~
4
Ordered strings Strings can be sorted by comparing the first characters that differ "Aardvark" < "Aardwolf" "MacDonald" < "Macanroy" "Zarnett" < "ant" "the" < "therefore" Question: Do we consider lower- and upper-case letters to be the same or different? If two strings are not the same length, pad the shorter one with blanks
5
Sorted arrays An array is said to be sorted if each entry is less than or equal to the next entry If two entries are out of order, we say that forms an inversion A sorted array has no inversions 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:
bool is_sorted( double const array[], std::size_t const capacity ) { for ( std::size_t k{1}; k < capacity; ++k ) { } // No inversions were found return true;
11
Is an array sorted? Inside the loop, we compare consecutive entries:
bool is_sorted( double const array[], std::size_t const capacity ) { for ( std::size_t k{1}; k < capacity; ++k ) { if ( array[k - 1] > array[k] ) { // An inversion is found--the array is not sorted return false; } // No inversions were found 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 We must compare each pair, 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: bool is_sorted( double 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 should re-implement the first function to call the second: bool is_sorted( double 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 you should not implement two algorithms if one can be expressed as a call to another
16
References [1] Wikipedia [2] nist’s Dictionary of Algorithms and Data Structures
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.