Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.

Similar presentations


Presentation on theme: "Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which."— Presentation transcript:

1 Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which to the computer is the same as numerically since characters are stored internally as numeric values.) Searching of an item becomes easy in an ordered array Addition of a new item in an ordered array is an expensive operation.

2 Example – searching for a given name in an ordered array.

3 Using Part of an Array For some applications, we do not know in advance how many elements are to be placed in the array. we must dimension the array large enough to handle all reasonable contingencies. E.g. if array is to hold exam marks for class sizes between 12 and 100 students, we use an upper bound of 100. In such cases, at runtime, we must employ a counter variableto keep track of the actual number of elements actually stored in the array during any run of the program.

4 Search Algorithms Linear Search – Search iteratively, start from the first item – Stop the search If an item is found If item under inspection is greater that the searched item Whole array is searched

5 Binary Search 1.Guess a number, normally a number in the mid of the range of interest is selected 2.If the number is too high search in the lower half otherwise the higher half. 3.Go to step 1 until the number is found or you are working with the range n-n and n is not the number.

6 Working Example of Binary Search Step NumberNumber GuessResultRange of possible values 01-100 150Too High1-49 225Too Low26-49 337Too High26-36 431Too Low32-36 534Too High32-33 632Too Low33-33 733Correct Try to guess number 33 in the range 1-100 using binary search

7 Merging Two Ordered Arrays To consolidatetwo lists into a single ordered list: 1. Compare the two names at the top of the first and second lists. a)If one name alphabetically precedes the other, copy it onto the third list and cross it off its original list. b)If the names are the same, copy the name onto the third list and cross out the name from the first and second lists. 2.Repeat Step 1 with the current top names until you reach the end of either list. 3.Copy the names from the remaining list onto the third list.

8 Passing Arrays to Procedures An array declared in a procedure is local to that procedure. An entire array can be passed to a Function as an argument.

9 Passing an Array Element A single element of an array can be passed to a procedure just like any ordinary numeric or string variable. int main() int num[20]; num[5] = 10; cout<< Triple(num[5]) <<endl; return 0; } int Triple(int x) { return (3 * x); }


Download ppt "Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which."

Similar presentations


Ads by Google