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

What's hot (20)

Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
Iteration, induction, and recursion
Iteration, induction, and recursionIteration, induction, and recursion
Iteration, induction, and recursion
Mohammed Hussein
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
Kamal Acharya
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
C++ Arrays different operations .pdf
C++ Arrays different operations .pdfC++ Arrays different operations .pdf
C++ Arrays different operations .pdf
manahilzulfiqar6
 
Heaps
HeapsHeaps
Heaps
invertis university
 
asymptotic notation
asymptotic notationasymptotic notation
asymptotic notation
SangeethaSasi1
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
String C Programming
String C ProgrammingString C Programming
String C Programming
Prionto Abdullah
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Trupti Agrawal
 
File Management in C
File Management in CFile Management in C
File Management in C
Paurav Shah
 
Algebraic structures
Algebraic structuresAlgebraic structures
Algebraic structures
BindhuBhargaviTalasi
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
International Islamic University
 
c-programming
c-programmingc-programming
c-programming
Zulhazmi Harith
 
Variables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detailVariables, Data Types, Operator & Expression in c in detail
Variables, Data Types, Operator & Expression in c in detail
gourav kottawar
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
Iteration, induction, and recursion
Iteration, induction, and recursionIteration, induction, and recursion
Iteration, induction, and recursion
Mohammed Hussein
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
P M Patil
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
Kamal Acharya
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
RAJ KUMAR
 
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Balwant Gorad
 
C++ Arrays different operations .pdf
C++ Arrays different operations .pdfC++ Arrays different operations .pdf
C++ Arrays different operations .pdf
manahilzulfiqar6
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
File Management in C
File Management in CFile Management in C
File Management in C
Paurav Shah
 
Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)Analysis of Algorithm (Bubblesort and Quicksort)
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 

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)

Searching
SearchingSearching
Searching
A. S. M. Shafi
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
JeoJoyA
 
Searching
Searching Searching
Searching
CGC Technical campus,Mohali
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Presentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxPresentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptx
Krishnanandmishra15
 
Module_1 Linear search search and Bsearch).pptx
Module_1 Linear search search and Bsearch).pptxModule_1 Linear search search and Bsearch).pptx
Module_1 Linear search search and Bsearch).pptx
vidhyapm2
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptxChapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
Searching.ppt
Searching.pptSearching.ppt
Searching.ppt
SpammemeLmao
 
Data structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptxData structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Searching
SearchingSearching
Searching
Muhammad Farhan
 
Unit 2 Searching and Sorting Technique.pptx
Unit 2 Searching and Sorting Technique.pptxUnit 2 Searching and Sorting Technique.pptx
Unit 2 Searching and Sorting Technique.pptx
Vaibhav Parjane
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
 
Searching and Sorting Unit II Part I.pptx
Searching and Sorting Unit II Part I.pptxSearching and Sorting Unit II Part I.pptx
Searching and Sorting Unit II Part I.pptx
Dr. Madhuri Jawale
 
placement preparation for Array Searching.pptx
placement preparation for Array Searching.pptxplacement preparation for Array Searching.pptx
placement preparation for Array Searching.pptx
upasnatiwari10
 
unit 2 First.pptx Searching - Linear and Binary Search
unit 2 First.pptx Searching - Linear and Binary Searchunit 2 First.pptx Searching - Linear and Binary Search
unit 2 First.pptx Searching - Linear and Binary Search
Vaibhav Parjane
 
Linear and binary search
Linear and binary searchLinear and binary search
Linear and binary search
JeoJoyA
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
sajinis3
 
Searching in c language
Searching in c languageSearching in c language
Searching in c language
CHANDAN KUMAR
 
Presentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxPresentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptx
Krishnanandmishra15
 
Module_1 Linear search search and Bsearch).pptx
Module_1 Linear search search and Bsearch).pptxModule_1 Linear search search and Bsearch).pptx
Module_1 Linear search search and Bsearch).pptx
vidhyapm2
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Chapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptxChapter #3 (Searchinmmmg ALgorithm).pptx
Chapter #3 (Searchinmmmg ALgorithm).pptx
hekmatyarzahir44
 
Data structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptxData structure Unit - II Searching and Sorting.pptx
Data structure Unit - II Searching and Sorting.pptx
gavanisanjana
 
