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

Algorithm, ADT and Arrays

The document provides an overview of algorithms, including their definition, properties, and efficiency analysis in terms of time and space complexity. It also discusses abstract data types (ADTs) and data structures like arrays, along with examples of algorithms for finding even/odd numbers and the smallest element in an array. Additionally, it highlights the importance of understanding the operations and implementations of data types while focusing on their abstract behavior.

Uploaded by

Angat Roshan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm, ADT and Arrays

The document provides an overview of algorithms, including their definition, properties, and efficiency analysis in terms of time and space complexity. It also discusses abstract data types (ADTs) and data structures like arrays, along with examples of algorithms for finding even/odd numbers and the smallest element in an array. Additionally, it highlights the importance of understanding the operations and implementations of data types while focusing on their abstract behavior.

Uploaded by

Angat Roshan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Algorithm

• An “algorithm" is a formally defined procedure for performing some calculation. It provides a blueprint to write a
program to solve a particular problem.

• It is considered to be an effective procedure for solving a problem in finite number of steps. That is, a well-defined
algorithm always provides an answer and is guaranteed to terminate.
• Algorithms are mainly used to achieve software re-use. Once we have an idea or a blueprint of a solution, we can
implement it in any high level language like C, C++, Java, so on and so forth.

Write an algorithm to find whether a number is even or odd


Properties of Algorithm
Step 1: Input the first number as A
Step 2: IF A%2 =0 Input
Output
Then Print "EVEN"
Finiteness
ELSE Definiteness
Order of sequence
PRINT "ODD"
Correctness
Step 3: END
Time and Space Complexity of Algorithm
• To analyze an algorithm means determining the amount of resources (such as time and storage) needed to
execute it.
• Efficiency or complexity of an algorithm is stated in terms of time complexity and space complexity. Asymptotic
notation is used to define time complexity.
• Time complexity of an algorithm is basically the running time of the program as a function of the input size.

• Similarly, space complexity of an algorithm is the amount of computer memory required during the program
execution, as a function of the input size.
Calculating Algorithm Efficiency
• The efficiency of an algorithm is expressed in terms of the number of elements that has to be processed. So, if n is the
number of elements, then the efficiency can be stated as Efficiency = f(n).

• If a function is linear (without any loops or recursions), the efficiency of that algorithm or the running time of that
algorithm can be given as the number of instructions it contains.
• If an algorithm contains certain loops or recursive functions then its efficiency may vary depending on the number of
loops and the running time of each loop in the algorithm.

Time complexity hierarchy

Minimum to
maximum
Calculating Algorithm Efficiency
Linear loops
for(i=0; i<n; i++)
statement block

f(n) = n or Order of n
Logarithmic Loops
for(i=1;i<1000;i*=2) for(i=1000;i>=1;i/=2)
statement block; Iterates 9 times statement block;
log21000 = 9.96 approx. 9
f(n) = log n or Order of log n

Nested Loops
Linear logarithmic
for(i=0;i<10;i++) for(i=0; i<n; i++)
for(j=1; j<1000;j*=2) for(j=0; j<n; j++)
statement block; statement block;

f(n)= n log n or Oder of nlogn f(n)= n2


Matrix Addition Matrix Multiplication

for(i=0; i<n; i++) for(j=0; j<n; j++) for(i=0; i<n; i++) for(j=0; j<n; j++)
read matrix A[i][j]; read matrix A[i][j];

for(i=0; i<n; i++) for(j=0; j<n; j++) for(i=0; i<n; i++) for(j=0; j<n; j++)
read matrix B[i][j]; read matrix B[i][j];

for(i=0; i<n; i++) for(j=0; j<n; j++) for(i=0; i<n; i++) for(j=0; j<n; j++)
C[i][j] = A[i][j] + B[i][j]; C[i][j] = 0;
for(k=0;K<n;k++)
C[i][j] = C[i][j] + A[i][K]*B[k][j];
for(i=0; i<n; i++) for(j=0; j<n; j++)
print matrix C[i][j];
for(i=0; i<n; i++) for(j=0; j<n; j++)
print matrix C[i][j];

Time Complexity: O(n2) Time Complexity: O(n3)


Calculating Algorithm Efficiency for recursive program

func factorial(n)
int i, fact=1; Time Complexity is Order of n
for(i=1;i<=n; i++) Recurrence Relation
fact=fact*i;
return fact; T(n) = a, n=0 or n=1
T(n) = T(n-1) +b n>1
where a and b are constants

func factorial(n) T(n) = T(n-2)+b+b


if (n == 0 || n == 1) T(n) = T(n-2)+2b
return 1
return n * factorial (n - 1) T(n) = T(n-3)+3b
T(n) = T(n-4)+4b
:
•factorial(0) or factorial(1) is only comparison (1 unit of time) :
•factorial(n) is 1 comparison, 1 multiplication, 1 subtraction and T(n) = T(n-(n-1))+(n-1)b
time for factorial(n-1) T(n) = T(1)+(n-1)b => T(n) = a+(n-1)b
So, T(n) is Order of n.
func factorial(n) T(n) = Time taken to execute the program of size n
if (n == 0 || n == 1) T(n-1) = Time taken to execute the program of size n-1
return 1
return n * factorial (n - 1) T(2) = Time taken to execute the program of size 2
T(1) = Time taken to execute the program of size 1, which is constant
factorial(n) will complete iff factorial(n-1) will complete
factorial(n-1) will complete iff factorial(n-2) will complete
factorial(n-2) will complete iff factorial(n-3) will complete If T(n) = T(n-1) +b …..(1), then T(n-1) = T(n-2) +b
: put the value of T(n-1) in 1
: T(n) = T(n-2)+b+b
. T(n) = T(n-2)+2b represent T(n-2) with the help of T(n-3)
T(n) = T(n-3)+3b
T(n) = T(n-4)+4b
Recurrence Relation
:
:
T(n) = a, n=0 or n=1
T(n) = T(n-(n-1))+(n-1)b
T(n) = T(n-1) +b n>1
T(n) = T(1)+(n-1)b => T(n) = a+(n-1)b
where a and b are constants
So, T(n) is Order of n.
Abstract Data Type (ADT)
An abstract data type (ADT) is a theoretical or mathematical concept for data types. It’s a special kind of data
type, whose behavior is defined by a set of values and set of operations. The keyword “Abstract” because how
those operations are working and implemented that is totally hidden from the user. The ADT is made of with
primitive data types, but operation logics are hidden.

