Data Structure
Data Structure
Data Structure
language.
The aim has been to design good programs, where a good
program is defined as a program that
runs correctly
is easy to read and understand
is easy to debug
is easy to modify
Inserting: It is used to add new data items to the given list of data
items.
3 integer variables are used. Lets assume the size as 4 bytes. So, the
total space occupied by the above-given program is 4 * 3 = 12 bytes.
Hence, space complexity for the above-given program is O(1), or
constant as there is no dependency on variable term n which is size
of input instance.
Consider the following code:
#include <stdio.h>
int main()
{
int n, i, sum = 0;
scanf("%d", &n);
int arr[n];
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
printf("%d", sum);
}
The array consists of n integer elements. So, the space occupied by the array is
4 * n. Also we have integer variables such as n, i and sum. Assuming 4 bytes for
each variable, the total space occupied by the program is 4n + 12 bytes. Since
the highest order of n in the equation 4n + 12 is n, so the space complexity is
O(n) or linear.
Time complexity of an algorithm is the amount of time requires to
run to completion.
Generally, T(x)=C(x)+I(x) where, C(x) is compile time and I(x) is run
time.
Example:-
int count = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < i; j++)
count++;
It eliminates half of the list from further searching by using the result of each
comparison.
It indicates whether the element being searched is before or after the current
position in the list.
This information is used to narrow the search.
For large lists of data, it works significantly better than linear search.