239
Searching and Sorting

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,

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

“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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

A Few Other Sorting Algorithms