Lecture#02 Arrays
Lecture#02 Arrays
An array is a collection of items stored at contiguous memory locations. The idea is to store
multiple items of the same type together. This makes it easier to calculate the position of each
element by simply adding an offset to a base value, i.e., the memory location of the first
element of the array (generally denoted by the name of the array).
The above image can be looked as a top-level view of a staircase where you are at the base of the
staircase. Each element can be uniquely identified by their index in the array (in a similar way as
you could identify your friends by the step on which they were on in the above example).
There are various ways in which we can declare an array. It can be done by specifying its type and
size, by initializing it or both.
int arr1[10];
int n = 10;
int arr2[n];
Example:
C
C++
#include <iostream>
using namespace std;
int main()
{
int arr[5];
arr[0] = 5;
arr[2] = -10;
cout << arr[0] << " " << arr[1] << " " << arr[2] << " "
<< arr[3];
return 0;
}
utput
5 2 -10 5
No Index Out of bound Checking:
There is no index out of bounds checking in C/C++, for example, the
following program compiles fine but may produce unexpected output when
run.
// This C++ program compiles fine
// as index out of bound
// is not checked in C.
#include <iostream>
using namespace std;
int main()
{
int arr[2];
return 0;
}
Output
-449684907 4195777
The elements are stored at contiguous memory locations
Example:
#include <iostream>
using namespace std;
int main()
{
// an array of 10 integers.
// If arr[0] is stored at
// address x, then arr[1] is
// stored at x + sizeof(int)
// arr[2] is stored at x +
// sizeof(int) + sizeof(int)
// and so on.
int arr[5], i;
return 0;
}
Output
Size of integer in this compiler is 4
Address arr[0] is 0x7ffe75c32210
Address arr[1] is 0x7ffe75c32214
Address arr[2] is 0x7ffe75c32218
Address arr[3] is 0x7ffe75c3221c
Address arr[4] is 0x7ffe75c32220
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int arr[6]={11,12,13,14,15,16};
// Way -1
for(int i=0;i<6;i++)
cout<<arr[i]<<" ";
cout<<endl;
// Way 2
cout<<"By Other Method:"<<endl;
for(int i=0;i<6;i++)
cout<<i[arr]<<" ";
cout<<endl;
return 0;
}
Output
11 12 13 14 15 16
By Other Method:
11 12 13 14 15 16
Array elements store in a contiguous memory Linked list elements can be stored anywhere in
location. the memory or randomly stored.
Array works with a static memory. Here static The Linked list works with dynamic memory.
memory means that the memory size is fixed Here, dynamic memory means that the memory
and cannot be changed at the run time. size can be changed at the run time according to
our requirements.
Array elements are independent of each other. Linked list elements are dependent on each other.
As each node contains the address of the next
node so to access the next node, we need to
access its previous node.
Array takes more time while performing any Linked list takes less time while performing any
operation like insertion, deletion, etc. operation like insertion, deletion, etc.
Accessing any element in an array is faster as Accessing an element in a linked list is slower as it
the element in an array can be directly accessed starts traversing from the first element of the
through the index. linked list.
In the case of an array, memory is allocated at In the case of a linked list, memory is allocated at
compile-time. run time.
Memory utilization is inefficient in the array. For Memory utilization is efficient in the case of a
example, if the size of the array is 6, and array linked list as the memory can be allocated or
consists of 3 elements only then the rest of the deallocated at the run time according to our
space will be unused. requirement.
Search, insert and delete in an
unsorted array
In an unsorted array, the search operation can be performed by linear
traversal from the first element to the last element.
return -1;
}
// Driver Code
int main()
{
int arr[] = {12, 34, 10, 6, 40};
int n = sizeof(arr) / sizeof(arr[0]);
if (position == - 1)
cout << "Element not found";
else
cout << "Element Found at Position: "
<< position + 1;
return 0;
}
Output:
Element Found at Position: 5
arr[n] = key;
return (n + 1);
}
// Driver Code
int main()
{
int arr[20] = {12, 16, 20, 40, 50, 70};
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
cout << "\n Before Insertion: ";
for (i = 0; i < n; i++)
cout << arr[i]<< " ";
// Inserting key
n = insertSorted(arr, n, key, capacity);
return 0;
}
Output:
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26
Delete Operation
In the delete operation, the element to be deleted is searched using
the linear search, and then delete operation is performed followed by shifting
the elements.
if (pos == - 1)
{
cout << "Element not found";
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
return - 1;
}
// Driver code
int main()
{
int i;
int arr[] = {10, 50, 30, 40, 20};
n = deleteElement(arr, n, key);
return 0;
}
Output:
Array before deletion
10 50 30 40 20
Array after deletion
10 50 40 20
Search Operation
In a sorted array, the search operation can be performed by using binary
search.
/* Driver code */
int main()
{
// Let us search 3 in below array
int arr[] = { 5, 6, 7, 8, 9, 10 };
int n, key;
n = sizeof(arr) / sizeof(arr[0]);
key = 10;
Output
Index: 5
Insert Operation
In a sorted array, a search operation is performed for the possible position of
the given element by using Binary search and then insert operation is
performed followed by shifting the elements. And in an unsorted array, the
insert operation is faster as compared to the sorted array because we don’t
have to care about the position at which the element to be placed.
int i;
for (i = n - 1; (i >= 0 && arr[i] > key); i--)
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
}
/* Driver code */
int main()
{
int arr[20] = { 12, 16, 20, 40, 50, 70 };
int capacity = sizeof(arr) / sizeof(arr[0]);
int n = 6;
int i, key = 26;
// Inserting key
n = insertSorted(arr, n, key, capacity);
return 0;
}
Output
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 26 40 50 70
Time Complexity of Insert Operation: O(n) [In the worst case all elements
may have to be moved]
Delete Operation
In the delete operation, the element to be deleted is searched using binary
search and then delete operation is performed followed by shifting the
elements.
// C++ program to implement delete operation in a
// sorted array
#include <iostream>
using namespace std;
if (pos == -1)
{
cout << "Element not found";
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
n = deleteElement(arr, n, key);
Output
Array before deletion
10 20 30 40 50