Search Sort
Search Sort
Searching
1
03/09/2020
Linear Search
Basic Concept
• Basic idea:
– Start at the beginning of the array.
– Inspect elements one by one to see if it matches the key.
• Time complexity:
– A measure of how long an algorithm takes to run.
– If there are n elements in the array:
• Best case: match found in first element (1 search operation)
• Worst case: no match found, or match found in the last element
(n search operations)
• Average case: (n + 1) / 2 search operations
2
03/09/2020
#include <stdio.h>
int main()
{
int x[]={12,-3,78,67,6,50,19,10}, val;
printf (”\nEnter number to search: ”);
scanf (”%d”, &val);
printf (”\nValue returned: %d \n”, linear_search (x,8,val);
}
5
3
03/09/2020
Contd.
Returns -1
Binary Search
4
03/09/2020
Basic Concept
• What we want?
– Find split between values larger and smaller than key:
0 n-1
x: <=key >key
L R
10
5
03/09/2020
Iterative Version
#include <stdio.h>
while (L <= R) {
mid = (L + R) / 2;
if (a[mid] < key) L = mid + 1;
else if (a[mid] > key) R = mid -1;
else return mid; /* FOUND AT INDEX mid */
}
11
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;
12
6
03/09/2020
Recursive Version
#include <stdio.h>
13
int main()
{
int x[]={10,20,30,40,50,60,70,80}, val;
14
7
03/09/2020
15
Time Complexity
16
8
03/09/2020
Sorting
17
• Given an array
x[0], x[1], ... , x[size-1]
• reorder entries so that
x[0] ≤ x[1] ≤ ... ≤ x[size-1]
18
9
03/09/2020
Example
• Original list:
10, 30, 20, 80, 70, 10, 60, 40, 70
19
Sorting Problem
• What we want ?
– Sort the given data sorted in the specified order
0 size-1
x: Unsorted list
Sorted list
20
10
03/09/2020
Selection Sort
21
How it works?
• General situation :
0 k size-1
x: smallest elements, sorted remainder, unsorted
• Steps :
• Find smallest element, mval, in x[k..size-1]
• Swap smallest element with x[k], then increase k by 1
0 k mval size-1
swap
22
11
03/09/2020
PASS 1:
10 5 17 11 -3 12 Find the minimum
10 5 17 11 -3 12 Exchange with 0th
-3 5 17 11 10 12 element
PASS 2:
-3 5 17 11 10 12 Find the minumum
-3 5 17 11 10 12 Exchange with 1st
-3 5 17 11 10 12 element
PASS 3:
-3 5 17 11 10 12 Find the minumum
-3 5 17 11 10 12 Exchange with 2nd
-3 5 10 11 17 12 element
23
PASS 4:
-3 5 10 11 17 12 Find the minumum
-3 5 10 11 17 12 Exchange with 3rd
-3 5 10 11 17 12 element
PASS 5:
-3 5 10 11 17 12 Find the minumum
-3 5 10 11 17 12 Exchange with 4th
-3 5 10 11 12 17 element
24
12
03/09/2020
Subproblem
pos = k;
for (j=k+1; j<size; j++)
if (x[j] < x[pos])
pos = j;
return pos;
}
25
26
13
03/09/2020
int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
-45 89 -65 87 0 3 -23 19 56 21 76 -50
sel_sort(x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
}
27
Analysis
Of the order of n2
28
14
03/09/2020
Insertion Sort
29
Basic Idea
30
15
03/09/2020
PASS 1:
10 5 17 11 -3 12 item = 5
? 10 17 11 -3 12 compare 5 and 10
5 10 17 11 -3 12 insert 5
PASS 2:
5 10 17 11 -3 12 item = 17
5 10 17 11 -3 12 compare 17 and 10
PASS 3:
5 10 17 11 -3 12 item = 11
5 10 ? 17 -3 12 compare 11 and 17
5 10 ? 17 -3 12 compare 11 and 10
5 10 11 17 -3 12 insert 11
31
PASS 4:
5 10 11 17 -3 12 item = -3
5 10 11 ? 17 12 compare -3 and 17
5 10 ? 11 17 12 compare -3 and 11
5 ? 10 11 17 12 compare -3 and 10
? 5 10 11 17 12 compare -3 and 5
-3 5 10 11 17 12 insert -3
PASS 5:
-3 5 10 11 17 12 item = 12
-3 5 10 11 ? 17 compare 12 and 17
-3 5 10 11 ? 17 compare 12 and 11
-3 5 10 11 12 17 insert 12
32
16
03/09/2020
Insertion Sort
void insert_sort (int list[], int size)
{
int i,j,item;
int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
-45 89 -65 87 0 3 -23 19 56 21 76 -50
insert_sort (x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
}
34
17
03/09/2020
Time Complexity
– Best case?
1 + 1 + …… to (n-1) terms = (n-1)
35
Bubble Sort
36
18
03/09/2020
How it works?
37
PASS 1:
10 5 17 11 -3 12
5 10 17 11 -3 12
5 10 17 11 -3 12
5 10 11 17 -3 12
5 10 11 -3 17 12
5 10 11 -3 12 17
PASS 2:
5 10 11 -3 12 17
5 10 11 -3 12 17
5 10 11 -3 12 17
5 10 -3 11 12 17
5 10 -3 11 12 17
38
19
03/09/2020
PASS 3:
5 10 -3 11 12 17
5 10 -3 11 12 17
5 -3 10 11 12 17
5 -3 10 11 12 17
PASS 4:
5 -3 10 11 12 17
-3 5 10 11 12 17
-3 5 10 11 12 17
PASS 5:
-3 5 10 11 12 17 Sorted
-3 5 10 11 12 17
39
40
20
03/09/2020
Bubble Sort
void swap(int *x, int *y) void bubble_sort
{ (int x[], int n)
int tmp = *x; {
*x = *y; int i,j;
*y = tmp;
} for (i=n-1; i>0; i--)
for (j=0; j<i; j++)
if (x[j] > x[j+1])
swap(&x[j],&x[j+1]);
}
41
int main()
{
int x[ ]={-45,89,-65,87,0,3,-23,19,56,21,76,-50};
int i;
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
-45 89 -65 87 0 3 -23 19 56 21 76 -50
bubble_sort (x,12); -65 -50 -45 -23 0 3 19 21 56 76 87 89
for(i=0;i<12;i++)
printf("%d ",x[i]);
printf("\n");
}
42
21
03/09/2020
Time Complexity
• Number of comparisons :
– Worst case?
1 + 2 + 3 + …… + (n-1) = n(n-1)/2
– Best case?
• Same
43
44
22
03/09/2020
45
46
23
03/09/2020
Quick Sort
48
24
03/09/2020
How it works?
49
Partitioning
0 size-1
x:
pivot
Perform Perform
partitioning partitioning
50
25
03/09/2020
52
26
03/09/2020
partition(x,0,num-1);
printf("Sorted list: ");
print (x,0,num-1);
}
54
45 -56 78 90 -3 -6 123 0 -3 45 69 68
45 -56 78 90 -3 -6 123 0 -3 45 69 68
-6 -56 -3 0 -3 45 123 90 78 45 69 68
-56 -6 -3 0 -3 68 90 78 45 69 123
-3 0 -3 45 68 78 90 69
-3 0 69 78 90
55
27
03/09/2020
Time Complexity
• Worst case:
n2 ==> list is already sorted
• Average case:
n log2n
56
Merge Sort
57
28
03/09/2020
Merge Sort
Input Array
Part-I Part-II
Merge Split
Sorted Arrays
58
59
29
03/09/2020
Example
• Pass 4 :: Merge the two sorted subarrays to get the final list
– (11, 20, 22, 30, 33, 40, 44, 50, 55, 60, 66, 77, 80, 88)
60
if (n>1) {
k = n/2;
m = n-k;
b = (int *) malloc(k*sizeof(int));
c = (int *) malloc(m*sizeof(int));
61
30
03/09/2020
void merge (int *a, int *b, int *c, int m, int n)
{
int i, j, k, p;
i = j = k = 0;
do {
if (a[i] < b[j]) {
c[k]=a[i]; i++;
}
else {
c[k]=b[j]; j++;
}
k++;
} while ((i<m) && (j<n));
if (i == m) {
for (p=j; p<n; p++) { c[k]=b[p]; k++; }
}
else {
for (p=i; p<m; p++) { c[k]=a[p]; k++; }
}
}
62
main()
{
int i, num;
int a[ ] = {-56,23,43,-5,-3,0,123,-35,87,56,75,80};
num = 12;
63
31
03/09/2020
Time Complexity
• Best/Worst/Average case:
n log2n
• Drawback:
– Needs double amount of space for storage.
– For sorting n elements, requires another array of size
n to carry out merge.
64
65
32
03/09/2020
Algorithm Analysis
67
33
03/09/2020
Analysis of Algorithms
• Observation :
– The larger amount of input data an algorithm has, the
larger amount of resource it requires.
– Complexities are functions of the amount of input
data (input size).
68
69
34
03/09/2020
70
3500
3000
2500
2000
Home
Desktop
1500
1000
500
0
125 250 500 1000 2000
71
35
03/09/2020
Contd.
• Home Computer :
f1(n) = 0.0007772 n2 + 0.00305 n + 0.001
• Desktop Computer :
f2(n) = 0.0001724 n2 + 0.00040 n + 0.100
72
Complexity Classes
73
36
03/09/2020
n 13
2 .1 ms .1 s 100 s 3y 3x10 c inf inf
74
75
37
03/09/2020
• Definition:
f(n) = O(g(n)) if there exists positive constants c
and n0 such that f(n) c.g(n) when n n0.
76
Examples
• f(n) = 2n2+4n+5 is O(n2).
– One possibility: c=11, and no=1.
• f(n) = 2n2+4n+5 is also O(n3),O(n4),etc.
– One possibility: c=11, and no=1.
• f(n) = n(n-1)/2 is O(n2).
– One possibility: c=1/2, and no=1.
• f(n) = 5n4+log2n is O(n4).
– One possibility: c=6, and no=1.
• f(n) = 75 is O(1).
– One possibility: c=75, and no=1.
77
38
03/09/2020
78
Observations
79
39