Primitive Operations
Primitive operations are the building blocks of the algorithm in the Data Structures. It help us to count the operations in any certain algorithm to calculate the time complexity of the algorithm.What is Data Structure?
Data Structure is the systematic way of accessing and organizing data.What is Algorithm?
Algorithm is the step by step procedure to perform the task in the finite amount of time.You might be thinking why is there a need to calculate the time complexity of the algorithm. Technically talking being a Computer Science student, it is too much necessary to calculate the run time to check the efficiency of the algorithm.
The analysis is performed on the two basic factors that are:
- Run time
- Space Usage
There are two types of the analysis:
- Experimental Analysis
- Theoretical Analysis
Experimental analysis is performed on the finite amount of data while the theoretical analysis is performed on all types of data so we can easily calculate the worst time complexity of the algorithm.
There are three types of complexities:
- Best Time Complexity
- Worst Time Complexity
- Average Time Complexity
(The details of these time complexities will be shared with you in further blogs.)
Here are some of the rules according to which we can count the primitive operator in the algorithm:
- Assigning the value to the variable.
- Calling a method.
- Performing an arithmetic operation.
- Comparing two numbers.
- Indexing into an Array.
- Returning from a method.
Assigning the value to the variable
Lets see the example of assigning the variable.
int x=5;
x is defined as the variable and an integer 5 is assigned to x. This whole process in counted as ONE primitive operator.
Calling a method
Calling a method or a function is counted as ONE primitive operator. For example there is any function defined outside the main class.
public int Calculate(int x,int y);//Adding two numbers
The purpose of this function is to calculate the sum of two integers and return the sum. Now if we call this function in main the instruction would somehow look like this.
int sum=Calculate(5,4);
As the function is returning the integer so it is called by the variable of integer type. This instruction is counted as one primitive operator.
Performing an arithmetic operation
Adding,Subtracting,Multiplication and Division is counted as ONE primitive operator.For example there is an instruction saying that a+b*c/d. Immediately you can say that there four operators in the instruction, so the primitive count should be four. NO.
No matter how many operators are there in a single instruction the primitive count will always be counted as one for that instruction.
Comparing two numbers
If you are comparing two number lets say the instruction is saying that 8>2. The comparison between the number is done in this instruction. So the primitive operator for this step is counted as ONE.
Indexing into an Array
For example there is an array. The first position of the array is counted as 0th position. We can move on to next position by adding 1 and so on like array[0],array[1],array[2] up to the size of the array. This indexing is counted as ONE primitive operator.Returning from a method
As described earlier if there is any function taking any parameters and returning any certain value. This returning of value from the function is counted as ONE primitive operator.
You can subscribe to my YouTube channel and watch the examples of calculating the time complexity of the algorithm.
https://www.youtube.com/watch?v=cBi5K8X4X2Y
https://www.youtube.com/watch?v=cBi5K8X4X2Y
Comments
Post a Comment