Documente Academic
Documente Profesional
Documente Cultură
int nCount = 0; int nN = 10; for(int nX = 0; nX < nN; nX++) for(int nY = 0; nY <nN; nY++) nCount++; System.out.println(nCount); What would the output be if nN was 5? or 20? What effect does doubling nN have on nCount?
O(n2)
The nested for loops "are multiplied" The efficiency of this type of algorithm is O(n2)
Sorting
Sorting is the process of putting something (e.g. an array) in order There are many ways (algorithms) to do this There is no, one single best way Some ways, though, are much worse than others Computer Scientists measure the efficiency of an algorithm in Big O notation
by nested loops
Bubble Sort Selection Sort Insertion Sort Doubling the data Quadruples (4 x) the work
4
Bubble Sort
Let's say we want to sort the following numbers: 12 34 18 5 7 We'll start by comparing the first two
12 34 18 5 7
Bubble Sort
Bubble Sort
Bubble Sort
and so on. . . 12 18 34 5 7
Bubble Sort
and so on. . . 12 18 5 34 7
Bubble Sort
and so on. . . 12 18 5 34 7
10
Bubble Sort
and so on. . . 12 18 5 7 34
11
Bubble Sort
finally, after one pass through the numbers, the last number is in the correct position We'll repeat the process, but we'll stop one position sooner.
12 18 5 7 34
12
Bubble Sort
12 18 5 7 34
13
Bubble Sort
12 18 5 7 34
14
Bubble Sort
12 5 18 7 34
15
Bubble Sort
12 5 18 7 34
16
Bubble Sort
12 5 7 18 34
17
Bubble Sort
12 5 7 18 34
18
Bubble Sort
12 5 7 18 34
19
Bubble Sort
5 12 7 18 34
20
Bubble Sort
5 12 7 18 34
21
Bubble Sort
5 7 12 18 34
22
Bubble Sort
5 7 12 18 34
23
Bubble Sort
5 7 12 18 34
24
Bubble Sort
5 7 12 18 34
25
Bubble Sort
26
Coding BubbleSort
public static void bubbleSort(int[] list) { for (int outer = 0; outer < list.length - 1; outer++) for (int inner = 0; inner < list.length-outer-1; inner++)
We'll start with some nested for loops that take us through the list of numbers We want to stop one position earlier after each pass
list.length-outer-1
27
Coding BubbleSort
public static void bubbleSort(int[] list) { for (int outer = 0; outer < list.length - 1; outer++) { for (int inner = 0; inner < list.length-outer-1; inner++) { if (list[inner] > list[inner + 1]) { //swap list[inner] & list[inner+1] int temp = list[inner]; list[inner] = list[inner + 1]; list[inner + 1] = temp; } } } }
28
29
12 34 18 5 7
32
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 34 18 5 7
33
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 34 18 5 7
34
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 18 34 5 7
35
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 18 34 5 7
36
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 18 34 5 7
37
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 18 34 5 7
38
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 18 5 34 7
39
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 18 5 34 7
40
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 5 18 34 7
41
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
12 5 18 34 7
42
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 12 18 34 7
43
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 12 18 34 7
44
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 12 18 34 7
45
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 12 18 34 7
46
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 12 18 7 34
47
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 12 18 7 34
48
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 12 7 18 34
49
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 12 7 18 34
50
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 7 12 18 34
51
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 7 12 18 34
52
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 7 12 18 34
53
Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right
5 7 12 18 34
54
Gnome Sort
When the gnome gets all the way to the right, he's done!
5 7 12 18 34
55
Selection Sort
Like Bubble Sort, but with fewer swaps We'll use a flag to keep track of the position of the smallest integer Then we will do one swap at the end of each pass 12 34 18 5 7 nFlag = 0
57
Selection Sort
12 34 18 5 7 nFlag = 0
58
Selection Sort
12 34 18 5 7 nFlag = 0 3
59
Selection Sort
12 34 18 5 7 nFlag = 3
60
Selection Sort
After one pass through the numbers, we'll swap the position marked by the flag with the first number 12 34 18 5 7 nFlag = 3
61
Selection Sort
After one pass through the numbers, we'll swap the position marked by the flag with the first number 5 34 18 12 7 nFlag = 3
62
Selection Sort
The first number is now sorted, so we'll start the process again from the next position 5 34 18 12 7 nFlag = 1
63
Selection Sort
5 34 18 12 7 nFlag = 2
64
Selection Sort
5 34 18 12 7 nFlag = 3
65
Selection Sort
5 34 18 12 7 nFlag = 4
66
Selection Sort
5 7 18 12 34 nFlag = 4
67
Selection Sort
5 7 18 12 34 nFlag = 2
68
Selection Sort
5 7 18 12 34 nFlag = 3
69
Selection Sort
5 7 12 18 34 nFlag = 3
70
Selection Sort
5 7 12 18 34 nFlag = 3
71
Selection Sort
5 7 12 18 34 nFlag = 3
72
A Common Confusion
min = outer; for (int inner = outer + 1; inner < list.length; inner++) { if (list[inner] < list[min]) { min = inner; } } //swap list[outer] & list[flag] temp = list[outer]; list[outer] = list[min]; list[min] = temp;
The Flag min, stores the index of the smallest number Don't confuse min with list[min] That's like confusing the apartment number with the person who lives in that apartment
74
What is the output? How would it change if the code in red was 2?
String [] saPets = {"cat", "bird","elephant","ant","dog"}; //bubble sort the list of pets for(int nI = 0; nI < saPets.length; nI++) for(int nJ = 0; nJ < saPets.length - nI -1; nJ++) { if(saPets[nJ].compareTo(saPets[nJ+1]) > 0) // if saPets[nJ + 1] comes before // saPets[nJ] alphabetically { String sTemp = saPets[nJ]; saPets[nJ] = saPets[nJ + 1]; saPets[nJ + 1] = sTemp; } } for(int nK = 0; nK < saPets.length; nK++) System.out.println(saPets[nK]);
76
Insertion Sort
Insertion Sort starts by sorting the first part of the list The first unsorted number is inserted into the sorted part of the list 12 34 18 5 7
77
Insertion Sort
A single number by itself is sorted, so we will start with the first number as the sorted part of the list We'll store the first number after the sorted section in a temporary variable 12 __ 18 5 7 nTemp = 34
78
Insertion Sort
While the numbers in the sorted section are larger than the number in nTemp, we will move them up one place 12 __ 18 5 7 nTemp = 34
79
Insertion Sort
Then we will move the number in nTemp into the empty position, and the next number in nTemp 12 34 __ 5 7 nTemp = 18
80
Insertion Sort
12 __ 34 5 7 nTemp = 18
81
Insertion Sort
12 18 34 __ 7 nTemp = 5
82
Insertion Sort
12 18 __ 34 7 nTemp = 5
83
Insertion Sort
12 __ 18 34 7 nTemp = 5
84
Insertion Sort
__ 12 18 34 7 nTemp = 5
85
Insertion Sort
5 12 18 34 __ nTemp = 7
86
Insertion Sort
5 12 18 __ 34 nTemp = 7
87
Insertion Sort
5 12 __ 18 34 nTemp = 7
88
Insertion Sort
5 __ 12 18 34 nTemp = 7
89
Insertion Sort
5 7 12 18 34 nTemp = 7
90
Big O notation
Expression
O(1) O(log n) O(n) O(n log n) O(n2) O(2n)
Explanation
Constant Logarithmic Linear Linear-Logarithmic Quadratic Exponential
Big O notation
Expression
O(1) O(log n)
Example
Array storage/retrieval Binary Search
O(n)
O(n log n) O(n2) O(2n)
the Data increases the work more than 2x but less than 4x
94
95
96
97
Merging
Merging is combining two smaller sorted groups into one larger sorted group {1,2,3,6,9} {0,4,5,7,8} It only takes one pass (loop) through the numbers, so it's faster than nested loop sorting
99
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} { } I'll start with the first two numbers, and copy the smaller
100
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 } I'll I'll move to the next number, and copy the smaller
101
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1} an so on. . .
102
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2} an so on. . .
103
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3} an so on. . .
104
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4} an so on. . .
105
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5} an so on. . .
106
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5,6} an so on. . .
107
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5,6,7} an so on. . .
108
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5,6,7,8} we I get to the end of one group, I can just copy the rest over
109
Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5,6,7,8,9} we I get to the end of one group, I can just copy the rest over
110
First I'll rewrite my sort so that it only sorts part of the numbers, not all of them
public void selectionSort( int[] list, int first, int last) //sorts list from positions first to last //code not shown
Then, I'll write a merge function that merges the numbers from first to mid, and mid + 1 to last
void merge( int[] a, int first, int mid, int last) //code not shown
111
112
113
114
115
116
If dividing the numbers to be sorted into 2 groups saves steps, would 4 be better?
119
120
If dividing the numbers to be sorted into 4 groups saves steps, would 8 be better?
121
122
Merge Sort
12 34 18 5 7
Merge Sort works with the idea that a single number by itself is always sorted. Merge Sort recursively breaks down the array into smaller and smaller sections, until each section only has one number.
123
Merge Sort
12 34 18 5 7
12 34 18 5 7
124
Merge Sort
12
12 34 18 5 7
12 34 34 18 5 7 18 57
125
Merge Sort
12
12 34 18 5 7
12 34 34 18 5 7 18 5 57 7
126
Merge Sort
12
12 34 18 5 7
12 34 34 18 5 7 18 5 57 7
Merge Sort
12
12 34 18 5 7
12 34 34 18 5 7 18 5 5 7 57 7
12 34
128
Merge Sort
12
12 34 18 5 7
12 34 34 18 5 7 18 5 5 7 5 7 18 57 7
12 34
129
Merge Sort
12
12 34 18 5 7
12 34 34 18 5 7 18 5 5 7 5 7 18 57 7
12 34
5 7 12 18 34
130
}
132
Javabat mergeTwo
http://www.javabat.com/prob?id=AP1.merg eTwo Start with two arrays of strings, A and B, each with its elements in alphabetical order and without duplicates. Return a new array containing the first N elements from the two arrays. The result array should be in alphabetical order and without duplicates. A and B will both have a length which is N or more.
133
134
The first thing I need is a new String array of the right size String saC[]= new String[3];
135
The first thing I need is a new String array of the right size String saC[]= new String[3];
I'll also need three index variables, one for each array int nA=0,nB=0,nC=0;
136
?? , ?? , ?? } //saC
137
"a" , ?? , ?? }
138
"a" , "b" , ?? }
139
3)
and stop!
140
?? , ?? , ?? }
141
"a" , ?? , ?? }
142
"a" , "c" , ?? }
that we incremented the indexes in both arrays!
Notice
143
3)
and stop!
144
comparing Strings
145
comparing Strings
public String[] mergeTwo( String[] a, String[] b, int n) { //lots of java if(a[nA].compareTo(b[nB]) < 0)
146
comparing Strings
if(a[nA].compareTo(b[nB]) < 0)
147
comparing Strings
if(a[nA].compareTo(b[nB]) > 0)
148
comparing Strings
if(a[nA].compareTo(b[nB]) == 0)
149
Coding Merge
private void merge(int[] naArray, int nFirst, int nMid, int nLast)
nFirst is the position of the first element in the first (sorted) section of the array nMid is the position of the the last element in the first (sorted) section of the array nLast is the position of the last element in the second (sorted) section of the array
150
Coding Merge
{17,12,5,8,9,3,6,7,2,1} this part of the array, nFirst is 2 nMid is 4 nLast is 7
In
151
Coding Merge
{17,12,5,8,9,3,6,7,2,1}
We'll
start with a temporary array big enough to hold the two sorted sections
Coding Merge
{17,12,5,8,9,3,6,7,2,1}
Then, we'll compare the numbers at nFirst and nMid + 1, and copy the smaller one to naTemp
153
Coding Merge
{17,12,5,8,9,3,6,7,2,1}
In this case nMid + 1 had the smaller number We'll move to the next value after nMid + 1 after copying it's value to naTemp
154
3 5
Coding Merge
{17,12,5,8,9,3,6,7,2,1}
155
3 5 6
Coding Merge
{17,12,5,8,9,3,6,7,2,1}
156
3 5 6 7
Coding Merge
{17,12,5,8,9,3,6,7,2,1}
157
3 5 6 7 8 9
Coding Merge
{17,12,5,8,9,3,6,7,2,1}
Once we get to the end of one section, we can copy the rest into the temp array
158
159
Copying an array
int[] na1 = {1,2,3,4}; int[] na2 = na1; na2 is NOT a copy of na1 na2 is an ALIAS of na1 {1,2,3,4}
160
Copying an array
int[] na1 = {1,2,3,4}; int[] na2 = new int[na1.length];
for(int nI = 0; nI < na1.length; nI++)
{1,2,3,4} {1,2,3,4}
161
Efficiency in MergeSort
The temporary array in merge sort only needs to hold the values we are currently sorting Remember, MergeSort recursively breaks down the array into smaller and smaller sections You can significantly reduce steps by only copying the values you are working on (from nFirst to nLast) rather than the entire array na2[nI] = na1[nI];
162
Quick Sort
12 34 18 5 7
Quick Sort is a two step process: 1. A helper function partitions the numbers into two groups 2. Quick Sort is recursively called to sort the two partitions
163
Quick Sort
Pivot: 12
12 34 18 5 7
164
Quick Sort
Pivot: 12
12 34 18 5 7
5 7
34 18
165
Quick Sort
Pivot: 12
12 34 18 5 7
5 7
34 18
166
Quick Sort
Pivot: 12
12 34 18 5 7
5 7
12
34 18
167
Quick Sort
Pivot: 5
12 34 18 5 7
5 7
12
34 18
168
Quick Sort
Pivot: 5
12 34 18 5 7
5 7
12
34 18
12
169
Quick Sort
Pivot: 5
12 34 18 5 7
5 7
12
34 18
12
170
Quick Sort
Pivot: 34
12 34 18 5 7
34 18
12
34 18
12
18
171
Quick Sort
Pivot: 34
12 34 18 5 7
5 7
12
34 18
12
18
34
172
{ int nPivot; if ( nHigh > nLow ) { nPivot = partition( naA, nLow, nHigh ); quicksort(naA, nLow, nPivot-1 ); quicksort(naA, nPivot+1, nHigh ); } }
173
174
What would be the Big O expression for "guess and check" sort?
In "Guess and Check" sort, we randomly arrange an array of numbers, and check to see if it's sorted If it's not sorted, we randomly arrange it again
175
What would be the Big O expression for "guess and check" sort?
Let's say the array had two numbers 13 & 17: how many ways could they be arranged? Let's increase the size of the data by one, adding the number 7. How many ways could they be arranged now?
13, 17 17, 13
176
What would be the Big O expression for "guess and check" sort?
Increasing the size of the data by just one more than doubles the number of steps! This is an example of O(n!)
177
178
179
Bucket Sort
5 9 3 7 2 1
10
180
Bucket Sort
5 9 3 7 2 1
10
181
Bucket Sort
5 9 3 7 2 1
5 5
10
182
Bucket Sort
5 9 3 7 2 1
5 5
10
183
Bucket Sort
5 9 3 7 2 1
5 5
4
9 9
10
184
Bucket Sort
5 9 3 7 2 1
5 5
4
9 9
10
185
Bucket Sort
5 9 3 7 2 1
3 3 5 5
4
9 9
10
186
Bucket Sort
5 9 3 7 2 1
3 3 5 5
4
9 9
10
187
Bucket Sort
5 9 3 7 2 1
3 3 5 5
2
7 7
4
9 9
10
188
Bucket Sort
5 9 3 7 2 1
3 3 5 5
2
7 7
4
9 9
10
189
Bucket Sort
5 9 3 7 2 1
2 2 7 7 3 3 5 5
4
9 9
10
190
Bucket Sort
5 9 3 7 2 1
2 2 7 7 3 3 5 5
4
9 9
10
191
Bucket Sort
5 9 3 7 2 1
1 1 2 2 7 7 3 3 5 5
4
9 9
10
192
Bucket Sort
5 9 3 7 2 1
1 1 2 2 7 7 3 3 5 5
4
9 9
10
Bucket Sort
I only need to make 2 passes through the numbers, once to put them in the buckets and once to take them out For 6 numbers, that's 12 steps How many steps for 12 numbers? What's the Big-O Notation for Bucket Sort? Why isn't Bucket Sort used more commonly than Quick Sort?
194
195
Linear Search
Let's say you had an array of 100 numbers in no particular order and you wanted to know if the number 17 was in the array You would have to look at the first number, then the second, and so on until you found 17 or reached the end of the array That's linear search, an O(n) algorithm
196
Binary Search
I'm thinking of a number between 1 and 100 I want you to try and guess it I'll tell you if your guess is too high, too low or if you guessed correctly What would be the first number you would guess? If your answer is 50, you know how Binary Search works
197
Binary Search
If someone correctly uses the Binary Search strategy, how many guesses will they need at most to guess the number?
198
Binary Search
Number of Guesses 0 1 2 3 4 5 6 7 Possible Numbers 100 50 25 13 7 4 2 1
199
Binary Search
Anyone should be able to guess the number in at most 7 guesses How many guesses would we need if we doubled the possibilities to 200 (a number between 1 and 200)? 8, just one more guess Binary Search is an O(log n) algorithm
200
201
Too High
202
203
Too Low
204
205
Too High
206
And so on. . . . Until either we guess the number or there are no more numbers to guess
207
208
{0,3,4,7,11,13,15,17,21,33}
nLow 0 nGuess 4 nHigh 9
209
{0,3,4,7,11,13,15,17,21,33}
nLow 0 nGuess 1 nHigh 3
210
{0,3,4,7,11,13,15,17,21,33}
nLow 2 nGuess 2 nHigh 3
211
{0,3,4,7,11,13,15,17,21,33}
nLow 3 nGuess 3 nHigh 3
212
Radix Sort
"Bucket Sort" is very efficient in terms of stepsO(n) But it is very inefficient in terms of memory And it cannot handle duplicate values By changing the "buckets" to linked lists, memory can be handled efficiently as well And any number of items can be stored in a linked list, not just one like a "bucket"
213
radix sort
0 1 2 3 4 5 6 7 8 9
214
radix
0 sort 1 2 3 4 The first pass will 5 arrange 6 the numbers 7 in order of 8 the ones 9 digit
215
radix sort
0 1 2 3 4 5 6 7 8 9
216
radix sort
0 1 2 3 4 5 6 7 8 9
217
radix sort
0 1 2 3 4 5 6 7 8 9
218
radix sort
0 1 2 3 4 5 6 7 8 9
219
radix sort
0 1 2 3 4 5 6 7 8 9
220
radix sort
Now, we'll repeat the process using the tens digit
0 1 2 3 4 5 6 7 8 9
221
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
222
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
223
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
224
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
225
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
226
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 Once again, this time using the hundreds digit
227
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
228
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
229
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
230
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
231
radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
232
radix sort
After three passes, one for each digit, the numbers are in order
0 1 2 3 4 5 6 7 8 9
233
Radix Sort
Three passes, one for each digit, sorted the 5 numbers Basically, that's 15 steps How many steps for 10 numbers with a maximum of 3 digits?
234
Radix Sort
O(n) Faster than QuickSort, MergeSort and other O(n log n) algorithms Less efficient use of memory than other sorting algorithms, though
235
A hash table is another variation of the bucket sort: Elements are arranged, but not sorted or ordered in a way you would expect Unlike radix sort, we only make one pass through the data Not really a sorting algorithm, but a data structure that can be used to store and retrieve items.
236
hash tables
Let's say I have five numbers, each of which is between 0 and 400: 7 266 399 12 125 I'll make an array of 5 linked liststhe hash table I'll take the number % 5 and use that to decide which of the 5 lists the number will be assigned I'm using % 5 because there are 5 numbers to be sortedto 5 lists In this example, % 5 is the "hash function"
237
hash tables
0
1 2
3
4
238
hash tables
0
1 2
3
4
239
hash tables
0
1 2
3
4
240
hash tables
0
1 2
3
4
241
hash tables
0
1 2
3
4
242
hash tables
12 and 7 are placed in the same location by the hash function. This is called a collision. 12 is inserted at the head of the linked list.
0
1 2
3
4
243
hash tables
0
1 2
3
4
244
Once the table is complete, it's quick and easy to retrieve an item
Let's say I'm searching for the value 532. I do 532%5 = 2. That means that 532 would be at position 2 if it is in the hash table. We go to the the linked list in position 2 and do a linear search for the value 532.
0
1 2
3
4
245
Once the table is complete, it's quick and easy to retrieve an item
Let's say I'm searching for the value 532. I do 532%5 = 2. That means that 532 would be at position 2 if it is in the hash table. We go to the the linked list in position 2 and do a linear search for the value 532.
0
1 2
3
4
246
hash tables
In this example, one of the five spots is empty. % of null pointers = 20% The average length of the (non-empty) linked lists are: (1 + 1 + 2 + 1)/4 = 1.25 Ideally, we'd like to have 0 % null pointers and an average length of 1. We could write a different hash function and see if it makes an improvement
0
1 2
3
4
247
0
1 2
3
4
248
3
4
249
3
4
250
hash tables
If there are no collisions, putting a single item in the Hash table is O(1), putting all the items in the Hash Table is O(n) If there are no collisions, searching for a single item is O(1) Just use the "hash function" to find where the number would be stored
251
hashCode()
You don't have to think of your own hashing method Every class inherits hashCode() from Object Uses "Some Formula" we don't know or care about Every Object is associated with an integer value called its hash code equal objects have equal hash codes
252