Searching and Sorting
Searching and Sorting
Linear Search
Binary Search.
Linear Search
The process used to find the location
of a target among a list of objects
Searching an array finds the index of first element in an array
containing that value
Linear Search
Algorithm LINEAR_SEARCH(A, Key)
for i ← 1 to n do
break
end
end
if flag == 0 then
end
Linear Search
Complexity
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.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 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.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
low high
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.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
low high
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.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
low
high
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.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 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.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
Ex. Binary search for 33.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
low
high
mid
Algorithm BINARY_SEARCH(A, Key)
// Description : Perform a binary search on array A
// Input : Sorted array A of size n and Key to be searched
// Output : Success / Failure
low ← 1
high ← n
while low < high do
mid ← (low + high) / 2
if A[mid] == key then
return mid
else if A[mid] < key then
low ← mid + 1
else
high ← mid – 1
end
end
return 0
16
17
Sorting
Bubble Sort
Stage 1: Compare each element (except the last one) with its neighbor to the right
◦ If they are out of order, swap them
◦ This puts the largest element at the very end
◦ The last element is now in the correct and final place
Stage 2: Compare each element (except the last two) with its neighbor to the right
◦ If they are out of order, swap them
◦ This puts the second largest element next to last
◦ The last two elements are now in their correct and final places
Stage 3: Compare each element (except the last three) with its neighbor to the
right
◦ Continue as above until you have no unsorted elements on the left
Example of Bubble
Sort
Pass 1 Pass 2 Pass 3 Pass 4
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
22
Code for Bubble Sort
void bubbleSort(int[] a) {
int pass, inner, temp;
for (pass = 0; pass <n-1; pass++) { // counting down
for (inner = 0; inner <n-pass-1 ; 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;
}
}
}
}
23
Complexity
Complexity Type Complexity
Best: O(n)
Time Complexity Average: O(n^2)
Worst: O(n^2)
24
Insertion Sort
25
Insertion Sort
26
Example
27
Insertion Sort
28
Insertion Sort Algorithm
void insertionSort(int arr[], int n)
{
int i, temp, j;
for (i = 1; i < n; i++)
{
temp = arr[i];
j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > temp)
} {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
}
29
Insertion Sort Complexity
30
Quick Sort
31
Quick Sort
32
Quick Sort Example
END
start
start END
start END
start END
start END
END START
33
start
34
Quick Sort Algorithm
35
Quick Sort Algorithm
partition(A,lb,ub)
{
pivot=a[lb];
start=lb;
end=ub;
while(start<end)
{
while(a[start]<=pivot)
{
start++;
}
while(a[end]>pivot)
{
end--;
}
If(start<end)
{
swap(a[start],a[end]);
}
}
swap(a[lb],a[end]);
Return end;
}
36
Quick Sort complexity
37
Worst Case (Quick sort)
Example :1 2 3 4 5 6
Mergesort
39
Basic Idea
40
DIVIDE AND CONQUER
41
MERGE SORT
ALGORITHM
42
MERGE SORT
ALGORITHM
1.MERGE_SORT(a, lb, ub)
2.
3.if lb < ub
4.set mid = (lb + ub)/2
5.MERGE_SORT(a, lb, mid)
6.MERGE_SORT(a, mid + 1, ub)
7.MERGE (a, lb, mid, ub)
8.end of if
9.
10.END MERGE_SORT
43
MERGE ALGORITHM
MERGE(a,lb,mid,ub)
{ i=lb; J=mid+1; k=lb;
While(i<=mid && j<=ub)
{ if(a[i]<=a[j])
◦ { b[k]=a[i];i++;k+++:}
◦ else
◦ { b[k]=a[j]; j++;k++;}
◦ }
}
if(i>mid)
{ while(j<=ub)
{ b[k]=a[j];j++ ;k++}
else
{ while(i<=mid)
{b[k]=a[i];i++;k++}
}
}
44
MERGE SORT
COMPLEXITY
45