When we implement stack, it will be a Data Structure.


We we represent stach as a mathematical model, it will be ADT
Abstract Data Type (ADT)

Some examples of ADT are Stack, Queue etc.


Let us see some operations of those mentioned ADT −
•Stack −
• isFull(), This is used to check whether stack is full or not
• isEmpry(), This is used to check whether stack is empty or not
• push(x), This is used to push x into the stack
• pop(), This is used to delete one element from top of the stack
• peek(), This is used to get the top most element of the stack
• size(), this function is used to get number of elements present into the stack
•Queue −
• isFull(), This is used to check whether queue is full or not
• isEmpry(), This is used to check whether queue is empty or not
• insert(x), This is used to add x into the queue at the rear end
• delete(), This is used to delete one element from the front end of the queue
• size(), this function is used to get number of elements present into the queue
An abstract data type (ADT) is the way we look at a data structure, focusing on what it does and ignoring how it
does its job. For example, stacks and queues are perfect examples of an ADT. We can implement both these
ADTs using an array or a linked list. This demonstrates the ‘abstract’ nature of stacks and queues.
To further understand the meaning of an abstract data type, we will break the term into ‘data type’ and ‘abstract’,
and then discuss their meanings.
Data type Data type of a variable a programming language is the set of values that the variable can take.
When we talk about a primitive type (built-in data type), we actually consider two things: a data item with certain
characteristics and the permissible operations on that data. For example, an int variable can contain any
whole-number value from –32768 to 32767 and can be operated with the operators +, –, *, and /. In other words,
the operations that can be performed on a data type are an inseparable part of its identity. Therefore, when we
declare a variable of an abstract data type (e.g., stack or a queue), we also need to specify the operations that can
be performed on it.
Abstract The word ‘abstract’ in the context of data structures means considered apart from the detailed
specifications or implementation.
The end-user is not concerned about the details of how the methods carry out their tasks. They are only aware of
the methods that are available to them and are only concerned about calling those methods and getting the results.
They are not concerned about how they work. For example, when we use a stack or a queue, the user is
concerned only with the type of data and the operations that can be performed on it. Therefore, the fundamentals
of how the data is stored should be invisible to the user. They should not be concerned with how the methods work
or what structures are being used to store the data. They should just know that to work with stacks, they have
push() and pop() functions available to them. Using these functions, they can manipulate the data (insertion or
deletion) stored in the stack.
Data Structures : Arrays
An array is defined as a set of finite number of homogeneous elements or same data items.
It means an array can contain one type of data only, either all integer, all float-point number or all
character.
One dimensional array:
An array with only one row or column is called one-dimensional array.
that:
It is finite collection of n number of elements of same type such
◦ can be referred by indexing.
◦ The syntax Elements are stored in continuous locations.
◦ Elements x to define one-dimensional array is:
Syntax: Datatype Array_Name [Size]; int marks[8]
Where,
Datatype : Type of value it can store (Example: int, char, float)
Array_Name: To identify the array.
Size : The maximum number of elements that the array can hold.

99 67 78 56 88 90 34 85

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


Read and Display N Numbers using an Array
#include<stdio.h>
#include<conio.h>
int main()
{
int i=0, n, num[20];
clrscr();
printf(“\n Enter the number of elements : ”);
scanf(“%d”, &n);
printf(“\n Enter the elements : ”);
for(i=0;i<n;i++)
{
scanf(“%d”, &num[i]);
}
printf(“\n The array elements are ”);
for(i=0;i<n;i++)
printf(“num[%d] = %d\t”, i, num[i]);
return 0;
}
print the position of the smallest number from n numbers using arrays
#include <stdio.h>
int main()
{
int i, n, arr[20], small, pos;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
small = arr[0];
pos =0;
for(i=1;i<n;i++)
{
if(arr[i]<small)
{
small = arr[i];
pos = i;
}
}
printf("\n The smallest element is : %d", small);
printf("\n The position of the smallest element in the array is : %d", pos);
return 0;
}
print the position of the smallest number of n numbers using arrays
#include <stdio.h>
int smallest_array(int [],int);
int main()
{
int i, n, arr[20], pos, result;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements : ");
for(i=0;i<n;i++) int smallest_array(int arr[], int n)
scanf("%d",&arr[i]); {
result = smallest_array(arr, n); int small, i, pos;
printf("\n The smallest element is : %d", result); small = arr[0];
return 0; pos =0;
} for(i=1;i<n;i++)
{
if(arr[i]<small)
{
small = arr[i];
pos = i;
}
}
printf("\n The position of the smallest element in the array is : %d", pos+1);
return small;
}

You might also like