0% found this document useful (0 votes)
11 views

Introduction to Algorithms

Uploaded by

pethu.tndalu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Introduction to Algorithms

Uploaded by

pethu.tndalu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Introduction to Algorithms

Algorithms
An algorithm is “a finite set of precise instructions for
performing a computation or for solving a problem”
– A program is one type of algorithm
• All programs are algorithms
• Not all algorithms are programs!
– Directions to somebody’s house is an algorithm
– A recipe for cooking a cake is an algorithm
– The steps to compute the factorial of a given
number is an algorithm
Properties of an Algorithm
Algorithms generally share a set of properties:
Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one
meaning.
Input − An algorithm should have 0 or more well-defined
inputs.
Output − An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
Definiteness: the steps are defined precisely
Correctness: should produce the correct output
Finiteness: the steps required should be finite
Effectiveness: each step must be able to be performed in a
finite amount of time
Algorithm Complexity
Time complexity is a type of computational complexity that
describes the time required to execute an algorithm.
The time complexity of an algorithm is the amount of time it
takes for each statement to complete.
As a result, it is highly dependent on the size of the processed
data.
It also aids in defining an algorithm's effectiveness and
evaluating its performance.
Space Complexity
The amount of memory used by a program to execute it is
represented by its space complexity. Because a program
requires memory to store input data and temporal values while
running.
Asymptotic Notations
Asymptotic notations are the mathematical notations used to
describe the running time of an algorithm when the input tends
towards a particular value or a limiting value.
Big-O Notation (O-notation) :Big-O notation represents the upper
bound of the running time of an algorithm. Thus, it gives the worst-
case complexity of an algorithm.
Omega Notation (Ω-notation) :Omega notation represents the
lower bound of the running time of an algorithm. Thus, it provides
the best case complexity of an algorithm.
Theta Notation (Θ-notation) :Theta notation encloses the function
from above and below. Since it represents the upper and the lower
bound of the running time of an algorithm, it is used for analyzing
the average-case complexity of an algorithm.
Asymptotic Notations
Asymptotic notations are the mathematical notations used to
describe the running time of an algorithm when the input tends
towards a particular value or a limiting value.
Big-O Notation (O-notation) :Big-O notation represents the upper
bound of the running time of an algorithm.
Omega Notation (Ω-notation) :Omega notation represents the
lower bound of the running time of an algorithm.
Theta Notation (Θ-notation) :Theta notation encloses the function
from above and below. Since it represents the upper and the lower
bound of the running time of an algorithm,
O-notation

O(g(n)) = {f(n) :
 positive constants c and n0, such that n  n0,

we have 0  f(n)  cg(n) }


g(n) is an asymptotic upper bound for f(n).
 -notation

(g(n)) = {f(n) :
 positive constants c and n0, such that n 
n0,
we have 0  cg(n)  f(n)}
g(n) is an asymptotic LOWER bound for f(n).
-notation

(g(n)) = {f(n) :  positive constants c1, c2, and n0,


such that n  n0, we have 0  c1g(n)  f(n)  c2g(n)
}
g(n) is an asymptotically tight bound for f(n).
Example
f(n)=2n+3
f(n)=O(n)
f(n)=(n)
f(n)=(n)
Sorting
Sorting Algorithms
Sorting Algorithms are methods of reorganizing a
large number of items into some specific order
such as highest to lowest, or vice-versa.
Types :
• Bubble Sort
• Selection Sort
• Insertion Sort
• Merge Sort
Bubble Sorting
Bubble sort works by comparing and swapping
adjacent elements in an array.

• Starts from the first index: arr[0] and compares the


first and second element: arr[0] and arr[1]
• If arr[0] is greater than arr[1], they are swapped
• Similarly, if arr[1] is greater than arr[2], they are
swapped
• The above process continues until the last element
arr[n-1]
Program- Bubble Sort

void bubble_sort(int x[], int n)


{
int i,j,temp;
for (i=0; i<=n-1; i++)
for (j=i; j<n; j++)
if (x[j] > x[j+1])
{
temp=x[j];
x[j]=x[j+1];
x[j+1]==temp;
}
}
Selection Sort
The selection sort algorithm sorts an array by
repeatedly finding the minimum element ) from
unsorted list and putting it at the beginning. The
algorithm maintains two subarrays in a given array.
• The subarray which is already sorted.
• Remaining subarray which is unsorted.
Selection Sort - Example
x: 3 12 -5 6 142 21 -17 45 x: -17 -5 3 6 12 21 142 45

