LectureEighteen - Lecture 18 Arrays – Part III By Ghada...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 18 Arrays – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department 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 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....
View Full Document

Page1 / 14

LectureEighteen - Lecture 18 Arrays – Part III By Ghada...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online