Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

Search Algorithms

Input:

A set of n integers {a0, a1, . . . , a n1}, and an


integer key.

Output:

If key = ai for 0 <= i <= n 1, then the function


prints Key is found at i; otherwise prints Key is
not found.
1
How to Find a Value in an
Array?
-23 97 18 21 5 -86 64 0 -37

Suppose you have an array full of data, and you want


to find a
particular value. How will you find that value?

2
Linear Search
-23 97 18 21 5 -86 64 0 -37

Linear search means looking at each element of the


array, in turn, until you find the target value.

3
Linear Search Example #0
-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.

4
Linear Search Example #1
-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.

5
Linear Search Example #2
-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.

6
Linear Search Example #3
-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.

7
Linear Search Example #4
-23 97 18 21 5 -86 64 0 -37

element

Searching for -86.

8
Linear Search Example #5
-23 97 18 21 5 -86 64 0 -37

element

Searching for -86: found!

9
Linear Search Code
int LinearSearch(int a[], int n, int key)
{
int i;
for(i = 0; i < n; i++) {
if(a[i] = = key) {
return i;
}
}
return -1;
}

10
Linear Search: Worst Case
How long will our search take?
In the worst case, the key value is in the last element
of the array.
So the search takes an amount of time proportional
to the length of the array.
Computer scientists denote this O(n) .

11
Binary Search
1. Initially, the search region is the whole array.
2. Look at the data value in the middle of the
search region.
3. If youve found your key, stop.
4. If key is less than middle data value, the new
search region is the lower half of the data.
5. If key is greater than middle data value, the new
search region is the higher half of the data.
6. Continue from Step 2.

12
Binary Search Example
-86 -37 -23 0 5 18 21 64 97

low middle high

Searching for 18.

13
Binary Search Example
-86 -37 -23 0 5 18 21 64 97

low high
middle

Searching for 18.

14
Binary Search Example
-86 -37 -23 0 5 18 21 64 97

low
high
middle

Searching for 18: found!

