{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

LectureEighteen - C Programming Lecture 18 Arrays Part III...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 18 Arrays – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Hashemite University ٢ Searching Arrays I Search array for a key value and return the subscript of the location in the array. Two types: Linear search. Binary search. Linear search Compare each element of array with key value till you find the key. Useful for small and unsorted arrays. The best case is to find the key in the first element of the array. So, you need 1 comparison or one step. The worst case is to find the key in the last element of the array. So, you need arraySize comparisons or steps.
Background image of page 2
The Hashemite University ٣ Searching Arrays II Binary search Can only be used on sorted arrays. Compares middle element with key If equal, match found If key < middle, repeat search through the first half of the array If key > middle, repeat search through the last half of the array Very fast; at most n steps, where 2 n is the smallest number larger than the number of elements in the array. E.g.: 30 element array takes at most 5 steps 2 5 > 30
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
The Hashemite University ٤ Linear and Binary Search Code On board. Found in Figure 3.20 in the textbook.
Background image of page 4
The Hashemite University ٥ Multiple-Subscripted Arrays I Multiple subscripts - tables with rows, columns Like matrices: specify row, then column.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}