SlideShare a Scribd company logo
PPS
Unit – 5
Basic Algorithms
5.1 Searching (Linear Search, Binary Search Etc.)
Sorting and Searching are fundamental operations in computer science. Sorting refers to
the operation of arranging data in some given order. Searching refers to the operation
of searching the particular record from the existing information.
Consider a database of banking system where information of all customers such as
name, contact number, address, account number is stored. If a manager wants to search
for a record of a particular customer, he has to look for that record from among all
records that has been stored in a database. This process of looking up for a particular
record in a database is referred as searching.
If records in a banking database are not ordered properly it will be very difficult for a
manager to search for a specific record. On the contrary if all record are arranged in
order according to some criteria like names in alphabetical order or account numbers in
ascending order, searching becomes easy and fast. The process of ordering the records
in a data base is called sorting. Thus for efficient searching sorting is necessary.
Linear Search
Given a collection of objects, the goal of search is to find a particular object in this
collection. The objects have key values on which search is performed and data values
which correspond to the information one wishes to retrieve once an object is found. For
example, a telephone book is a collection of names on which one searches and
telephone numbers which correspond to the data being sought. The collection of
objects is often stored in a list or an array. Given a collection of objects in an array A[1..
n], the ith element A[i] corresponds to the key value of the ith object in the collection.
The input to a search algorithm is an array of objects A, the number of objects n, and the
key value being sought x.
Thus Algorithm for Linear Search is
1. start at one end of the list
2. if the current element is not equal to the search target, move to the next element,
3. stop when a match is found or the opposite end of the list is reached.
Function for Linear search is
find(values, target)
{
for(i = 0; i<values.length; i++)
{
if (values[i] == target)
{ returni; }
}
return –1;
}
If element is found this function returns index of that element otherwise it returns –1.
Example : we have an array of integers, like the following:
Elements  9 6 7 12 1 5
Index 0 1 2 3 4 5
Let’s search for the number 7. According to function for linear search index for element
7 should be returned. We start at the beginning and check the first element in the array.
9 6 7 12 1 5
First element is 9.Match is not found and hence we move to next element i.e 6.
9 6 7 12 1 5
Again match is not found so move to next element i.e.7.
9 6 7 12 1 5
0 1 2 3 4 5
We found the match at the index position 2. Hence output is 2.
If we try to find element 10 in above array, it will return –1,as 10 is not present in the
array.
Complexity Analysis of Linear Search:
Let us assume element x is to be searched in array A of size n.
An algorithm can be analyzed using following three cases.
1. Worst case
2. Average case
3. Best case
1. Worst case :In Linear search worst case happens when element to be searched is not
present. When x is not present, the linear search function compares it with all the
elements of A one by one. The size of A is n and hence x is to be compared with all n
elements. Thus, the worst case time complexity of linear search would be O(n).
2. Average case : In average case analysis, we take all possible inputs and calculate
computing time for all of the inputs. Sum all the calculated values and divide the sum by
total number of inputs. Following is the value of average case time complexity.
3. Best Case : In linear search , the best case occurs when x is present at the first
location. So time complexity in the best case would be O(1).
P1 : Program for Linear Search
#include<stdio.h>
#include<conio.h>
int search(int [],int, int);
void main()
{
int a[20],k,i,n,m;
clrscr();
printf(“nn ENTER THE SIZE OF ARRAY”);
scanf(“%d”,&n);
printf(“nnENTER THE ARRAY ELEMENTSnn”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf(“nnENTER THE ELEMENT FOR SEARCHn”);
scanf(“%d”,&k);
m=search(a,n,k);
if(m==–1)
{
printf(“nnELEMENT IS NOT FOUND IN LIST”);
}
else
{
printf(“nnELEMENT IS FOUND AT LOCATION %d”,m);
}
}
getch();
}
int search(int a[],intn,int k)
{
inti;
for(i=0;i<n;i++)
{
if(a[i]==k)
{
return i;
}
}
if(i==n)
{
return –1;
}
}
Merits :
1. Simple and easy to implement
2. It works well even on unsorted arrays.
3. It is memory efficient as it does not require copying and partitioning of the array
being searched.
Demerits :
1. Suitable only for small number of values.
2. It is slow as compared to binary search.
Binary Search
Binary Search requires sorted array.
Steps to perform binary search on sorted array A of size n are as follows.
1. Initialize lower and upper limit of array to be searched Low=0,High= n–1.
2. Find the middle position
Mid=(Low + High)/2
3. Compare the input key value with the key value of the middle element of the array. It
has 3 possibilities.
Case 1 : If keys match then return mid and print element found.
if(key==A[mid])
return mid
Case 2 : If key is less than middle element then element is present in left half of array.
Update High and goto step 2.
if(key<A[mid])
High= Mid–1
goto step 2
Case 3 : If key is greater than middle element then element is present in right half of
array. Update Low and goto step 2.
if(key>A[mid])
Low= Mid+1
goto step 2
Case 4:if(Low>High)
Print element not found.
Iterative algorithm for Binary Search
intBinarySearch( int A[],int x)
{
intmid,n;
int Low = 0;
int High = n–1;
while ( Low <= High )
{
mid = (Low + High) / 2;
if ( A[mid] == x )
return mid;
if ( A[mid] > x )
High = mid – 1;
else Low = mid + 1;
}
return –1;
}
Example 1 :Let array A= {–1, 5, 6, 18, 19, 25, 46, 78, 102, 114}. We want to find element
‘6’ using binary search.
–1 5 6 18 19 25 46 78 102 114
0 1 2 3 4 5 6 7 8 9
Solution:
Step 1 : Low=0, High= 9
Step 2 :Mid=(Low + High)/2
Mid = (0+9)/2 =4.5 i.e 4. It means middle element is located at position 4. So A[Mid] is
19.Thus
Low=0
Mid=4
High=9
Low Mid High
–1 5 6 18 19 25 46 78 10
2
114
0 1 2 3 4 5 6 7 8 9
Step 3 :Compare 6 with 19. It satisfies case 2 of step 3. so High= 4–1=3 and element is
present in left half of array. We can ignore right half of array.
Repeat step 2. So mid= (0+3)/2 = 1.5 i.e 1. mid is at position 1. Element 5 is present at
position 1.
Low = 0
Mid = 1
High = 3
Low Mid High
–1 5 6 18 19
0 1 2 3 4
Again compare 5 with 6. It satisfies case 3 of step 3. so
Low = 1+1=2.
Repeat step 2. Mid= (2+ 3)/2= 2.5 i.e 2 .Mid is at position 2. Element 6 is at position 2.
Low=2
Mid=2
High=3
Low,
Mid
High
–1 5 6 18 19
0 1 2 3 4
Compare 6 with 6.
Match is found. Return the position and print element found.
Example 2 : Find 103 in {–1, 5, 6, 18, 19, 25, 46, 78, 102, 114} using Binary Search.
Low Mid High
–1 5 6 18 19 25 46 78 10
2
114
0 1 2 3 4 5 6 7 8 9
Step 1 : Low=0
High=9
Step 2 :Mid=(0+9)/2=4
So middle element is 19.
Step 3 : Compare(103,19)
103>19 hence element is located at right half of array and we can ignore left half of
array. Update Low and recalculate mid.
Low = 4+1=5
High = 9
Mid = (5+9)/2=7
Low Mid High
25 46 78 102 114
5 6 7 8 9
Middle element is 78. Compare(103,78)
103>78. Recalculate Low and Mid
Low = 7+1=8
High = 9
Mid = (8+9)/2=8
Low, Mid High
102 114
8 9
Middle element is 102. Compare(103,102)
103>102. Recalculate Low and Mid.
Low = 8+1=9
High = 9
Here Low=High and match is not found hence we conclude that element is not present
in array.
Example 3 : Find 44 in A={5,12,17,23,38,44,77,84,90} using binary search.
Step 1: Low=0
High=8
Mid=(0+8)/2=4
Low Mid High
5 12 17 23 38 44 77 84 90
0 1 2 3 4 5 6 7 8
Middle element is 38. Compare(44,38)
44>38.Thus element is located at right half of array and we can ignore left half of array.
Recalculate Low and Mid.
Low = (4+1)= 5
High = 8
Mid = (5+8)/2=6.5 i.e 6
Low Mid high
44 77 84 90
0 1 2 3
Middle element is 77. Compare(44,77)
44<77. Recalculate High and Mid.
Low=5
High = 6–1=5
Mid = (5+5)/2=5
Low, Mid,
high
44 77 84 90
0 1 2 3
Middle element is 44. Compare(44,44)
Match is Found. Return index and print “Element Found.”
Algorithm for Recursive Binary Search
intBinarySearch (int A[], int x, int Low, int High)
{
if (High > Low)
return –1;
int mid = (High + Low) / 2;
if (A[mid] == x)
return mid;
if (A[mid] < x)
returnBinarySearch(A, x, mid+1, High);
returnBinarySearch(A, x, Low, mid–1);
}
P2 : Program for Binary Search
#include<stdio.h>
#include<conio.h>
void main()
{
intn,i,search,f=0,low,high,mid,a[20];
clrscr();
printf("Enter the number of elements:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the number in ascending order a[%d]=",i);
scanf("%d",&a[i]);
}
printf("Enter the element to be searched:");
scanf("%d",&search);
low=0;
high=n–1;
while(low<=high)
{
mid=(low+high)/2;
if(search<a[mid])
{
high=mid–1;
}
else if(search>a[mid])
{
low=mid+1;
}
else
{
f=1;
printf("Element is found at the position %d:",mid);
exit();
}
}
if(f==0)
{
printf("Element not found");
}
getch();
}
Complexity Analysis of Binary Search :
Assume our array size is 16.
When n= 16 BinarySearch is called to reduce size to n=8.
When n= 8 BinarySearch is called to reduce size to n=4.
When n= 4 BinarySearch is called to reduce size to n=2.
When n= 2 BinarySearch is called to reduce size to n=1 .
Thus we see that BinarySearch function is called 4 times
i.e. 4 elements of the array were examined for n =16.
Note that 16 = 24
Let us consider a more general case where
n is still a power of 2. Let us say n =2k
After k searches, the while loop is executed k times and n reduces to size 1. Let us
assume that each run of the while loop involves at most 4 operations.
Thus total number of operations: 4k.
The value of k can be determined from the expression
2k = n
Taking log of both sides
k = log n
Thus total number of operations = 4 log n.
We conclude that the time complexity of the Binary search method is O(log n), which is
much more efficient than the Linear Search method.
Merits :
Binary Search is fast and efficient.
Suitable for large number of values.
Demerits :
Binary search works only on sorted array or list.
5.2 Basic Sorting Algorithms (Bubble, Insertion And Selection)
Bubble Sort
Steps to perform Bubble sort are as follows.
Assume we want to sort the elements in ascending order and no two elements are equal.
1. Compare first element with second element. If first element is greater than next
element, we interchange their position accordingly. i.e first element is moved to second
element’s position and second element is moved to first element’s position. If No, then
we don’t interchange any elements.
2. The element in second position is compared with element in third position and the
process of interchanging elements is performed if second is greater than third element.
The whole process of comparing and interchanging is repeated till last element. When
the process gets completed, the largest element in array will get placed in the last
position of the array.
Let us consider array A of size n. Then Algorithm for Bubble sort is
1. Set i to 0.
2. Set j to 0.
3. if(A[j]>A[j+1])
swap A[j],A[j+1]
4. Increment j
5. if j<(n–1–i) goto step 3
6. Increment i
7. if(i<n–1) goto step 2.
Example 1 : Suppose the following numbers are stored in an array A:
32 51 27 85 66 23 13 57
j = 0 1 2 3 4 5 6 7
We apply bubble sort to array A. We discuss each pass separately.
Pass 1
1. Compare j[0] and j[1]. Since 32 < 51, the list is not altered.
2. Compare j[1] and j[2] Since 51 > 27, interchange 51 and 27 as follows:
32 51 27 85 66 23 13 57
j = 0 1 2 3 4 5 6 7
Array A becomes
32 27 51 85 66 23 13 57
j = 0 1 2 3 4 5 6 7
3. Compare j[2] and j[3]. Since 51 < 85, array is not altered.
4. Compare j[3] and j[4]. Since 85 > 66, interchange 85 and 66 as follows:
32 27 51 85 66 23 13 57
j = 0 1 2 3 4 5 6 7
Array A becomes
32 27 51 66 85 23 13 57
j = 0 1 2 3 4 5 6 7
5. Compare j[4] and j[5]. Since 85 > 23, interchange 85 and 23 as follows:
32 27 51 66 85 23 13 57
j = 0 1 2 3 5 6 7
4
Array A becomes
32 27 51 66 23 85 13 57
j = 0 1 2 3 4 5 6 7
6. Compare j[5] and j[6]. Since 85 > 13, interchange 85 and 13 as follows:
32 27 51 66 23 85 13 57
j = 0 1 2 3 4
5
6 7
32 27 51 66 23 13 85 57
j = 0 1 2 3 4 5 6 7
7. Compare j[6] and j[7]. Since 85 > 57, interchange 85 and 57 as follows:
32 27 51 66 23 13 85 57
j = 0 1 2 3 4 5
6
7
Array A becomes
32 27 51 66 23 13 57 85
j = 0 1 2 3 4 5 6 7
At the end of this first pass, the largest number, 85, has moved to the last position.
However, the rest of the numbers are not sorted, even though some of them have
changed their positions.
Pass 2 will move second last number at second last position, Pass 3 will move third last
number at third last position and so on. Here we show array after every pass.
Pass 2 :
27 33 51 23 13 57 66 85
j =
0
1 2 3 4 5 6 7
Pass 3 :
27 33 23 13 51 57 66 85
j =
0
1 2 3 4 5 6 7
Pass 4:
27 23 13 33 51 57 66 85
j =
0
1 2 3 4 5 6 7
Pass 5 :
23 13 27 33 51 57 66 85
j =
0
1 2 3 4 5 6 7
Pass 6 :
13 23 27 33 51 57 66 85
j =
0
1 2 3 4 5 6 7
Hence the array is sorted in 6 passes.
We will see one more example
Example 2 : Apply Bubble Sort on array A= {5,3,1,9,8,2,4,7}.
j
=0
5 3 1 9 8 2 4 7
j
=1
3 5 1 9 8 2 4 7
j
=2
3 1 5 9 8 2 4 7
j
=3
3 1 5 9 8 2 4 7
j
=4
3 1 5 8 9 2 4 7
j
=5
3 1 5 8 2 9 4 7
j
=6
3 1 5 8 2 4 9 7
j
=7
3 1 5 8 2 4 7 9
Pass 2 :i=2
3 1 5 8 2 4 7 9
1 3 5 8 2 4 7 9
1 3 5 8 2 4 7 9
1 3 5 8 2 4 7 9
1 3 5 2 8 4 7 9
1 3 5 2 4 8 7 9
1 3 5 2 4 7 8 9
Pass 3 :
1 3 5 2 4 7 8 9
1 3 5 2 4 7 8 9
1 3 5 2 4 7 8 9
1 3 2 5 4 7 8 9
1 3 2 4 5 7 8 9
Pass 4 :
1 3 2 4 5 7 8 9
1 3 2 4 5 7 8 9
1 2 3 4 5 7 8 9
This array gets sorted in 4 passes only.
Example : Apply Bubble Sort on array A= {23,78,45,8,32,56}
Pass 1 :
23 78 45 8 32 56
23 78 45 8 32 56
23 45 78 8 32 56
23 45 8 78 32 56
23 45 8 32 78 56
23 45 8 32 56 78
Pass 2 :
23 45 8 32 56 78
23 45 8 32 56 78
23 8 45 32 56 78
23 8 32 45 56 78
Pass 3 :
23 8 32 45 56 78
8 23 32 45 56 78
This array is sorted in 3 passes.
P3: Write a Program for Bubble sort in C
#include <stdio.h>
#include<conio.h>
voidbubble_sort(int A[], int n);
int main()
{
int A[100], n, i, j, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter the elementsn", );
for (i = 0; i< n; i++)
scanf("%d", &A[i]);
bubble_sort(A, n);
printf("Sorted list in ascending order:n");
for ( i = 0 ; i< n ; i++ )
printf("%ldn", A[i]);
return 0;
}
voidbubble_sort(int A[], int n)
{
inti, j, t;
for (i = 0 ; i< ( n – 1 ); i++)
{
for (j = 0 ; j < (n – i – 1); j++)
{
if (A[j] > list[j+1])
{
/* Swapping */
t = A[j];
A[j] = A[j+1];
A[j+1] = t;
}
}
}
}
Complexity Bubble sort:
Suppose we have a list with n elements, and each element perform n – 1 comparisons
with elements to its left, and swap, if necessary.
Best Case: If the list is already sorted, only one iteration is performed and complexity is
O(1).
Average and Worst case: For n elements at most n – 1 swap operations is performed in
each pass. The worst and average case complexity is O(n2).
Selection Sort
Assume we want to sort list in ascending order. Selection sort finds the smallest element
from unsorted list and swap it with the element in first position. Then it finds second
smallest element and swap it with element at second position. This process is repeated
till entire list is sorted in ascending order. Each time we swap elements, we say that
we have completed a sort pass. A list of n elements requires n–1 passes to
completely rearrange the data.
Procedure for every pass is as follows.
Pass 1 : Find the position P of the smallest in the list of N elements A[l], A[2], . . . , A[N],
and then interchange A[P] and A[1] . Then A[1] is sorted.
Pass 2 : Find the position P of the smallest in the sublist of N –1 elements A[2], A[3],. . . ,
A[N], and then interchange A[P]and A[2]. Then A[l], A[2] is sorted.
Pass 3 : Find the position P of the smallest in the sublist of N–2 elements A[3], A[4], . . . ,
A[N], and then interchange A[P] and A[3]. Then: A[l], A[2] , A[3] is sorted.
Pass N –1 :Find the position P of the smaller of the elements A[N –1), A[N], and then
interchange A[P] and A[N–1]. Then: A[l], A[2], . . . , A[N] is sorted. Thus A is sorted after
N –1 passes.
Example 1 :
25 79 41 9 34 60
Fig. 4.2 : Original List
We will apply selection sort on this list.
Unsorted sublist has 25 as a smallest element and 79 is located at second position.
Hence we swap 25 and 79.
After Pass 5 :
9 25 34 41 60 79
Thus list is sorted.
We will see one more example.
Apply selection sort on array A= {77,30,40,10,88,20,65,56}
Function for Selection Sort
voidselectionSort(int A[], int n)
{
inti, j, s, temp;
for (i= 0; i<= n; i ++)
{
s = i;
for (j=i+1; j <= n; j++)
if(A[j] < A[s])
s= j;
// Smallest selected; swap with current element
temp = A[i];
A[i] = A[s];
A[s] = temp;
}
P4 : Program to perform Selection Sort.
#include <stdio.h>
int main()
{
int A[100], n, i, j, s, temp;
/* n=total no. of elements in array
s= smallest element in unsorted array
temp is used for swapping */
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for ( i = 0 ; i< n ; i++ )
scanf("%d", &A[i]);
for ( i = 0 ; i< ( n – 1 ) ; i++ )
{
s = i;
for ( j = i + 1 ; j < n ; j++ )
{
if ( A[s] > A[j] )
s = j;
}
if ( s != i )
{
temp = A[i];
A[i] = A[s];
A[s] = temp;
}
}
printf("Sorted list in ascending order:n");
for ( i = 0 ; i< n ; i++ )
printf("%dn", A[i]);
return 0;
}
Output:
Enter number of elements
5
Enter 5 integers
4
1
7
5
9
Sorted list in ascending order
1
4
5
7
9
Complexity of Selection Sort :
In selection sort outer for loop is executed n–1 times. On the kth time through the outer
loop, initially the sorted list holds
k–1 elements and unsorted portion holds n–k+1 elements. In inner for loop 2 elements
are compared each time.
Thus, 2*(n–k) elements are examined by the inner loop during the k th pass through the
outer loop. But k ranges from 1 to n–1.
Total number of elements examined is:
T(n) = 2*(n –1) + 2*(n–2) + 2*(n–3) + .. + 2*(n–(n–2))
+ 2*(n–(n–1))
= 2*((n–1) + (n–2) + (n–3) + ... + 2 + 1)
(or 2*(sum of first n–1 integers)
= 2*((n–1)*n)/2)
= n2
– n, so complexity of algorithm is O(n2
).
Insertion Sort
Insertion Sort reads all elements from 1 to n and inserts each element at its proper
position. This sorting method works well on small list of elements.
Procedure for each pass is as follows.
Pass 1 :
A[l] by itself is trivially sorted.
Pass 2 :
A[2] is inserted either before or after A[l] so that: A[l], A[2] is sorted.
Pass 3 :
A[3] is inserted into its proper place so that: A[l], A[2], A[3] is sorted.
Pass 4 :
A[4] is inserted into its proper place A[l], A[2], A[3], A[4] is sorted.
Pass N :
A[N] is inserted into its proper place in so that:
A[l], A[ 2 ] , . . . , A[ N ] is sorted
Example :We will apply insertion sort on original list of Fig.3.Assume we are arranging
elements in ascending order.
25 79 41 9 34 60
Fig. 4.3 : Original List
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASIC SORTING ALGORITHMS (BUBBLE, INSERTION AND SELECTION), FINDING ROOTS OF EQUATIONS, NOTION OF ORDER OF COMPLEXITY THROUGH EXAMPLE PROGRAMS (NO FORMAL DEFINITION REQUIRED)
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASIC SORTING ALGORITHMS (BUBBLE, INSERTION AND SELECTION), FINDING ROOTS OF EQUATIONS, NOTION OF ORDER OF COMPLEXITY THROUGH EXAMPLE PROGRAMS (NO FORMAL DEFINITION REQUIRED)
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASIC SORTING ALGORITHMS (BUBBLE, INSERTION AND SELECTION), FINDING ROOTS OF EQUATIONS, NOTION OF ORDER OF COMPLEXITY THROUGH EXAMPLE PROGRAMS (NO FORMAL DEFINITION REQUIRED)
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASIC SORTING ALGORITHMS (BUBBLE, INSERTION AND SELECTION), FINDING ROOTS OF EQUATIONS, NOTION OF ORDER OF COMPLEXITY THROUGH EXAMPLE PROGRAMS (NO FORMAL DEFINITION REQUIRED)
C Function for Insertion Sort
voidinsertionSort(int A[], int n)
{
inti, p, temp, j;
for (i = 1; i<=n; i++)
{
p= 0;
temp = A[i];
for (j=i–1; j >= 0 && !p;)
if(temp < A[j])
{
A[j + 1]= A[j];
j––;
}
else
p = 1;
A[j + 1] = temp;
}
return;
}
P5 : Program to implement insertion sort.
#include <stdio.h>
int main()
{
int n, A[100], i, j, t;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (i = 0; i< n; i++) {
scanf("%d", &A[i]);
}
for (i = 1 ; i<=( n – 1); i++)
{
j = i;
while ( j > 0 && A[j] < A[j–1])
{
t = A[j];
A[j] = A[j–1];
A[j–1] = t;
j––;
}
}
printf("Sorted list in ascending order:n");
for (i = 0; i<= n – 1; i++) {
printf("%dn", A[i]);
}
return 0;
}
Output :
Enter number of elements
5
Enter 5 integers
9
4
2
5
3
Sorted list in ascending order
2
3
4
5
9
Complexity of Insertion Sort :
Worst Case Complexity: In the worst case, for each i we do (i – 1) swaps inside the inner
for–loop–therefore, overall number of swaps (when i goes from 2 to n in the outer for
loop) is
T(n) = 1 + 2 + 3 +...+(I – 1) +....+ n – 1
= n  (n – 1)/2
= O(n2
)
Average case Complexity is O(n2
).
Best Case Complexity is O(n).
5.3 Finding Roots Of Equations
Algorithm:
Step 1: Start
Step 2: Read a,b,c
Step 3: Initialize d <- (b*b) – (4*a*c)
Step 4: Initialize r<- b/2*a
Step 5: if d>0 go to step 6
Step 6: r1 = r+(sqrt(d)/2*a) and r2 = r-(sqrt(d)/2*a)
Step 7: print roots are real and distinct first root r1, second root r2
Step 8: if d=0 go to step 9
else go to step 10
Step 9: print roots are real and equal -r
Step 10 : d= -d
Step 11: im = sqrt(d)/2*a
Step 12: print roots are imaginary, first root r+iim, second root r-iim
Step 13: stop
Program:
void main()
{
float a,b,c,r,d,r1,r2,im;
clrscr();
printf(“nt Quadratic Equtionnn Enter the coefficientsn”);
scanf(“%f%f%f”, &a,&b,&c);
d= (b*b) –(4*a*c);
r=b/2*a
if(d>0)
{
r 1 = -r + (sqrt (d)/2*a)
r2 = -r - + (sqrt (d)/2*a)
printf(“n Roots are real and distinct nn first roott: %.2fnn second roott:%.2f 
n”,r1,r2);
}
else if(d==0)
printf(“n Roots are real and equal nn roots =:%.2f,-r);
}
else
{
d=-d;
im=sqrt(d)/2*a;
printf(“nRoots are imaginary nnfirst roott;%.2fnnsecond roott:%.2f-i
%.2f”,r,im,r,im);
}
getch();
}
5.4 Notion Of Order Of Complexity Through Example Programs (No Formal
Definition Required)
To express the running time complexity of algorithm three asymptotic notations are
available. Asymptotic notations are also called as asymptotic bounds. Notations are used
to relate the growth of complex function with the simple function. Asymptotic notations
have domain of natural numbers. These notations are as follows
1. Big-oh notation: Big-oh notations can be express by using ‘o’ symbol. This notation
indicates the maximum number of steps required to solve the problem. This notation
expresses the worst case growth of an algorithm. Consider the following diagram in
which there are two functions f(x) and g(x). f(x) is more complex than the function g(x).
Fig. 3.7
In the above diagram out of two function f(n) and g(n), more complex function is f(n), so
need to relate its growth as compare to simple function g(n). More specifically we need
one constant function c(n) which bound function f(n) at some point n0. Beyond the point
‘n0’ function c*g(n) is always greater than f(n).
Now f(n)=Og(n)if there is some constant ‘c’ and some initial value ‘n0’. such that
f(n)<=c*g(n) for all n>n0
In the above expression ‘n’ represents the input size
f(n) represents the time that algorithm will take
f(n) and g(n) are non-negative functions.
Consider the following example
Fig. 3.8
In the above example g(n) is ‘’n’ and f(n) is ‘2n+1’ and value of constant is 3. The point
where c*g(n) and f(n) intersect is ‘’n0’. Beyond ‘n0’ c*g(n) is always greater than f(n) for
constant ‘3’. We can write the conclusion as follows
f(n)=O g(n) for constant ‘c’ =3 and ‘n0’=1
such that f(n)<=c*g(n) for all n>n0
2. Big-omega notation: Big-omega notations can be express by using ‘Ω’ symbol. This
notation indicates the minimum number of steps required to solve the problem. This
notation expresses the best case growth of an algorithm. Consider the following
diagram in which there are two functions f(n) and g(n). f(n) is more complex than the
function g(n).
Fig. 3.9
In the above diagram out of two function f(n) and g(n), more complex function is f(n), so
need to relate its growth as compare to simple function g(n). More specifically we need
one constant function c(n) which bound function f(n) at some point n0. Beyond the point
‘n0’ function c*g(n) is always smaller than f(n).
Now f(n)=Ωg(n)if there is some constant ‘c’ and some initial value ‘n0’. such that c*g(n)
<= f(n)for all n>n0
In the above expression ‘n’ represents the input size
f(n) represents the time that algorithm will take
f(n) and g(n) are non-negative functions.
Consider the following example
In the above example g(n) is ‘2n’ and f(n) is ‘2n-2’ and value of constant is 0.5. The point
where c*g(n) and f(n) intersect is ‘’n0’. Beyond ‘n0’ c*g(n) is always smaller than f(n) for
constant ‘0.5’. We can write the conclusion as follows
f(n)=Ω g(n) for constant ‘c’ =0.5 and ‘n0’=2
such that c*g(n)<= f(n) for all n>n0
Fig. 3.10
Big-theta notation: Big-theta notations can be express by using ‘Ɵ’ symbol. This
notation indicates the exact number of steps required to solve the problem. This
notation expresses the average case growth of an algorithm. Consider the following
diagram in which there are two functions f(n) and g(n). f(n) is more complex than the
function g(n).
Fig. 3.11
In the above diagram out of two function f(n) and g(n), more complex function is f(n), so
need to relate its growth as compare to simple function g(n). More specifically we need
one constant function c1 and c2 which bound function f(n) at some point n0. Beyond the
point ‘n0’ function c1*g(n) is always smaller than f(n) and c2*g(n) is always greater than
f(n).
Now f(n)= g(n) if there is some constant ‘c1 and c2’ and some initial value ‘n0’. such
that c1*g(n) <= f(n)<=c2*g(n)for all n>n0
In the above expression ‘n’ represents the input size
f(n) represents the time that algorithm will take
f(n) and g(n) are non-negative functions.
f(n)= g(n) if and only if f(n)=O g(n) and f(n)=Ω g(n)
Consider the following example
Fig. 3.12
In the above example g(n) is ‘2n’ and f(n) is ‘3n-2’ and value of constant c1 is 0.5 and c2
is 2. The point where c*g(n) and f(n) intersect is ‘’n0’. Beyond ‘n0’ c1*g(n) is always
smaller than f(n) for constant ‘0.5’ and c2*g(n) is always greater than f(n) for constant ‘2’.
We can write the conclusion as follows
f(n)=Ω g(n) for constant ‘c1’ =0.5 , ‘c2’ = 2 and ‘n0’=2
such that c1*g(n)<= f(n)<=c2*g(n) for all n>n0

