{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

LectureEighteen

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

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

Lecture 18 Arrays – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

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

View Full Document
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.
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

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

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}