Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.

Similar presentations


Presentation on theme: "CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT."— Presentation transcript:

1 CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT

2 Plots plot(x,y,’line specifiers’, ‘PropertyName’, PropertyValue) x = vector y = vector ‘line specifiers’ = defines type and color of line and markers (optional) ‘PropertyName’ = defines values used to adjust line width, marker size, and fill colors (optional)

3 Plots

4

5

6

7 Sorting an array of numbers Input: [ 4 7 50 63 21 1 4 90 ] Output: [ 1 4 7 21 50 63 90 ] Algorithm: –Move through array and compare each pair of numbers –Find the smallest and move to the extreme left. Then ignore the first element and find smallest in the rest of the array and move to the second. Then ignore second and find smallest in rest of array and …

8 Sorting an array of numbers Input: [ 4 7 50 63 21 1 90 ] Output: [ 1 4 7 21 50 63 90 ] Algorithm: –[ 4 7 50 63 21 1 90 ] Is 7 smaller than 4? No. Is 50 smaller than 4? No. … Is 1 smaller than 4. Yes. Switch. Continue. –[ 1 7 50 63 21 4 90 ] Now ignore the first number because we know it is the smallest. Start from second number 7. Is 50 smaller than 7? No… Is 4 smaller than 7? Yes. Switch and continue –[ 1 4 50 63 21 7 90 ] Now ignore the second number because we know it is te second smallest. Start from the third number 50. Is 63 smaller than 50? No… Is 7 smaller than 50? Yes. Switch. Continue…

9 Sorting an array of numbers Is the previous algorithm correct? Yes, because we make sure that the smallest is in the correct order after each pass of the array How long does this take? For the input [ 4 7 50 63 21 1 90 ] we traversed 6 numbers for the first position, 5 numbers for the second position, 4 numbers for the third,… So total number of comparisons made were 6+5+4+3+2+1 = 21

10 Sorting an array of numbers If the array is of arbitrary size of say n numbers, then how many comparisons have to be made for sorting the array? Based upon previous analysis it would be (n-1) + (n-2) + (n-3) + … 2 + 1 = ?? Let’s not worry about what the above quantity sums to (it’s actually n(n-1)/2) but it looks like it is at most n^2. So we say that the running time of this algorithm is n^2

11 Searching an array Input: A (sorted array), x (integer) Output: 1 if x is in A and 0 otherwise Algorithm: –Traverse the array and compare x with each element of A Running time (number of comparisons made): since we have to traverse the entire array, the number of comparisons made is equal to the number of elements in the array Can we do this faster?

12 Searching a sorted array We can make use of the fact that the array is sorted to cut down the number of comparisons. For example if the number to be searched for is large it is likely that it is in the extreme right of the array. Binary search: –Look for query x in the middle of the array –If found return 1. –Otherwise if x is larger than the number in the middle of the array look in the right half of the array. If x is smaller than the number in the middle look in the left half of the array

13 Searching a sorted array Input [ 2 4 5 8 10 12 13 15 16 ] Query: 15 Look in middle [ 2 4 5 8 10 12 13 15 16 ] 15 is larger than 10 so look in second half of array Look in middle of [ 10 12 13 15 16 ] 15 is larger than 13 so look in second half Look in middle of [13 15 16 ]. Found!

14 Searching a sorted array What if we were looking for 14? When the array is reduced to [13 15 16 ] we would search in the left half Since the left half has an even number of elements [13 15] we search take the floor which gives 13. At this point the full array has been searched and we output 0.

15 Searching a sorted array The previous algorithm took 3 comparisons. A traversal through the entire array would have taken [ 2 4 5 8 10 12 13 15 16 ] 8 comparisons. Binary search is faster. A linear search that traverses an array of n numbers makes at most n comparisons. A binary search makes at most log(n) comparisons.


Download ppt "CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT."

Similar presentations


Ads by Google