More Related Content

PPT
Data Structure and Algorithms Hashing
ManishPrajapati78
 
PPTX
Array implementation and linked list as datat structure
Tushar Aneyrao
 
PPT
Hash tables
Rajendran
 
PPTX
Introduction to tree ds
Viji B
 
PPTX
Queues
nidhisatija1
 
PDF
File Types in Data Structure
Prof Ansari
 
PDF
Fundamentals of data structures ellis horowitz & sartaj sahni
Hitesh Wagle
 
PPTX
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
DrkhanchanaR
 
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Array implementation and linked list as datat structure
Tushar Aneyrao
 
Hash tables
Rajendran
 
Introduction to tree ds
Viji B
 
Queues
nidhisatija1
 
File Types in Data Structure
Prof Ansari
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Hitesh Wagle
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
DrkhanchanaR
 

What's hot (20)

PPTX
Hashing Technique In Data Structures
SHAKOOR AB
 
PPTX
DMBS Indexes.pptx
husainsadikarvy
 
PPTX
B+ trees and height balance tree
Jasleen Kaur (Chandigarh University)
 
PPTX
Cardinality and participation constraints
Nikhil Deswal
 
PPT
B-tree & R-tree
Shakil Ahmed
 
PPTX
Queues in C++
Vineeta Garg
 