x: -17 12 -5 6 142 21 3 45 x: -17 -5 3 6 12 21 45 142

x: -17 -5 12 6 142 21 3 45 x: -17 -5 3 6 12 21 45 142

x: -17 -5 3 6 142 21 12 45

x: -17 -5 3 6 142 21 12 45

x: -17 -5 3 6 12 21 14245
Selection Sort
/*Location of smallest element */

int findMinLloc (int x[ ], int k, int size)


{
int j, pos;
pos = k;
for (j=k+1; j<size; j++)
if (x[j] < x[pos])
pos = j;
return pos;
}
Selection Sort

int selectionSort (int x[], int size)


{ int k, m;
for (k=0; k<size-1; k++)
{
m = findMinLoc(x, k, size);
temp = a[k];
a[k] = a[m];
a[m] = temp;
}
}
Insertion Sort
• Insertion Sort works in a similar manner as we
arrange a deck of cards.
• In each iteration, insertion sort compares the current
element with the next element and determines
whether the current element is greater than the one it
was compared to.
• If this is true, then it leaves the element in its place
and moves on to the next element. If it is false, then it
finds its correct position in the sorted array and
moves it to that position by shifting all the elements
which are larger in the sorted array to one position
ahead.
void insertion_sort ( int A[ ] , int n)
{
for( int i = 0 ;i < n ; i++ ) {
/*storing current element whose left side is checked for its
correct position .*/
int temp = A[ i ];
int j = i;
/* check whether the adjacent element in left side is greater
or less than the current element. */

while( j > 0 && temp < A[ j -1])


{
// moving the left side element to one position forward.
A[ j ] = A[ j-1];
j= j - 1;
}
// moving current element to its correct position.
A[ j ] = temp; }}
Merge Sort
Merge sort is divide and conquer approach to
sort the elements. It is one of the most popular
and efficient sorting algorithm. It divides the
given list into two equal halves, calls itself for
the two halves and then merges the two sorted
halves.
Merge Sort
Searching
Linear Search

Linear search is a sequential searching


algorithm where the user can start from one
end and check every element of the list until
the desired element is found. It is the
simplest searching algorithm.
Sequential Search with Array
int main()
{
int A[10], i, n, K, flag = 0;
printf("Enter the size of an array: ");
scanf("%d",&n);

printf("Enter the elements of the array: ");


for(i=0; i < n; i++)
scanf("%d",&A[i]);
printf("Enter the number to be searched: ");
scanf("%d",&K);
for(i=0;i<n;i++){
if(a[i] == K){
flag = 1; break;
}
}
if(flag == 0)
printf("The number is not in the list");
else
printf("The number is found at index %d",i);
return 0;
}
Binary Search
• Binary search works only on a sorted set of
elements. To use binary search on a
collection, the collection must first be
sorted.

• When binary search is used to perform


operations on a sorted set, the number of
iterations can always be reduced on the
basis of the value that is being searched.
Binary Search
• Let's see how the number of iterations can be
reduced by using binary search.
• Before we start the search, the user needs to
know the start and end of the range.
• starting index as low = 0 and ending index as
High = n-1
• middle=(low+high)/2
• Now, compare the search value with the
element located at the middle.If the value is
greater, increase the lower bound ie) low=mid+1,
else decrease the upper bound ie) high=mid-1.
Binary Search

Example: sorted array of integer keys.


Target=7.
[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53
Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 = midpoint key? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Is 7 < midpoint key? YES.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area before midpoint.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Find approximate midpoint


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target = key of midpoint? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target < key of midpoint? NO.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Target > key of midpoint? YES.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5] [6]

3 6 7 11 32 33 53

Search for the target in the area after midpoint.


Binary Search

Example: sorted array of integer keys. Target=7.

[0] [1] [2] [3] [4] [5]

3 6 7 11 32 33

Find approximate midpoint.


Is target = midpoint key? YES.
Binary Search (with Recursion)
int binary(int a[],int n,int K,int low,int high){

int mid;

if(low<=high){
mid=(low+high)/2;
if(K==a[mid]){
return(1);
}
else if(k<a[mid]){
return binarySearch(a,n,K,low,mid-1);
}
else
return binarySearch(a,n,k,mid+1,high);
}
else return(0);
}

You might also like