Unit 2 Searching and Sorting Technique.pptx
Unit 2 Searching and Sorting Technique.pptxUnit 2 Searching and Sorting Technique.pptx
Unit 2 Searching and Sorting Technique.pptx
Vaibhav Parjane
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
 
Searching and Sorting Unit II Part I.pptx
Searching and Sorting Unit II Part I.pptxSearching and Sorting Unit II Part I.pptx
Searching and Sorting Unit II Part I.pptx
Dr. Madhuri Jawale
 
placement preparation for Array Searching.pptx
placement preparation for Array Searching.pptxplacement preparation for Array Searching.pptx
placement preparation for Array Searching.pptx
upasnatiwari10
 
unit 2 First.pptx Searching - Linear and Binary Search
unit 2 First.pptx Searching - Linear and Binary Searchunit 2 First.pptx Searching - Linear and Binary Search
unit 2 First.pptx Searching - Linear and Binary Search
Vaibhav Parjane
 
Ad

More from Sitamarhi Institute of Technology (20)

STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdfSTET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
Sitamarhi Institute of Technology
 
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdfDeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
DeepSeek vs. ChatGPT - The Battle of AI Titans.pdf
Sitamarhi Institute of Technology
 
METHODS OF CUTTING COPYING HTML BASIC NOTES
METHODS OF CUTTING COPYING HTML BASIC  NOTESMETHODS OF CUTTING COPYING HTML BASIC  NOTES
METHODS OF CUTTING COPYING HTML BASIC NOTES
Sitamarhi Institute of Technology
 
introduction Printer basic notes Hindi and English
introduction Printer basic notes Hindi and Englishintroduction Printer basic notes Hindi and English
introduction Printer basic notes Hindi and English
Sitamarhi Institute of Technology
 
Beginners Guide to Microsoft OneDrive 2024–2025.pdf
Beginners Guide to Microsoft OneDrive 2024–2025.pdfBeginners Guide to Microsoft OneDrive 2024–2025.pdf
Beginners Guide to Microsoft OneDrive 2024–2025.pdf
Sitamarhi Institute of Technology
 
ChatGPT Foundations rompts given for each topic in both personal and business...
ChatGPT Foundations rompts given for each topic in both personal and business...ChatGPT Foundations rompts given for each topic in both personal and business...
ChatGPT Foundations rompts given for each topic in both personal and business...
Sitamarhi Institute of Technology
 
Google Drive Mastery Guide for Beginners.pdf
Google Drive Mastery Guide for Beginners.pdfGoogle Drive Mastery Guide for Beginners.pdf
Google Drive Mastery Guide for Beginners.pdf
Sitamarhi Institute of Technology
 
Chat GPT 1000+ Prompts - Chat GPT Prompts .pdf
Chat GPT 1000+ Prompts - Chat GPT Prompts .pdfChat GPT 1000+ Prompts - Chat GPT Prompts .pdf
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...
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
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
WhatsApp Tricks and Tips - 20th Edition 2024.pdfWhatsApp Tricks and Tips - 20th Edition 2024.pdf
WhatsApp Tricks and Tips - 20th Edition 2024.pdf
Sitamarhi Institute of Technology
 
Mastering ChatGPT for Creative Ideas Generation.pdf
Mastering ChatGPT for Creative Ideas Generation.pdfMastering ChatGPT for Creative Ideas Generation.pdf
Mastering ChatGPT for Creative Ideas Generation.pdf
Sitamarhi Institute of Technology
 
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMARBASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
BASIC COMPUTER CONCEPTSMADE BY: SIR SAROJ KUMAR
Sitamarhi Institute of Technology
 
MS Word tutorial provides basic and advanced concepts of Word.
MS Word tutorial provides basic and advanced concepts of Word.MS Word tutorial provides basic and advanced concepts of Word.
MS Word tutorial provides basic and advanced concepts of Word.
Sitamarhi Institute of Technology
 
