Module 1
Module 1
• For any value of n, the running time of an algorithm does not cross
the time provided by O(g(n)).
Since it gives the worst-case running time of an algorithm, it is
widely used to analyze an algorithm as we are always interested in
the worst-case scenario.
and c such that it can be sandwiched between c g(n) and c g(n), for sufficiently large n.
2 1 2
If a function f(n) lies anywhere in between c g(n) and c g(n) for all n ≥ n0, then f(n) is said to
1 2
Multidimensional arrays
A multidimensional array associates each element in the
array with multiple indexes.
The most commonly used multidimensional array is
the two-dimensional array, also known as
a table or matrix. A two-dimensional array associates
each of its elements with two indexes.
2]={0,1,2,3,4,5,6,7,8,9,3,2}
In this type of declaration,
we have an array of type integer,
block size is 3,
row size is 2, column size is 2 and we have mentioned the values
inside the curly braces during the declaration of array.
So all the values will be stored one by one in the array cells.
int arr[3][2][2]={0,1,2,3,4,5,6,7,8,9,3,2}
1.malloc()
2.calloc()
3.free()
4.realloc()
The “malloc” or “memory allocation” method in C is used to
dynamically allocate a single large block of memory with the specified
size.
It returns a pointer of type void which can be cast into a pointer of any
form. It doesn’t Initialize memory at execution time so that it has
initialized each block with the default garbage value initially.
Syntax:
ptr = (cast-type*) malloc(byte-size)
For Example:
ptr = (int*) malloc(100 * sizeof(int));
Since the size of int is 4 bytes, this statement will allocate 400
bytes of memory.
And, the pointer ptr holds the address of the first byte in the
allocated memory.
C calloc() method
1.“calloc” or “contiguous allocation” method in C is
used to dynamically allocate the specified number of
blocks of memory of the specified type. it is very much
similar to malloc() but has two different points and these
are:
2.It initializes each block with a default value ‘0’.
3.It has two parameters or arguments as compare to
malloc().
Syntax:
ptr = (cast-type*)calloc(n, element-size);
here, n is the no. of elements and element-
size is the size of each element.
For Example:
Syntax:
free(ptr);
• “realloc” or “re-allocation” method in C is used to
dynamically change the memory allocation of a previously
allocated memory.
• In other words, realloc can be used to dynamically re-allocate
memory. re-allocation of memory maintains the already
present value and new blocks will be initialized with the default
garbage value.
Syntax:
ptr = realloc(ptr, newSize); where ptr is
reallocated with new size 'newSize'.
Basic Operations:
Following are the basic operations supported by an array.
•Traverse − print all the array elements one by one.
•Insertion − Adds an element at the given index.
•Deletion − Deletes an element at the given index.
•Search − Searches an element using the given index or by
the value.
•Update − Updates an element at the given index.