Presentation is loading. Please wait.

Presentation is loading. Please wait.

TRAVERSING AN ARRAY My friend, the FOR loop Analyze an array in other ways than max,min,sort… 11.

Similar presentations


Presentation on theme: "TRAVERSING AN ARRAY My friend, the FOR loop Analyze an array in other ways than max,min,sort… 11."— Presentation transcript:

1 TRAVERSING AN ARRAY My friend, the FOR loop Analyze an array in other ways than max,min,sort… 11

2 Linear Searching “Linear searching” is exactly how we typically work a search – we look at each element and see if it matches. We stop when we reach the end of the array. For example: Our program has generated a matrix. We would like to know if any value in the matrix exceeds the value of 10.

3 Linear Searching, cont. s = size(M); found = []; for r = 1:s(1) for c = 1:s(2) if M(r, c) > 10 found(1) = r; found(2) = c; end size() gives us # of rows and # of columns found will contain the position of the last value found that is greater than 10

4 Binary Searching Binary searching is a fancy name for how we use the phone book – an extremely fast way to search. Since names are sorted in the phone book, we can skip massive portions. For example, if the last name starts with “T”, we can skip about 75% of the phonebook!

5 Binary Searching, cont. Just like with the phonebook, we can search arrays using binary searching. And like the phonebook, this method requires that the information already is sorted. Although it can be modified for matrices, it is most useful when searching vectors.

6 Binary Searching, cont. To use binary searching: Sort the vector Repeat until found or run out of values to check Compute the ½ way index for the current range Is it what you want? If not, is it too high or too low? If too high, move to middle of lower range If too low, move to middle of upper range

7 Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end

8 Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end Repeat until found or run out of values to check

9 Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end Compute the ½ way index for the current range

10 Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end Is it what you want?

11 Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end If too high, move to middle of lower range

12 Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end If too low, move to middle of upper range

13 Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end Have we used up all the values?

14 Wrapping Up Linear Searching implies looking at each element. The for loop is the most appropriate Binary Searching (yellow-pages) zooms in faster on the element to find. DATA MUST BE PRE-SORTED. The while loop is the most appropriate 14


Download ppt "TRAVERSING AN ARRAY My friend, the FOR loop Analyze an array in other ways than max,min,sort… 11."

Similar presentations


Ads by Google