BELTRON_PROGRAMMER 2018 and 2019 previous papers
BELTRON_PROGRAMMER 2018 and 2019  previous papersBELTRON_PROGRAMMER 2018 and 2019  previous papers
BELTRON_PROGRAMMER 2018 and 2019 previous papers
Sitamarhi Institute of Technology
 
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
CORPORATE SOCIAL RESPONSIBILITY  CSR) through a presentation by R.K. SahooCORPORATE SOCIAL RESPONSIBILITY  CSR) through a presentation by R.K. Sahoo
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
Sitamarhi Institute of Technology
 
Enhancing-digital-engagement-integrating-storytelling-
Enhancing-digital-engagement-integrating-storytelling-Enhancing-digital-engagement-integrating-storytelling-
Enhancing-digital-engagement-integrating-storytelling-
Sitamarhi Institute of Technology
 
business-with-innovative email-marketing-solution-
business-with-innovative email-marketing-solution-business-with-innovative email-marketing-solution-
business-with-innovative email-marketing-solution-
Sitamarhi Institute of Technology
 
MS Excel Notes PDF in Hindi माइोसॉट एसेल
MS Excel Notes PDF in Hindi माइोसॉट एसेलMS Excel Notes PDF in Hindi माइोसॉट एसेल
MS Excel Notes PDF in Hindi माइोसॉट एसेल
Sitamarhi Institute of Technology
 
beltron-programmer-2023-previous-year-question.pdf
beltron-programmer-2023-previous-year-question.pdfbeltron-programmer-2023-previous-year-question.pdf
beltron-programmer-2023-previous-year-question.pdf
Sitamarhi Institute of Technology
 
Beltron Programmer IGNOU-MCA-NEW-Syllabus.pdf
Beltron Programmer IGNOU-MCA-NEW-Syllabus.pdfBeltron Programmer IGNOU-MCA-NEW-Syllabus.pdf
Beltron Programmer IGNOU-MCA-NEW-Syllabus.pdf
Sitamarhi Institute of Technology
 
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdfSTET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
STET 2025 900+ Computer MCQs in English PDF (studynotes.online).pdf
Sitamarhi Institute of Technology
 
ChatGPT Foundations rompts given for each topic in both personal and business...
ChatGPT Foundations rompts given for each topic in both personal and business...ChatGPT Foundations rompts given for each topic in both personal and business...
ChatGPT Foundations rompts given for each topic in both personal and business...
Sitamarhi Institute of Technology
 
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Smart Phone Film Making.filmmaking but feel limited by the constraints of exp...
Sitamarhi Institute of Technology
 
MS Word tutorial provides basic and advanced concepts of Word.
MS Word tutorial provides basic and advanced concepts of Word.MS Word tutorial provides basic and advanced concepts of Word.
MS Word tutorial provides basic and advanced concepts of Word.
Sitamarhi Institute of Technology
 
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
CORPORATE SOCIAL RESPONSIBILITY  CSR) through a presentation by R.K. SahooCORPORATE SOCIAL RESPONSIBILITY  CSR) through a presentation by R.K. Sahoo
CORPORATE SOCIAL RESPONSIBILITY CSR) through a presentation by R.K. Sahoo
Sitamarhi Institute of Technology
 
MS Excel Notes PDF in Hindi माइोसॉट एसेल
MS Excel Notes PDF in Hindi माइोसॉट एसेलMS Excel Notes PDF in Hindi माइोसॉट एसेल
MS Excel Notes PDF in Hindi माइोसॉट एसेल
Sitamarhi Institute of Technology
 
Ad

Recently uploaded (20)

IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdfIntroSlides-June-GDG-Cloud-Munich community gathering@Netlight.pdf
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quizgrade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSWIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptxFINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
Blood bank management system project report.pdf
Blood bank management system project report.pdfBlood bank management system project report.pdf
Blood bank management system project report.pdf
Kamal Acharya
 
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible PavementsPavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cellsGreat power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdfOCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDSWater demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptxWeek 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quizgrade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSWIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptxFINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
Blood bank management system project report.pdf
Blood bank management system project report.pdfBlood bank management system project report.pdf
Blood bank management system project report.pdf
Kamal Acharya
 
Pavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible PavementsPavement and its types, Application of rigid and Flexible Pavements
Pavement and its types, Application of rigid and Flexible Pavements
Sakthivel M
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cellsGreat power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptxDevelopment of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdfOCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDSWater demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 

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