searching and sorting - stanford university...searching given an array of values, determine whether...
TRANSCRIPT
![Page 1: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/1.jpg)
Searching and Sorting
![Page 2: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/2.jpg)
Friday Four Square!Outside Gates, 4:15PM
![Page 3: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/3.jpg)
An Interesting Lecture Excerpt
Admiral Grace Hopper on nanoseconds:
http://www.youtube.com/watch?v=JEpsKnWZrJ8
![Page 4: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/4.jpg)
Algorithms Revisited
● An algorithm is a procedure for effecting some result.
● There can be many different algorithms for solving the same problem.
● How can we compare algorithms against one another?
● What's the best algorithm for solving a given problem?
![Page 5: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/5.jpg)
Two Famous Problems
● Searching● Given an array of values, determine whether
some value is contained in that array.● Very important: finding medical records,
determining if a bookstore has a copy of a book, etc.
● Sorting● Given an array of values, rearrange those values
to put them in sorted order.● Enormously important: shows up in iTunes,
Google, Facebook, etc.
![Page 6: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/6.jpg)
Searching
![Page 7: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/7.jpg)
Can I get some volunteers?
![Page 8: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/8.jpg)
Linear Search
![Page 9: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/9.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
![Page 10: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/10.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
![Page 11: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/11.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
![Page 12: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/12.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i0
![Page 13: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/13.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i0
![Page 14: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/14.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
6 7 5 3 0 9arr
key3
i0
8
![Page 15: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/15.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i0
![Page 16: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/16.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i1
![Page 17: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/17.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i1
![Page 18: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/18.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 7 5 3 0 9arr
key3
i1
6
![Page 19: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/19.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i1
![Page 20: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/20.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i2
![Page 21: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/21.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i2
![Page 22: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/22.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 5 3 0 9arr
key3
i2
7
![Page 23: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/23.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i2
![Page 24: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/24.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i3
![Page 25: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/25.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i3
![Page 26: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/26.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 3 0 9arr
key3
i3
5
![Page 27: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/27.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i3
![Page 28: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/28.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i4
![Page 29: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/29.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i4
![Page 30: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/30.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 0 9arr
key3
i4
3
![Page 31: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/31.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 0 9arr
key3
i4
3
![Page 32: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/32.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 0 9arr
key3
i4
3
![Page 33: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/33.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 0 9arr
key3
i4
3
![Page 34: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/34.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
![Page 35: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/35.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key1
![Page 36: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/36.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key1
![Page 37: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/37.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i01
![Page 38: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/38.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i01
![Page 39: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/39.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
6 7 5 3 0 9arr
key3
i0
8
1
![Page 40: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/40.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i01
![Page 41: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/41.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i11
![Page 42: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/42.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i11
![Page 43: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/43.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 7 5 3 0 9arr
key3
i1
6
1
![Page 44: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/44.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i11
![Page 45: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/45.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i21
![Page 46: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/46.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i21
![Page 47: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/47.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 5 3 0 9arr
key3
i2
7
1
![Page 48: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/48.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i21
![Page 49: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/49.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i31
![Page 50: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/50.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i31
![Page 51: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/51.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 3 0 9arr
key3
i3
5
1
![Page 52: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/52.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i31
![Page 53: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/53.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i41
![Page 54: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/54.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 3 0 9arr
key3
i41
![Page 55: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/55.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 0 9arr
key3
i4
3
1
![Page 56: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/56.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 0 9arr
key3
i4
3
1
![Page 57: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/57.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 0 9arr
key3
i5
3
1
![Page 58: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/58.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 0 9arr
key3
i5
3
1
![Page 59: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/59.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 9arr
key3
i5
3
1
0
![Page 60: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/60.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 9arr
key3
i5
3
1
0
![Page 61: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/61.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 9arr
key3
i6
3
1
0
![Page 62: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/62.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5 9arr
key3
i6
3
1
0
![Page 63: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/63.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5arr
key3
i6
3
1
0 9
![Page 64: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/64.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5arr
key3
i6
3
1
0 9
![Page 65: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/65.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5arr
key3
i7
3
1
0 9
![Page 66: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/66.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5arr
key3
i7
3
1
0 9
![Page 67: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/67.jpg)
Linear Searchprivate int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
8 6 7 5arr
key3
i7
3
1
0 9
![Page 68: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/68.jpg)
Searching II
![Page 69: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/69.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 70: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/70.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 71: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/71.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 72: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/72.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 73: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/73.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 74: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/74.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 75: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/75.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 76: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/76.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 77: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/77.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 78: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/78.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 79: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/79.jpg)
Binary Search
2 3 5 7 11 13 231917 29 37 41
43 47 53 59 716761
31
79 83 89 97 101
103 113109107
73
131 137 139 149 151 157 167163127
![Page 80: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/80.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
![Page 81: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/81.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
key6
![Page 82: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/82.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
key6
![Page 83: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/83.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key6
![Page 84: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/84.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key6
![Page 85: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/85.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key6
![Page 86: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/86.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key6
mid3
![Page 87: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/87.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key6
mid3
![Page 88: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/88.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs0
rhs6
key6
mid3
5
![Page 89: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/89.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs0
rhs6
key6
mid3
5
![Page 90: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/90.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs0
rhs6
key6
mid3
5
![Page 91: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/91.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key6
mid3
5
![Page 92: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/92.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key6
mid3
5
![Page 93: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/93.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key6
5
![Page 94: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/94.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key6
5
![Page 95: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/95.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key6
5
![Page 96: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/96.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key6
5
mid5
![Page 97: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/97.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key6
5
mid5
![Page 98: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/98.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs6
key6
5
mid5
8
![Page 99: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/99.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs6
key6
5
mid5
8
![Page 100: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/100.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs6
key6
5
mid5
8
![Page 101: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/101.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs6
key6
5
mid5
8
![Page 102: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/102.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key6
5 8
![Page 103: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/103.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key6
5 8
![Page 104: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/104.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key6
5 8
![Page 105: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/105.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key6
5 8
mid4
![Page 106: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/106.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key6
5 8
mid4
![Page 107: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/107.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs4
rhs4
key6
5 8
mid4
6
![Page 108: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/108.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs4
rhs4
key6
5 8
mid4
6
![Page 109: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/109.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs4
rhs4
key6
5 8
mid4
6
![Page 110: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/110.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs4
rhs4
key6
5 8
mid4
6
![Page 111: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/111.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
![Page 112: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/112.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
key7
![Page 113: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/113.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
key7
![Page 114: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/114.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key7
![Page 115: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/115.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key7
![Page 116: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/116.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key7
![Page 117: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/117.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key7
mid3
![Page 118: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/118.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 5 6 8 9arr
lhs0
rhs6
key7
mid3
![Page 119: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/119.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs0
rhs6
key7
mid3
5
![Page 120: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/120.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs0
rhs6
key7
mid3
5
![Page 121: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/121.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs0
rhs6
key7
mid3
5
![Page 122: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/122.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key7
mid3
5
![Page 123: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/123.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key7
mid3
5
![Page 124: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/124.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key7
5
![Page 125: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/125.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key7
5
![Page 126: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/126.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key7
5
![Page 127: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/127.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key7
5
mid5
![Page 128: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/128.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 8 9arr
lhs4
rhs6
key7
5
mid5
![Page 129: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/129.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs6
key7
5
mid5
8
![Page 130: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/130.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs6
key7
5
mid5
8
![Page 131: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/131.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs6
key7
5
mid5
8
![Page 132: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/132.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs6
key7
5
mid5
8
![Page 133: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/133.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key7
5 8
![Page 134: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/134.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key7
5 8
![Page 135: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/135.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key7
5 8
![Page 136: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/136.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key7
5 8
mid4
![Page 137: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/137.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 6 9arr
lhs4
rhs4
key7
5 8
mid4
![Page 138: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/138.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs4
rhs4
key7
5 8
mid4
6
![Page 139: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/139.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs4
rhs4
key7
5 8
mid4
6
![Page 140: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/140.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs4
rhs4
key7
5 8
mid4
6
![Page 141: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/141.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs5
rhs4
key7
5 86
![Page 142: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/142.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs5
rhs4
key7
5 86
![Page 143: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/143.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs5
rhs4
key7
5 86
![Page 144: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/144.jpg)
Binary Searchprivate int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
1 2 3 9arr
lhs5
rhs4
key7
5 86
![Page 145: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/145.jpg)
Analyzing the Algorithms
![Page 146: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/146.jpg)
For Comparison
private int binarySearch(int[] arr, int key) { int lhs = 0; int rhs = arr.length – 1;
while (lhs <= rhs) { int mid = (lhs + rhs) / 2;
if (arr[mid] == key) return mid; else if (arr[mid] < key) lhs = mid + 1; else rhs = mid – 1; } return -1;}
private int linearSearch(int[] arr, int key) { for (int i = 0; i < arr.length; i++) { if (arr[i] == key) return i; }
return -1;}
![Page 147: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/147.jpg)
Analyzing Linear Search
● How many elements of the array do we have to look at to do a linear search?
● Let's suppose that there are N elements in the array.
● We may have to look at each of them once.
● Number of lookups: N.
![Page 148: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/148.jpg)
Analyzing Binary Search
● How many elements of the array do we have to look at to do a binary search?
● Let's suppose that there are N elements in the array.
● Each lookup cuts the size of the array in half.
● How many times can we cut the array in half before we run out of elements?
![Page 149: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/149.jpg)
Slicing and Dicing
● After zero lookups: N● After one lookup: N / 2● After two lookups: N / 4● After three lookups: N / 8
…● After k lookups: N / 2k
![Page 150: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/150.jpg)
Cutting in Half
● After doing k lookups, there are N / 2k elements left.
● The algorithm stops when there is just one element left.
● Solving for the number of iterations:
N / 2k = 1
N = 2k
log2 N = k
● So binary search stops after log2 N lookups.
![Page 151: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/151.jpg)
For Comparison
10
100
1000
1,000,000
1,000,000,000
log2 N3
7
10
20
30
N
Binary search can check whether a value exists in an array of one billion elements in just 30 array
accesses!
![Page 152: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/152.jpg)
A Feel for log2 N
● It is conjectured that the number of atoms in the universe is 10100.
● log2 10100 is roughly 300.
● If you (somehow) listed all the atoms in the universe in sorted order, you would need to look at 300 before you found the one you were looking for.
![Page 153: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/153.jpg)
Sorting
![Page 154: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/154.jpg)
Bubble Sort
● Until the array is sorted:● Look at each adjacent pair of elements.● If they are out of order, swap them.
![Page 155: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/155.jpg)
Should we use bubble sort?
![Page 156: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/156.jpg)
Should we use bubble sort?
Let's ask the President of the United States!
![Page 157: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/157.jpg)
“What is the most efficient way tosort a million 32-bit integers?”
![Page 158: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/158.jpg)
A Presidential Decree
● Bubble sort is the wrong way to go.
● What other algorithms could we use instead?
http://www.whitehouse.gov/sites/default/files/imagecache/admin_official_lowres/administration-official/ao_image/President_Official_Portrait_HiRes.jpg
![Page 159: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/159.jpg)
Selection Sort
● Find the smallest number and swap it to the front of the array.
● Find the second-smallest number and swap it to the second position.
● Find the third-smallest number and swap it to the third position.
● …
![Page 160: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/160.jpg)
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
private void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp;}
![Page 161: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/161.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array
![Page 162: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/162.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array
![Page 163: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/163.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
![Page 164: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/164.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
![Page 165: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/165.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0
![Page 166: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/166.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0
![Page 167: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/167.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0smallestIndex
0
![Page 168: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/168.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0smallestIndex
0
![Page 169: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/169.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
1smallestIndex
0
![Page 170: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/170.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
1smallestIndex
0
![Page 171: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/171.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
1smallestIndex
0
![Page 172: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/172.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
1smallestIndex
1
![Page 173: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/173.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
1smallestIndex
1
![Page 174: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/174.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
2smallestIndex
1
![Page 175: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/175.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
2smallestIndex
1
![Page 176: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/176.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
2smallestIndex
1
![Page 177: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/177.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
2smallestIndex
2
![Page 178: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/178.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
2smallestIndex
2
![Page 179: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/179.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
3smallestIndex
2
![Page 180: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/180.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
3smallestIndex
2
![Page 181: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/181.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
3smallestIndex
2
![Page 182: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/182.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
4smallestIndex
2
![Page 183: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/183.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
4smallestIndex
2
![Page 184: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/184.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
4smallestIndex
2
![Page 185: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/185.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
5smallestIndex
2
![Page 186: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/186.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
5smallestIndex
2
![Page 187: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/187.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
5smallestIndex
2
![Page 188: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/188.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
5smallestIndex
5
![Page 189: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/189.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
5smallestIndex
5
![Page 190: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/190.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
6smallestIndex
5
![Page 191: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/191.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
6smallestIndex
5
![Page 192: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/192.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
6smallestIndex
5
![Page 193: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/193.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
7smallestIndex
5
![Page 194: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/194.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
private int findSmallest(int[] array, int startPoint) { int smallestIndex = startPoint; for (int i = startPoint + 1; i < array.length; i++) { if (array[i] < array[smallestIndex]) smallestIndex = i; } return smallestIndex;}
array startPoint
0i
7smallestIndex
5
![Page 195: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/195.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0smallestIndex
5
![Page 196: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/196.jpg)
Selection Sort in Action
8 5 2 9 63 1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0smallestIndex
5
![Page 197: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/197.jpg)
Selection Sort in Action
5 2 9 63
1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0smallestIndex
5
8
![Page 198: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/198.jpg)
Selection Sort in Action
5 2 9 63
1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0smallestIndex
5
8
![Page 199: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/199.jpg)
Selection Sort in Action
5 2 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0smallestIndex
5
8
![Page 200: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/200.jpg)
Selection Sort in Action
5 2 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
8
![Page 201: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/201.jpg)
Selection Sort in Action
5 2 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
0
8
![Page 202: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/202.jpg)
Selection Sort in Action
5 2 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
![Page 203: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/203.jpg)
Selection Sort in Action
5 2 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
![Page 204: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/204.jpg)
Selection Sort in Action
5 2 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
![Page 205: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/205.jpg)
Selection Sort in Action
5 2 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
smallestIndex
2
![Page 206: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/206.jpg)
Selection Sort in Action
5 2 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
smallestIndex
2
![Page 207: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/207.jpg)
Selection Sort in Action
5 2
9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
smallestIndex
2
![Page 208: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/208.jpg)
Selection Sort in Action
52
9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
smallestIndex
2
![Page 209: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/209.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
smallestIndex
2
![Page 210: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/210.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
![Page 211: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/211.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
1
8
![Page 212: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/212.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
2
8
![Page 213: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/213.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
2
8
![Page 214: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/214.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
2
8
![Page 215: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/215.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
2
8
smallestIndex
3
![Page 216: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/216.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
2
8
smallestIndex
3
![Page 217: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/217.jpg)
Selection Sort in Action
5
2 9 6
3
1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
2
8
smallestIndex
3
![Page 218: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/218.jpg)
Selection Sort in Action
5
2 9 6
3
1
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
2
8
smallestIndex
3
![Page 219: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/219.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
2
8
smallestIndex
3
![Page 220: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/220.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
3
8
![Page 221: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/221.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
3
8
![Page 222: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/222.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
4
8
![Page 223: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/223.jpg)
Selection Sort in Action
52 9 631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
4
8
![Page 224: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/224.jpg)
Selection Sort in Action
52
9 6
31
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
4
8
![Page 225: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/225.jpg)
Selection Sort in Action
52
96
31
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
4
8
![Page 226: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/226.jpg)
Selection Sort in Action
52 9631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
4
8
![Page 227: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/227.jpg)
Selection Sort in Action
52 9631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
5
8
![Page 228: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/228.jpg)
Selection Sort in Action
52 9631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
5
8
![Page 229: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/229.jpg)
Selection Sort in Action
52 9631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
6
8
![Page 230: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/230.jpg)
Selection Sort in Action
52 9631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
6
8
![Page 231: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/231.jpg)
Selection Sort in Action
52 9631
private void selectionSort(int[] array) { for (int index = 0; index < array.length; index++) { int smallestIndex = findSmallest(array, index); swap(array, index, smallestIndex); }}
array index
7
8
![Page 232: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/232.jpg)
Selection Sort in Action
52 9631 8
![Page 233: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/233.jpg)
Analyzing Selection Sort
● How much work do we do for selection sort?
● To find the smallest value, we need to look at all N array elements.
● To find the second-smallest value, we need to look at N – 1 array elements.
● To find the third-smallest value, we need to look at N – 2 array elements.
● Work is N + (N – 1) + (N – 2) + … + 1.
![Page 234: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/234.jpg)
1 + 2 + … + (n – 1) + n
n
n + 1
= n(n+1) / 2
![Page 235: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/235.jpg)
An Interesting Observation
● Selection sort does roughly N2 / 2 array lookups.
● Suppose we double the number of elements in the array we want to sort.
● How much longer will it take to sort the new array?
newTime / oldTime
= ((2N)2 / 2) / (N2 / 2)
= (2N)2 / N2
= 4N2 / N2
= 4
![Page 236: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/236.jpg)
An Interesting Observation
● Selection sort does roughly N2 / 2 array lookups.
● Suppose we double the number of elements in the array we want to sort.
● How much longer will it take to sort the new array?
newTime / oldTime
= ((2N)2 / 2) / (N2 / 2)
= (2N)2 / N2
= 4N2 / N2
= 4
● So we should expect it to take about four times longer.
![Page 237: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/237.jpg)
Analyzing Selection Sort
● Work done is roughly N2 / 2.
10
100
1000
1,000,000
1,000,000,000
N2 / 250
5,000
500,000
500,000,000,000
500,000,000,000,000,000
N
![Page 238: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/238.jpg)
Analyzing Selection Sort
● Work done is roughly N2 / 2.
10
100
1000
1,000,000
1,000,000,000
N2 / 250
5,000
500,000
500,000,000,000
500,000,000,000,000,000
N
![Page 239: Searching and Sorting - Stanford University...Searching Given an array of values, determine whether some value is contained in that array. Very important: finding medical records,](https://reader033.vdocuments.net/reader033/viewer/2022060218/5f067c7c7e708231d4183b5c/html5/thumbnails/239.jpg)
A Few Other Sorting Algorithms