PDF
การแยกตัวประกอบของพหุนามดีกรีสูงกว่าสองที่มีสัมประสิทธิ์เป็นจำนวนเต็ม 
Somporn Amornwech
 
PDF
Hashing and File Structures in Data Structure.pdf
JaithoonBibi
 
PPTX
Stacks in c++
Vineeta Garg
 
PPT
Present
guest841a6f
 
PPT
File handling in C++
Hitesh Kumar
 
PPTX
Searching
Ashim Lamichhane
 
PPTX
Trees
Burhan Ahmed
 
PDF
Expression trees
Salman Vadsarya
 
PPTX
Lexical analyzer
Princess Doll
 
PPT
Indexing and Hashing.ppt
vedantihp21
 
PPT
Abstract data types (adt) intro to data structure part 2
Self-Employed
 
PPT
vdocuments.mx_chapter-2-database-environment-thomas-connolly-carolyn-begg-dat...
adeel8937
 
PPTX
Tree Basic concepts of Tree in Data Structure
Manoj PAtil
 
PPTX
Binary search
AparnaKumari31
 
Hashing Technique In Data Structures
SHAKOOR AB
 
DMBS Indexes.pptx
husainsadikarvy
 
B+ trees and height balance tree
Jasleen Kaur (Chandigarh University)
 
