Académique Documents
Professionnel Documents
Culture Documents
Searching
Search an unordered array of integers for a value and return its index if
the value is found. Otherwise, return -1.
A[0] A[1] A[2]
A[3] A[4] A[5] A[6] A[7]
14 2 10 5 1 3 17 2
Algorithm:
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
mid
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
mid
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
mid
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
hi
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
hi
mid
Binary Search
Binary search. Given value and sorted array a[], find index i
such that a[i] = value, or report that no such index exists.
Invariant. Algorithm maintains a[lo] value a[hi].
13
14
25
33
43
51
53
64
72
84
93
95
96
97
10
11
12
13
14
lo
hi
mid
17
int main() {
int A[100], n, k, i, mid, low, high;
scanf(%d %d, &n, &k);
for (i=0; i<n; ++i)
scanf(%d, &A[i]);
low = 0; high = n 1; mid = (high + low)/2;
while (high >= low) {
if (A[mid] == k) {
printf(%d is found\n, k);
break;
}
if (k < A[mid]) high = mid 1;
else
low = mid + 1;
mid = (high + low)/2;
}
}
Sorting
Bubble Sort
Stage 1: Compare each element (except the last one) with
its neighbor to the right
20
Stage 1
Stage 2
Stage 3
Stage 4
(done)
21
void bubbleSort(int[] a) {
int stage, inner, temp;
for (stage = a.length - 1; stage > 0; stage--) { // counting down
for (inner = 0; inner < stage; inner++) {
// bubbling up
if (a[inner] > a[inner + 1]) { // if out of order...
temp = a[inner];
// ...then swap
a[inner] = a[inner + 1];
a[inner + 1] = temp;
}
}
}
}
22
Insertion Sort
23
Insertion Sort
24
25
A 5 7
11 13 20 22
Insert x = 15
26
A 5 7
11 13 20 22
Insert x = 15
5 7
11 13 20 15 22
27
A 5 7
11 13 20 22
Insert x = 15
5 7
11 13 20 15 22
5 7
11 13 15 20 22
28
A 5 7
11 13 20 22
Insert x = 15
5 7
11 13 20 15 22
5 7
11 13 15 20 22
5 7
11 13 15 20 22
29
A 7 5
13 11 22 20
Insert 5 in 7
5 7
13 11 22 20
Insert 13 in 5, 7
5 7
13 11 22 20
5 7
11 13 20 22
Insert 11 in 5, 7, 13
5 7
11 13 22 20
Insert 22 in 5, 7, 11, 13
5 7
11 13 22 20
30
9,
2,
4,
7,
7,
7,
7,
7,
4,
4,
2,
4,
6,
6,
6,
6,
7,
7,
7,
2,
4,
4,
4,
5,
6,
6,
6,
6,
2,
2,
2,
4,
2,
2,
2,
2,
2,
2,
2,
2,
32
1,
1,
1,
1,
1,
1,
1,
2,
5
5
5
5
5
5
5
1
Mergesort
33
Basic Idea
34
3 4 7 8 9
3 4 7 8 9
2 5 7
2 5 7
elements of A
2
3 4 7 8 9
3 4 7 8 9
2 5 7
2 5 7
2 3
2 3 4
35
3 4 7 8 9
3 4 7 8 9
2 5 7
2 5 7
2 3 4 5
2 3 4 5 7
3 4 7 8 9
3 4 7 8 9
2 5 7
2 5 7
2 3 4 5 7 7
2 3 4 5 7 7 8
36
Merge Code
3 4 7 8 9
2 5 7
2 3 4 57 7 8 9
void
merge (int A[], int B[], int C[], int m,int n)
{
int i=0,j=0,k=0;
while (i<m && j<n)
{
if (A[i] < B[j]) C[k++] = A[i++];
else C[k++] = B[j++];
}
while (i<m) C[k++] = A[i++];
while (j<n) C[k++] = B[j++];
}
37
38