catie welsh april 20, 2011. program 4 due wed, april 27 th by 11:59pm final exam, comprehensive...
TRANSCRIPT
![Page 1: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/1.jpg)
Catie WelshApril 20, 2011
![Page 2: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/2.jpg)
Program 4 due Wed, April 27th by 11:59pm
Final exam, comprehensive◦ Friday, May 6th, 12pm
No class Friday - Holiday
2
![Page 3: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/3.jpg)
3
![Page 4: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/4.jpg)
Search Algorithms
4
![Page 5: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/5.jpg)
The array itself is referred to by the name scores (in this particular case)
5
0 1 2 3 4
68 73 57 102 94
Indices
the array scoresscores[3]
![Page 6: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/6.jpg)
System.out.println("Enter 5 basketball scores:");
int[] scores = new int[5];int scoreSum = 0;for (int i = 0; i < scores.length; i++){ scores[i] = keyboard.nextInt(); scoreSum += scores[i];}double average = (double) scoreSum / scores.length;System.out.println("Average score: " + average);
for (int i = 0; i < scores.length; i++){ if (scores[i] > average) System.out.println(scores[i] + ": above average"); else if (scores[i] < average) System.out.println(scores[i] + ": below average"); else System.out.println(scores[i] + ": equal to the
average");}
6
![Page 7: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/7.jpg)
Given an array of numbers, sort the numbers into ascending order
Input array:
Sorted array:
7
4 7 3 9 6 2 8
2 3 4 6 7 8 9
![Page 8: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/8.jpg)
public static void printArray(int[] array) {
for(int i = 0; i < array.length; i++) System.out.print(array[i] + " ");
System.out.println( ); }
8
![Page 9: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/9.jpg)
Get in groups of 2-3 people Write an algorithm to search for an item in
an unsorted list Start with find the index of 5 in the list
◦ 3 6 2 1 7 8 5 9 10 3 Then turn that into a Java method with the
following header:public int findIndex(int x, int[] array)
Finally, think about a more efficient way to do that with a sorted array – write pseudocode
Hand in with names on it9
![Page 10: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/10.jpg)
public int findIndex(int x, int[] array){
for(int i =0; i< array.length; i++){
if(array[i] == x)return i;
}return -1;
}
10
![Page 11: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/11.jpg)
Do we need to start at the beginning and look all the way through?
What if we start in the middle of the array?◦ int midPoint = (array.length / 2);
Since the array is sorted:◦ If array[midPoint] > x, we know x must be in first
half of array◦ Else if array[midPoint] > x, x must be in 2nd half of
array◦ Else if array[midPoint] == x, return midPoint
This is called a binary search
11
![Page 12: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/12.jpg)
public int binarySearch(int x, int[] array){
int min= 0;int max= array.length-1;while (min <= max){
int mid = (min+ max) / 2;if(array[mid] > x) max = mid-1; else if(array[mid] < x) min = mid + 1; else //must be equal return mid;
}return -1;
}
12
![Page 13: Catie Welsh April 20, 2011. Program 4 due Wed, April 27 th by 11:59pm Final exam, comprehensive ◦ Friday, May 6th, 12pm No class Friday - Holiday](https://reader036.vdocuments.mx/reader036/viewer/2022070413/5697bfc21a28abf838ca51d0/html5/thumbnails/13.jpg)
Review of Arrays, Inheritance and Sorting/Search Algorithms
13