Cardinality and participation constraints
Nikhil Deswal
 
B-tree & R-tree
Shakil Ahmed
 
Queues in C++
Vineeta Garg
 
การแยกตัวประกอบของพหุนามดีกรีสูงกว่าสองที่มีสัมประสิทธิ์เป็นจำนวนเต็ม 
Somporn Amornwech
 
Hashing and File Structures in Data Structure.pdf
JaithoonBibi
 
Stacks in c++
Vineeta Garg
 
Present
guest841a6f
 
File handling in C++
Hitesh Kumar
 
Searching
Ashim Lamichhane
 
Expression trees
Salman Vadsarya
 
Lexical analyzer
Princess Doll
 
Indexing and Hashing.ppt
vedantihp21
 
Abstract data types (adt) intro to data structure part 2
Self-Employed
 
vdocuments.mx_chapter-2-database-environment-thomas-connolly-carolyn-begg-dat...
adeel8937
 
Tree Basic concepts of Tree in Data Structure
Manoj PAtil
 
Binary search
AparnaKumari31
 
Ad

Similar to PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASIC SORTING ALGORITHMS (BUBBLE, INSERTION AND SELECTION), FINDING ROOTS OF EQUATIONS, NOTION OF ORDER OF COMPLEXITY THROUGH EXAMPLE PROGRAMS (NO FORMAL DEFINITION REQUIRED) (20)

PDF
Searching
A. S. M. Shafi
 
PPTX
Searching and Sorting Algorithms in Data Structures
poongothai11
 
PPTX
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
DOCX
UNIT V.docx
Revathiparamanathan
 