15
Binary Search Code
int BinarySearch (int a[], int n, int key)
{
int low, high, mid, i;
low = 0; high = n - 1;
while(low <= high) {
mid = (low + high) / 2;
if(a[mid] = = key)
return mid;
else if(a[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
return -1;
} 16
Sorting
Sorting takes an unordered collection and makes
it an ordered one.

1 2 3 4 5 6
77 42 35 12 101 5

1 2 3 4 5 6
5 12 35 42 77 101
Bubble Sort
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping

0 1 2 3 4 5
77 42 35 12 101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping

0 1 2 3 4 5
42Swap42
77 77 35 12 101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping

0 1 2 3 4 5
42 35Swap35
77 77 12 101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping

0 1 2 3 4 5
42 35 12Swap12
77 77 101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping

0 1 2 3 4 5
42 35 12 77 101 5
No need to swap
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping

1 2 3 4 5 6
42 35 12 77 5 Swap101
101 5
"Bubbling Up" the Largest
Element
Traverse a collection of elements
Move from the front to the end
Bubble the largest value to the end using pair-wise
comparisons and swapping

0 1 2 3 4 5
42 35 12 77 5 101

Largest value correctly placed


Bubbling All the Elements
0 1 2 3 4 5
42 35 12 77 5 101

35 12 42 5 77 101
N-1

12 35 5 42 77 101

12 5 35 42 77 101

5 12 35 42 77 101
Exchanging Elements: SWAP
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

26
Bubble Sort
void BubbleSort(int a[], int n)
{
int i, j, flag = 0;
for(i = 0; flag = = 0 && i < n-1; i++) {
flag = 1;
for(j = 0; j <= n-i-2; j++) {
if(a[j] > a[j+1]) {
swap(&a[j], &a[j+1]);
flag = 0;
}
}
}
} 27
Selection Sort
1. i = 0;
2. Find the index min of the minimum element
between a[i] to a[n-1]
3. Swap a[i] with a[min]
4. i = i +1
5. Goto Step 2 if i != n-1.
0 1 2 3 4 5
77 42 35 12 101 5

28
Selection Sort
0 1 2 3 4 5
77 42 35 12 101 5 Min=1

77 42 35 12 101 5 Min=2

77 42 35 12 101 5 Min=3

77 42 35 12 101 5 Min=3
29

77 42 35 12 101 5 Min=5

5 42 35 12 101 77 Min=3
29
Selection Sort
void SelectionSort(int a[], int n)
{
int i, j,min;
for(i = 0; i < n-1; i++) {
min = i;
for(j = i+1; j < n; j++) {
if(a[j] < a[min])
min = j;
}
swap(&a[i], &a[min]);
}
} 30
Insertion Sort
1. i = 1;
2. Insert the i-th element in the sorted array a[0] to
a[i-1] so that a[0] to a[i] is sorted.
3. i = i +1
4. Goto Step 2 if i != n.

31
Last Iteration of Insertion Sort
0 1 2 3 4 5
5 12 35 42 77 6 Key = 6

5 12 35 42 77 77

5 12 35 42 42 77

5 12 35 35 42 77
32

5 12 12 35 42 77

5 6 12 35 42 77
32
Insertion Sort
void InsertionSort(int a[], int n)
{
int i, j, key;
for(i = 1; i < n; i++) {
key = a[i];
for(j = i-1;j > -1 && a[j] > key; j--)
a[j+1] = a[j];
a[j+1] = key;
}
}
33
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C 0 0 0 0 0 0 0 0 0 0 0
k 34
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 0 0 0 0 0 0 0 0 0 0
k 35
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 0 0 0 0 0 0 0 0 0
k 36
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 0 0 0 0 0 0 0 0
k 37
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 0 0 0 0 0 0 0
k 38
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 0 0 0 0 0 0
k 39
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 0 0 0 0 0
k 40
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 0 0 0 0
k 41
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 92 0 0 0
k 42
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 92 101 0 0
k 43
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 92 101 101 0
k 44
How to merge two sorted array?
1 2 3 4 5 6
A 5 26 35 92 101 500
i
1 2 3 4 5
B -9 -1 30 60 101
j

1 2 3 4 5 6 7 8 9 10 11
C -9 -1 5 26 30 35 60 92 101 101 500
k
45
Merge
void merge(int a[], int low, int mid, int high)
{
int i = low, j = mid+1, count = 0;
int b[high-low+1];
while(count < high-low+1 && i <= mid && j <= high)
{
if(a[i] <= a[j])
b[count++] = a[i++];
else
b[count++] = a[j++];
} 46
Merge

while(count < high-low+1) {


if(j != high+1)
b[count++] = a[j++];
if(i != mid+1)
b[count++] = a[i++];
}
for(i = low, j = 0; i <= high; i++, j++)
a[i] = b[j];
}
47
Merge Sort : Divide
1 2 3 4 5 6 7 8
A 92 35 26 101 5 500 101 92
1 2 3 4 5 6 7 8
A 92 35 26 101 5 500 101 92

1 2 3 4 5 6 7 8
A 92 35 26 101 5 500 101 92

1 2 3 4 5 6 7 8
A 92 35 26 101 5 500 101 92
48
Merge Sort : Conquer
1 2 3 4 5 6 7 8
A 5 26 35 92 92 101 101 500
1 2 3 4 5 6 7 8
A 26 35 92 101 5 92 101 500

1 2 3 4 5 6 7 8
A 35 92 26 101 5 500 92 101

1 2 3 4 5 6 7 8
A 92 35 26 101 5 500 101 92
49
Merge Sort
void mergesort(int a[], int low, int high)
{
if(low < high) {
int mid = (high+low)/2;
mergesort(a, low, mid);
mergesort(a, mid+1, high);
merge(a, low, mid, high);
}
}
50
Quick Sort
0 1 2 3 4 5 6 7
A 2 8 7 1 3 5 6 4

GOAL: Place A[n-1] in A[i] such that all


elements that are less than A[n-1] are placed on
the left side of A[i] and all elements that are
greater than A[n-1] are placed on the right side of
A[i].

51
i=-1 j=0 1 2 3 4 5 6 7
A 2 8 7 1 3 5 6 4
i=0 j=1 2 3 4 5 6 7
A 2 8 7 1 3 5 6 4
i=0 1 j=2 3 4 5 6 7
A 2 8 7 1 3 5 6 4
0 i=1 2 j=3 4 5 6 7
A 2 1 7 8 3 5 6 4
0 1 i=2 3 j=4 5 6 7
A 2 1 3 8 7 5 6 4
52
0 1 i=2 3 j=4 5 6 7
A 2 1 3 8 7 5 6 4
0 1 i=2 3 4 j=5 6 7
A 2 1 3 8 7 5 6 4

0 1 i=2 3 4 5 j=6 7
A 2 1 3 8 7 5 6 4

0 1 i=2 3 4 5 6 j=7
A 2 1 3 4 7 5 6 8
Partition
int partition(int a[], int low, int high)
{
int i, j;
i = low-1;
for(j = low; j < high; j++) {
if(a[j] < a[high]) {
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1], &a[high]);
return i+1;
} 54
Quick Sort
void quicksort(int a[], int low, int high)
{
if(low < high) {
int pivot = partition(a, low, high);
quicksort(a, low, pivot-1);
quicksort(a, pivot+1, high);
}
}

55

You might also like