PDF
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
PDF
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
PPTX
searching in data structure.pptx
chouguleamruta24
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
PPT
search_sort.ppt
SwatiHans10
 
PPTX
Linear and binary search
JeoJoyA
 
DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PPTX
placement preparation for Array Searching.pptx
upasnatiwari10
 
PPTX
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
PPT
Data structure and problem solving ch03.ppt
Ping261512
 
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
PPT
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
arpitasahad87
 
PPTX
Lecture3b searching
mbadhi barnabas
 
Searching
A. S. M. Shafi
 
Searching and Sorting Algorithms in Data Structures
poongothai11
 
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
UNIT V.docx
Revathiparamanathan
 
UNIT IV -Data Structures.pdf
KPRevathiAsstprofITD
 
In the binary search, if the array being searched has 32 elements in.pdf
arpitaeron555
 
searching in data structure.pptx
chouguleamruta24
 
Chapter 2 Sorting and Searching .pptx.soft
kuruabeje7
 
search_sort.ppt
SwatiHans10
 
Linear and binary search
JeoJoyA
 
MODULE 5-Searching and-sorting
nikshaikh786
 
placement preparation for Array Searching.pptx
upasnatiwari10
 
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
Data structure and problem solving ch03.ppt
Ping261512
 
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
search_sort_v1.pptgghghhhggggjjjjjjllllllllvbbbbbcfdsdfffg
arpitasahad87
 
Lecture3b searching
mbadhi barnabas
 
Ad

More from Sitamarhi Institute of Technology (20)

DOCX
RRB Technician Syllabus for Technician Gr I Signal.docx
Sitamarhi Institute of Technology
 
DOCX
NISHCHAY INDIA MANPOWER SUPPLY THROUGH CBT EXAMINATIONAssistant Manager Posit...
Sitamarhi Institute of Technology
 
PDF
Microsoft OneDrive and Google Drive for Beginners.pdf
Sitamarhi Institute of Technology
 
PDF
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
Sitamarhi Institute of Technology
 
PDF
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
Sitamarhi Institute of Technology
 
DOCX
METHODS OF CUTTING COPYING HTML BASIC NOTES
Sitamarhi Institute of Technology
 
PDF
introduction Printer basic notes Hindi and English
Sitamarhi Institute of Technology
 
PDF
Beginners Guide to Microsoft OneDrive 2024–2025.pdf
Sitamarhi Institute of Technology
 
PDF
ChatGPT Foundations rompts given for each topic in both personal and business...
Sitamarhi Institute of Technology
 
PDF
Google Drive Mastery Guide for Beginners.pdf
Sitamarhi Institute of Technology
 
PDF
Chat GPT 1000+ Prompts - Chat GPT Prompts .pdf
Sitamarhi Institute of Technology
 
PDF
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Sitamarhi Institute of Technology
 
PDF
WhatsApp Tricks and Tips - 20th Edition 2024.pdf
Sitamarhi Institute of Technology
 
PDF
Mastering ChatGPT for Creative Ideas Generation.pdf
Sitamarhi Institute of Technology
 
PDF
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
Sitamarhi Institute of Technology
 
PDF
MS Word tutorial provides basic and advanced concepts of Word.
Sitamarhi Institute of Technology
 
PPTX
BELTRON_PROGRAMMER 2018 and 2019 previous papers
Sitamarhi Institute of Technology
 
PDF
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
Sitamarhi Institute of Technology
 
PDF
Enhancing-digital-engagement-integrating-storytelling-
Sitamarhi Institute of Technology
 
PDF
business-with-innovative email-marketing-solution-
Sitamarhi Institute of Technology
 
RRB Technician Syllabus for Technician Gr I Signal.docx
Sitamarhi Institute of Technology
 
NISHCHAY INDIA MANPOWER SUPPLY THROUGH CBT EXAMINATIONAssistant Manager Posit...
Sitamarhi Institute of Technology
 
Microsoft OneDrive and Google Drive for Beginners.pdf
Sitamarhi Institute of Technology
 
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
Sitamarhi Institute of Technology
 
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
Sitamarhi Institute of Technology
 
METHODS OF CUTTING COPYING HTML BASIC NOTES
Sitamarhi Institute of Technology
 
introduction Printer basic notes Hindi and English
Sitamarhi Institute of Technology
 
Beginners Guide to Microsoft OneDrive 2024–2025.pdf
Sitamarhi Institute of Technology
 
ChatGPT Foundations rompts given for each topic in both personal and business...
Sitamarhi Institute of Technology
 
Google Drive Mastery Guide for Beginners.pdf
Sitamarhi Institute of Technology
 
Chat GPT 1000+ Prompts - Chat GPT Prompts .pdf
Sitamarhi Institute of Technology
 
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Sitamarhi Institute of Technology
 
WhatsApp Tricks and Tips - 20th Edition 2024.pdf
Sitamarhi Institute of Technology
 
Mastering ChatGPT for Creative Ideas Generation.pdf
Sitamarhi Institute of Technology
 
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
Sitamarhi Institute of Technology
 
MS Word tutorial provides basic and advanced concepts of Word.
Sitamarhi Institute of Technology
 
BELTRON_PROGRAMMER 2018 and 2019 previous papers
Sitamarhi Institute of Technology
 
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
Sitamarhi Institute of Technology
 
Enhancing-digital-engagement-integrating-storytelling-
Sitamarhi Institute of Technology
 
business-with-innovative email-marketing-solution-
Sitamarhi Institute of Technology
 

Recently uploaded (20)

PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
easa module 3 funtamental electronics.pptx
tryanothert7
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Information Retrieval and Extraction - Module 7
premSankar19
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
easa module 3 funtamental electronics.pptx
tryanothert7
 
Inventory management chapter in automation and robotics.
atisht0104
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Introduction to Data Science: data science process
ShivarkarSandip
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 

PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASIC SORTING ALGORITHMS (BUBBLE, INSERTION AND SELECTION), FINDING ROOTS OF EQUATIONS, NOTION OF ORDER OF COMPLEXITY THROUGH EXAMPLE PROGRAMS (NO FORMAL DEFINITION REQUIRED)

  • 1. PPS Unit – 5 Basic Algorithms 5.1 Searching (Linear Search, Binary Search Etc.) Sorting and Searching are fundamental operations in computer science. Sorting refers to the operation of arranging data in some given order. Searching refers to the operation of searching the particular record from the existing information. Consider a database of banking system where information of all customers such as name, contact number, address, account number is stored. If a manager wants to search for a record of a particular customer, he has to look for that record from among all records that has been stored in a database. This process of looking up for a particular record in a database is referred as searching. If records in a banking database are not ordered properly it will be very difficult for a manager to search for a specific record. On the contrary if all record are arranged in order according to some criteria like names in alphabetical order or account numbers in ascending order, searching becomes easy and fast. The process of ordering the records in a data base is called sorting. Thus for efficient searching sorting is necessary. Linear Search Given a collection of objects, the goal of search is to find a particular object in this collection. The objects have key values on which search is performed and data values which correspond to the information one wishes to retrieve once an object is found. For example, a telephone book is a collection of names on which one searches and telephone numbers which correspond to the data being sought. The collection of objects is often stored in a list or an array. Given a collection of objects in an array A[1.. n], the ith element A[i] corresponds to the key value of the ith object in the collection. The input to a search algorithm is an array of objects A, the number of objects n, and the key value being sought x. Thus Algorithm for Linear Search is 1. start at one end of the list 2. if the current element is not equal to the search target, move to the next element, 3. stop when a match is found or the opposite end of the list is reached.
  • 2. Function for Linear search is find(values, target) { for(i = 0; i<values.length; i++) { if (values[i] == target) { returni; } } return –1; } If element is found this function returns index of that element otherwise it returns –1. Example : we have an array of integers, like the following: Elements  9 6 7 12 1 5 Index 0 1 2 3 4 5 Let’s search for the number 7. According to function for linear search index for element 7 should be returned. We start at the beginning and check the first element in the array. 9 6 7 12 1 5 First element is 9.Match is not found and hence we move to next element i.e 6. 9 6 7 12 1 5 Again match is not found so move to next element i.e.7. 9 6 7 12 1 5 0 1 2 3 4 5 We found the match at the index position 2. Hence output is 2.
  • 3. If we try to find element 10 in above array, it will return –1,as 10 is not present in the array. Complexity Analysis of Linear Search: Let us assume element x is to be searched in array A of size n. An algorithm can be analyzed using following three cases. 1. Worst case 2. Average case 3. Best case 1. Worst case :In Linear search worst case happens when element to be searched is not present. When x is not present, the linear search function compares it with all the elements of A one by one. The size of A is n and hence x is to be compared with all n elements. Thus, the worst case time complexity of linear search would be O(n). 2. Average case : In average case analysis, we take all possible inputs and calculate computing time for all of the inputs. Sum all the calculated values and divide the sum by total number of inputs. Following is the value of average case time complexity. 3. Best Case : In linear search , the best case occurs when x is present at the first location. So time complexity in the best case would be O(1). P1 : Program for Linear Search #include<stdio.h> #include<conio.h> int search(int [],int, int); void main() { int a[20],k,i,n,m; clrscr();
  • 4. printf(“nn ENTER THE SIZE OF ARRAY”); scanf(“%d”,&n); printf(“nnENTER THE ARRAY ELEMENTSnn”); for(i=0;i<n;i++) { scanf(“%d”,&a[i]); } printf(“nnENTER THE ELEMENT FOR SEARCHn”); scanf(“%d”,&k); m=search(a,n,k); if(m==–1) { printf(“nnELEMENT IS NOT FOUND IN LIST”); } else { printf(“nnELEMENT IS FOUND AT LOCATION %d”,m); } } getch(); } int search(int a[],intn,int k) { inti; for(i=0;i<n;i++) { if(a[i]==k) { return i; } } if(i==n) { return –1; } } Merits : 1. Simple and easy to implement
  • 5. 2. It works well even on unsorted arrays. 3. It is memory efficient as it does not require copying and partitioning of the array being searched. Demerits : 1. Suitable only for small number of values. 2. It is slow as compared to binary search. Binary Search Binary Search requires sorted array. Steps to perform binary search on sorted array A of size n are as follows. 1. Initialize lower and upper limit of array to be searched Low=0,High= n–1. 2. Find the middle position Mid=(Low + High)/2 3. Compare the input key value with the key value of the middle element of the array. It has 3 possibilities. Case 1 : If keys match then return mid and print element found. if(key==A[mid]) return mid Case 2 : If key is less than middle element then element is present in left half of array. Update High and goto step 2. if(key<A[mid]) High= Mid–1 goto step 2 Case 3 : If key is greater than middle element then element is present in right half of array. Update Low and goto step 2. if(key>A[mid])
  • 6. Low= Mid+1 goto step 2 Case 4:if(Low>High) Print element not found. Iterative algorithm for Binary Search intBinarySearch( int A[],int x) { intmid,n; int Low = 0; int High = n–1; while ( Low <= High ) { mid = (Low + High) / 2; if ( A[mid] == x ) return mid; if ( A[mid] > x ) High = mid – 1; else Low = mid + 1; } return –1; }
  • 7. Example 1 :Let array A= {–1, 5, 6, 18, 19, 25, 46, 78, 102, 114}. We want to find element ‘6’ using binary search. –1 5 6 18 19 25 46 78 102 114 0 1 2 3 4 5 6 7 8 9 Solution: Step 1 : Low=0, High= 9 Step 2 :Mid=(Low + High)/2 Mid = (0+9)/2 =4.5 i.e 4. It means middle element is located at position 4. So A[Mid] is 19.Thus Low=0 Mid=4 High=9 Low Mid High –1 5 6 18 19 25 46 78 10 2 114 0 1 2 3 4 5 6 7 8 9 Step 3 :Compare 6 with 19. It satisfies case 2 of step 3. so High= 4–1=3 and element is present in left half of array. We can ignore right half of array. Repeat step 2. So mid= (0+3)/2 = 1.5 i.e 1. mid is at position 1. Element 5 is present at position 1. Low = 0 Mid = 1 High = 3 Low Mid High –1 5 6 18 19 0 1 2 3 4 Again compare 5 with 6. It satisfies case 3 of step 3. so Low = 1+1=2.
  • 8. Repeat step 2. Mid= (2+ 3)/2= 2.5 i.e 2 .Mid is at position 2. Element 6 is at position 2. Low=2 Mid=2 High=3 Low, Mid High –1 5 6 18 19 0 1 2 3 4 Compare 6 with 6. Match is found. Return the position and print element found. Example 2 : Find 103 in {–1, 5, 6, 18, 19, 25, 46, 78, 102, 114} using Binary Search. Low Mid High –1 5 6 18 19 25 46 78 10 2 114 0 1 2 3 4 5 6 7 8 9 Step 1 : Low=0 High=9 Step 2 :Mid=(0+9)/2=4 So middle element is 19. Step 3 : Compare(103,19) 103>19 hence element is located at right half of array and we can ignore left half of array. Update Low and recalculate mid. Low = 4+1=5 High = 9 Mid = (5+9)/2=7 Low Mid High 25 46 78 102 114 5 6 7 8 9
  • 9. Middle element is 78. Compare(103,78) 103>78. Recalculate Low and Mid Low = 7+1=8 High = 9 Mid = (8+9)/2=8 Low, Mid High 102 114 8 9 Middle element is 102. Compare(103,102) 103>102. Recalculate Low and Mid. Low = 8+1=9 High = 9 Here Low=High and match is not found hence we conclude that element is not present in array. Example 3 : Find 44 in A={5,12,17,23,38,44,77,84,90} using binary search. Step 1: Low=0 High=8 Mid=(0+8)/2=4 Low Mid High 5 12 17 23 38 44 77 84 90 0 1 2 3 4 5 6 7 8 Middle element is 38. Compare(44,38) 44>38.Thus element is located at right half of array and we can ignore left half of array. Recalculate Low and Mid. Low = (4+1)= 5
  • 10. High = 8 Mid = (5+8)/2=6.5 i.e 6 Low Mid high 44 77 84 90 0 1 2 3 Middle element is 77. Compare(44,77) 44<77. Recalculate High and Mid. Low=5 High = 6–1=5 Mid = (5+5)/2=5 Low, Mid, high 44 77 84 90 0 1 2 3 Middle element is 44. Compare(44,44) Match is Found. Return index and print “Element Found.” Algorithm for Recursive Binary Search intBinarySearch (int A[], int x, int Low, int High) { if (High > Low) return –1; int mid = (High + Low) / 2; if (A[mid] == x) return mid; if (A[mid] < x)
  • 11. returnBinarySearch(A, x, mid+1, High); returnBinarySearch(A, x, Low, mid–1); } P2 : Program for Binary Search #include<stdio.h> #include<conio.h> void main() { intn,i,search,f=0,low,high,mid,a[20]; clrscr(); printf("Enter the number of elements:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter the number in ascending order a[%d]=",i); scanf("%d",&a[i]); } printf("Enter the element to be searched:"); scanf("%d",&search); low=0; high=n–1; while(low<=high) { mid=(low+high)/2; if(search<a[mid]) { high=mid–1; } else if(search>a[mid]) { low=mid+1; } else { f=1; printf("Element is found at the position %d:",mid); exit();
  • 12. } } if(f==0) { printf("Element not found"); } getch(); } Complexity Analysis of Binary Search : Assume our array size is 16. When n= 16 BinarySearch is called to reduce size to n=8. When n= 8 BinarySearch is called to reduce size to n=4. When n= 4 BinarySearch is called to reduce size to n=2. When n= 2 BinarySearch is called to reduce size to n=1 . Thus we see that BinarySearch function is called 4 times i.e. 4 elements of the array were examined for n =16. Note that 16 = 24 Let us consider a more general case where n is still a power of 2. Let us say n =2k After k searches, the while loop is executed k times and n reduces to size 1. Let us assume that each run of the while loop involves at most 4 operations. Thus total number of operations: 4k. The value of k can be determined from the expression 2k = n Taking log of both sides k = log n
  • 13. Thus total number of operations = 4 log n. We conclude that the time complexity of the Binary search method is O(log n), which is much more efficient than the Linear Search method. Merits : Binary Search is fast and efficient. Suitable for large number of values. Demerits : Binary search works only on sorted array or list. 5.2 Basic Sorting Algorithms (Bubble, Insertion And Selection) Bubble Sort Steps to perform Bubble sort are as follows. Assume we want to sort the elements in ascending order and no two elements are equal. 1. Compare first element with second element. If first element is greater than next element, we interchange their position accordingly. i.e first element is moved to second element’s position and second element is moved to first element’s position. If No, then we don’t interchange any elements. 2. The element in second position is compared with element in third position and the process of interchanging elements is performed if second is greater than third element. The whole process of comparing and interchanging is repeated till last element. When the process gets completed, the largest element in array will get placed in the last position of the array. Let us consider array A of size n. Then Algorithm for Bubble sort is 1. Set i to 0. 2. Set j to 0. 3. if(A[j]>A[j+1]) swap A[j],A[j+1]
  • 14. 4. Increment j 5. if j<(n–1–i) goto step 3 6. Increment i 7. if(i<n–1) goto step 2. Example 1 : Suppose the following numbers are stored in an array A: 32 51 27 85 66 23 13 57 j = 0 1 2 3 4 5 6 7 We apply bubble sort to array A. We discuss each pass separately. Pass 1 1. Compare j[0] and j[1]. Since 32 < 51, the list is not altered. 2. Compare j[1] and j[2] Since 51 > 27, interchange 51 and 27 as follows: 32 51 27 85 66 23 13 57 j = 0 1 2 3 4 5 6 7 Array A becomes 32 27 51 85 66 23 13 57 j = 0 1 2 3 4 5 6 7 3. Compare j[2] and j[3]. Since 51 < 85, array is not altered. 4. Compare j[3] and j[4]. Since 85 > 66, interchange 85 and 66 as follows: 32 27 51 85 66 23 13 57 j = 0 1 2 3 4 5 6 7 Array A becomes 32 27 51 66 85 23 13 57 j = 0 1 2 3 4 5 6 7 5. Compare j[4] and j[5]. Since 85 > 23, interchange 85 and 23 as follows: 32 27 51 66 85 23 13 57 j = 0 1 2 3 5 6 7
  • 15. 4 Array A becomes 32 27 51 66 23 85 13 57 j = 0 1 2 3 4 5 6 7 6. Compare j[5] and j[6]. Since 85 > 13, interchange 85 and 13 as follows: 32 27 51 66 23 85 13 57 j = 0 1 2 3 4 5 6 7 32 27 51 66 23 13 85 57 j = 0 1 2 3 4 5 6 7 7. Compare j[6] and j[7]. Since 85 > 57, interchange 85 and 57 as follows: 32 27 51 66 23 13 85 57 j = 0 1 2 3 4 5 6 7 Array A becomes 32 27 51 66 23 13 57 85 j = 0 1 2 3 4 5 6 7 At the end of this first pass, the largest number, 85, has moved to the last position. However, the rest of the numbers are not sorted, even though some of them have changed their positions. Pass 2 will move second last number at second last position, Pass 3 will move third last number at third last position and so on. Here we show array after every pass. Pass 2 : 27 33 51 23 13 57 66 85 j = 0 1 2 3 4 5 6 7 Pass 3 : 27 33 23 13 51 57 66 85
  • 16. j = 0 1 2 3 4 5 6 7 Pass 4: 27 23 13 33 51 57 66 85 j = 0 1 2 3 4 5 6 7 Pass 5 : 23 13 27 33 51 57 66 85 j = 0 1 2 3 4 5 6 7 Pass 6 : 13 23 27 33 51 57 66 85 j = 0 1 2 3 4 5 6 7 Hence the array is sorted in 6 passes. We will see one more example Example 2 : Apply Bubble Sort on array A= {5,3,1,9,8,2,4,7}. j =0 5 3 1 9 8 2 4 7 j =1 3 5 1 9 8 2 4 7 j =2 3 1 5 9 8 2 4 7 j =3 3 1 5 9 8 2 4 7 j =4 3 1 5 8 9 2 4 7 j =5 3 1 5 8 2 9 4 7 j =6 3 1 5 8 2 4 9 7 j =7 3 1 5 8 2 4 7 9
  • 17. Pass 2 :i=2 3 1 5 8 2 4 7 9 1 3 5 8 2 4 7 9 1 3 5 8 2 4 7 9 1 3 5 8 2 4 7 9 1 3 5 2 8 4 7 9 1 3 5 2 4 8 7 9 1 3 5 2 4 7 8 9 Pass 3 : 1 3 5 2 4 7 8 9 1 3 5 2 4 7 8 9 1 3 5 2 4 7 8 9 1 3 2 5 4 7 8 9 1 3 2 4 5 7 8 9 Pass 4 : 1 3 2 4 5 7 8 9 1 3 2 4 5 7 8 9 1 2 3 4 5 7 8 9 This array gets sorted in 4 passes only. Example : Apply Bubble Sort on array A= {23,78,45,8,32,56} Pass 1 : 23 78 45 8 32 56 23 78 45 8 32 56 23 45 78 8 32 56 23 45 8 78 32 56 23 45 8 32 78 56 23 45 8 32 56 78
  • 18. Pass 2 : 23 45 8 32 56 78 23 45 8 32 56 78 23 8 45 32 56 78 23 8 32 45 56 78 Pass 3 : 23 8 32 45 56 78 8 23 32 45 56 78 This array is sorted in 3 passes. P3: Write a Program for Bubble sort in C #include <stdio.h> #include<conio.h> voidbubble_sort(int A[], int n); int main() { int A[100], n, i, j, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter the elementsn", ); for (i = 0; i< n; i++) scanf("%d", &A[i]);
  • 19. bubble_sort(A, n); printf("Sorted list in ascending order:n"); for ( i = 0 ; i< n ; i++ ) printf("%ldn", A[i]); return 0; } voidbubble_sort(int A[], int n) { inti, j, t; for (i = 0 ; i< ( n – 1 ); i++) { for (j = 0 ; j < (n – i – 1); j++) { if (A[j] > list[j+1]) { /* Swapping */ t = A[j]; A[j] = A[j+1]; A[j+1] = t; } } }
  • 20. } Complexity Bubble sort: Suppose we have a list with n elements, and each element perform n – 1 comparisons with elements to its left, and swap, if necessary. Best Case: If the list is already sorted, only one iteration is performed and complexity is O(1). Average and Worst case: For n elements at most n – 1 swap operations is performed in each pass. The worst and average case complexity is O(n2). Selection Sort Assume we want to sort list in ascending order. Selection sort finds the smallest element from unsorted list and swap it with the element in first position. Then it finds second smallest element and swap it with element at second position. This process is repeated till entire list is sorted in ascending order. Each time we swap elements, we say that we have completed a sort pass. A list of n elements requires n–1 passes to completely rearrange the data. Procedure for every pass is as follows. Pass 1 : Find the position P of the smallest in the list of N elements A[l], A[2], . . . , A[N], and then interchange A[P] and A[1] . Then A[1] is sorted. Pass 2 : Find the position P of the smallest in the sublist of N –1 elements A[2], A[3],. . . , A[N], and then interchange A[P]and A[2]. Then A[l], A[2] is sorted. Pass 3 : Find the position P of the smallest in the sublist of N–2 elements A[3], A[4], . . . , A[N], and then interchange A[P] and A[3]. Then: A[l], A[2] , A[3] is sorted. Pass N –1 :Find the position P of the smaller of the elements A[N –1), A[N], and then interchange A[P] and A[N–1]. Then: A[l], A[2], . . . , A[N] is sorted. Thus A is sorted after N –1 passes. Example 1 : 25 79 41 9 34 60 Fig. 4.2 : Original List We will apply selection sort on this list.
  • 21. Unsorted sublist has 25 as a smallest element and 79 is located at second position. Hence we swap 25 and 79.
  • 23. 9 25 34 41 60 79 Thus list is sorted. We will see one more example. Apply selection sort on array A= {77,30,40,10,88,20,65,56}
  • 24. Function for Selection Sort voidselectionSort(int A[], int n) { inti, j, s, temp; for (i= 0; i<= n; i ++) { s = i;
  • 25. for (j=i+1; j <= n; j++) if(A[j] < A[s]) s= j; // Smallest selected; swap with current element temp = A[i]; A[i] = A[s]; A[s] = temp; } P4 : Program to perform Selection Sort. #include <stdio.h> int main() { int A[100], n, i, j, s, temp; /* n=total no. of elements in array s= smallest element in unsorted array temp is used for swapping */ printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for ( i = 0 ; i< n ; i++ ) scanf("%d", &A[i]);
  • 26. for ( i = 0 ; i< ( n – 1 ) ; i++ ) { s = i; for ( j = i + 1 ; j < n ; j++ ) { if ( A[s] > A[j] ) s = j; } if ( s != i ) { temp = A[i]; A[i] = A[s]; A[s] = temp; } } printf("Sorted list in ascending order:n"); for ( i = 0 ; i< n ; i++ ) printf("%dn", A[i]); return 0; } Output: Enter number of elements
  • 27. 5 Enter 5 integers 4 1 7 5 9 Sorted list in ascending order 1 4 5 7 9 Complexity of Selection Sort : In selection sort outer for loop is executed n–1 times. On the kth time through the outer loop, initially the sorted list holds k–1 elements and unsorted portion holds n–k+1 elements. In inner for loop 2 elements are compared each time. Thus, 2*(n–k) elements are examined by the inner loop during the k th pass through the outer loop. But k ranges from 1 to n–1. Total number of elements examined is: T(n) = 2*(n –1) + 2*(n–2) + 2*(n–3) + .. + 2*(n–(n–2)) + 2*(n–(n–1))
  • 28. = 2*((n–1) + (n–2) + (n–3) + ... + 2 + 1) (or 2*(sum of first n–1 integers) = 2*((n–1)*n)/2) = n2 – n, so complexity of algorithm is O(n2 ). Insertion Sort Insertion Sort reads all elements from 1 to n and inserts each element at its proper position. This sorting method works well on small list of elements. Procedure for each pass is as follows. Pass 1 : A[l] by itself is trivially sorted. Pass 2 : A[2] is inserted either before or after A[l] so that: A[l], A[2] is sorted. Pass 3 : A[3] is inserted into its proper place so that: A[l], A[2], A[3] is sorted. Pass 4 : A[4] is inserted into its proper place A[l], A[2], A[3], A[4] is sorted. Pass N : A[N] is inserted into its proper place in so that: A[l], A[ 2 ] , . . . , A[ N ] is sorted Example :We will apply insertion sort on original list of Fig.3.Assume we are arranging elements in ascending order. 25 79 41 9 34 60 Fig. 4.3 : Original List
  • 33. C Function for Insertion Sort voidinsertionSort(int A[], int n) { inti, p, temp, j; for (i = 1; i<=n; i++) { p= 0; temp = A[i]; for (j=i–1; j >= 0 && !p;) if(temp < A[j]) { A[j + 1]= A[j]; j––;
  • 34. } else p = 1; A[j + 1] = temp; } return; } P5 : Program to implement insertion sort. #include <stdio.h> int main() { int n, A[100], i, j, t; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (i = 0; i< n; i++) { scanf("%d", &A[i]); } for (i = 1 ; i<=( n – 1); i++) { j = i; while ( j > 0 && A[j] < A[j–1])
  • 35. { t = A[j]; A[j] = A[j–1]; A[j–1] = t; j––; } } printf("Sorted list in ascending order:n"); for (i = 0; i<= n – 1; i++) { printf("%dn", A[i]); } return 0; } Output : Enter number of elements 5 Enter 5 integers 9 4 2 5 3
  • 36. Sorted list in ascending order 2 3 4 5 9 Complexity of Insertion Sort : Worst Case Complexity: In the worst case, for each i we do (i – 1) swaps inside the inner for–loop–therefore, overall number of swaps (when i goes from 2 to n in the outer for loop) is T(n) = 1 + 2 + 3 +...+(I – 1) +....+ n – 1 = n  (n – 1)/2 = O(n2 ) Average case Complexity is O(n2 ). Best Case Complexity is O(n). 5.3 Finding Roots Of Equations Algorithm: Step 1: Start Step 2: Read a,b,c Step 3: Initialize d <- (b*b) – (4*a*c) Step 4: Initialize r<- b/2*a Step 5: if d>0 go to step 6
  • 37. Step 6: r1 = r+(sqrt(d)/2*a) and r2 = r-(sqrt(d)/2*a) Step 7: print roots are real and distinct first root r1, second root r2 Step 8: if d=0 go to step 9 else go to step 10 Step 9: print roots are real and equal -r Step 10 : d= -d Step 11: im = sqrt(d)/2*a Step 12: print roots are imaginary, first root r+iim, second root r-iim Step 13: stop Program: void main() { float a,b,c,r,d,r1,r2,im; clrscr(); printf(“nt Quadratic Equtionnn Enter the coefficientsn”); scanf(“%f%f%f”, &a,&b,&c); d= (b*b) –(4*a*c); r=b/2*a if(d>0) { r 1 = -r + (sqrt (d)/2*a) r2 = -r - + (sqrt (d)/2*a) printf(“n Roots are real and distinct nn first roott: %.2fnn second roott:%.2f n”,r1,r2);
  • 38. } else if(d==0) printf(“n Roots are real and equal nn roots =:%.2f,-r); } else { d=-d; im=sqrt(d)/2*a; printf(“nRoots are imaginary nnfirst roott;%.2fnnsecond roott:%.2f-i %.2f”,r,im,r,im); } getch(); } 5.4 Notion Of Order Of Complexity Through Example Programs (No Formal Definition Required) To express the running time complexity of algorithm three asymptotic notations are available. Asymptotic notations are also called as asymptotic bounds. Notations are used to relate the growth of complex function with the simple function. Asymptotic notations have domain of natural numbers. These notations are as follows 1. Big-oh notation: Big-oh notations can be express by using ‘o’ symbol. This notation indicates the maximum number of steps required to solve the problem. This notation expresses the worst case growth of an algorithm. Consider the following diagram in which there are two functions f(x) and g(x). f(x) is more complex than the function g(x).
  • 39. Fig. 3.7 In the above diagram out of two function f(n) and g(n), more complex function is f(n), so need to relate its growth as compare to simple function g(n). More specifically we need one constant function c(n) which bound function f(n) at some point n0. Beyond the point ‘n0’ function c*g(n) is always greater than f(n). Now f(n)=Og(n)if there is some constant ‘c’ and some initial value ‘n0’. such that f(n)<=c*g(n) for all n>n0 In the above expression ‘n’ represents the input size f(n) represents the time that algorithm will take f(n) and g(n) are non-negative functions. Consider the following example
  • 40. Fig. 3.8 In the above example g(n) is ‘’n’ and f(n) is ‘2n+1’ and value of constant is 3. The point where c*g(n) and f(n) intersect is ‘’n0’. Beyond ‘n0’ c*g(n) is always greater than f(n) for constant ‘3’. We can write the conclusion as follows f(n)=O g(n) for constant ‘c’ =3 and ‘n0’=1 such that f(n)<=c*g(n) for all n>n0 2. Big-omega notation: Big-omega notations can be express by using ‘Ω’ symbol. This notation indicates the minimum number of steps required to solve the problem. This notation expresses the best case growth of an algorithm. Consider the following diagram in which there are two functions f(n) and g(n). f(n) is more complex than the function g(n).
  • 41. Fig. 3.9 In the above diagram out of two function f(n) and g(n), more complex function is f(n), so need to relate its growth as compare to simple function g(n). More specifically we need one constant function c(n) which bound function f(n) at some point n0. Beyond the point ‘n0’ function c*g(n) is always smaller than f(n). Now f(n)=Ωg(n)if there is some constant ‘c’ and some initial value ‘n0’. such that c*g(n) <= f(n)for all n>n0 In the above expression ‘n’ represents the input size f(n) represents the time that algorithm will take f(n) and g(n) are non-negative functions. Consider the following example In the above example g(n) is ‘2n’ and f(n) is ‘2n-2’ and value of constant is 0.5. The point where c*g(n) and f(n) intersect is ‘’n0’. Beyond ‘n0’ c*g(n) is always smaller than f(n) for constant ‘0.5’. We can write the conclusion as follows f(n)=Ω g(n) for constant ‘c’ =0.5 and ‘n0’=2 such that c*g(n)<= f(n) for all n>n0
  • 42. Fig. 3.10 Big-theta notation: Big-theta notations can be express by using ‘Ɵ’ symbol. This notation indicates the exact number of steps required to solve the problem. This notation expresses the average case growth of an algorithm. Consider the following diagram in which there are two functions f(n) and g(n). f(n) is more complex than the function g(n). Fig. 3.11
  • 43. In the above diagram out of two function f(n) and g(n), more complex function is f(n), so need to relate its growth as compare to simple function g(n). More specifically we need one constant function c1 and c2 which bound function f(n) at some point n0. Beyond the point ‘n0’ function c1*g(n) is always smaller than f(n) and c2*g(n) is always greater than f(n). Now f(n)= g(n) if there is some constant ‘c1 and c2’ and some initial value ‘n0’. such that c1*g(n) <= f(n)<=c2*g(n)for all n>n0 In the above expression ‘n’ represents the input size f(n) represents the time that algorithm will take f(n) and g(n) are non-negative functions. f(n)= g(n) if and only if f(n)=O g(n) and f(n)=Ω g(n) Consider the following example Fig. 3.12 In the above example g(n) is ‘2n’ and f(n) is ‘3n-2’ and value of constant c1 is 0.5 and c2 is 2. The point where c*g(n) and f(n) intersect is ‘’n0’. Beyond ‘n0’ c1*g(n) is always smaller than f(n) for constant ‘0.5’ and c2*g(n) is always greater than f(n) for constant ‘2’. We can write the conclusion as follows
  • 44. f(n)=Ω g(n) for constant ‘c1’ =0.5 , ‘c2’ = 2 and ‘n0’=2 such that c1*g(n)<= f(n)<=c2*g(n) for all n>n0