Array Ds 1 Chapter
Array Ds 1 Chapter
● Linear data structure: Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to its previous and
next adjacent elements, is called a linear data structure.
Examples of linear data structures are array, stack, queue, linked list, etc.
● Static data structure: Static data structure has a fixed memory size. It is
easier to access the elements in a static data structure.
An example of this data structure is an array.
● Dynamic data structure: In dynamic data structure, the size is not fixed. It
can be randomly updated during the runtime which may be considered
efficient concerning the memory (space) complexity of the code.
Examples of this data structure are queue, stack, etc.
2
● Non-linear data structure: Data structures where data elements are not
placed sequentially or linearly are called non-linear data structures. In a non-
linear data structure, we can’t traverse all the elements in a single run only.
Examples of non-linear data structures are trees and graphs.
● For example, we can store a list of items having the same data-type using
the array data structure.
1.2 Array introduction, need for array, representation, basic operation on array-create static
and dynamic array, traverse, insertion, deletion.
Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations. It is one of the simplest data structures where
each data element can be randomly accessed by using its index number.
In C programming, they are the derived data types that can store the
primitive type of data such as int, char, double, float, etc. For example, if we
want to store the marks of a student in 6 subjects, then we don't need to
define a different variable for the marks in different subjects. Instead, we can
define an array that can store the marks in each subject at the contiguous
memory locations.
Properties of array
There are some of the properties of an array that are listed as follows -
o Each element in an array is of the same data type and carries the
same size that is 4 bytes.
o Elements in the array are stored at contiguous memory locations from
which the first element is stored at the smallest memory location.
o Elements of the array can be randomly accessed since we can
calculate the address of each element of the array with the given base
address and the size of the data element.
Representation of an array
We can represent an array in various ways in different programming
languages. As an illustration, let's see the declaration of array in C language
-
As per the above illustration, there are some of the following important
points -
3
In the above image, we have shown the memory allocation of an array arr of
size 5. The array follows a 0-based indexing approach. The base address of
the array is 100 bytes. It is the address of arr[0]. Here, the size of the data
type used is 4 bytes; therefore, each element will take 4 bytes in the
memory.
Here, size represents the memory taken by the primitive data types. As an
instance, int takes 2 bytes, float takes 4 bytes of memory space in C
programming.
Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size of
an element = 2 bytes, find the location of A[-1].
= 999 + 18
= 1017
Basic operations
Now, let's discuss the basic operations supported in the array -
o Traversal - This operation is used to print the elements of the array.
o Insertion - It is used to add an element at a particular index.
o Deletion - It is used to delete an element from a particular index.
o Search - It is used to search an element using the given index or by the
value.
o Update - It updates an element at a particular index.
Traversal operation
This operation is performed to traverse through the array elements. It prints
all array elements one after another. We can understand it with the below
program -
#include <iostream>
Using namespace std;
5
void main() {
int Arr[5] = {18, 30, 15, 70, 12};
int i;
cout<<"Elements of the array are:”<<”\n";
for(i = 0; i<5; i++)
{
cout<<Arr[i]<<”\n”;
}
}
Output
Insertion operation
This operation is performed to insert one or more elements into the array. As
per the requirements, an element can be added at the beginning, end, or at
any index of the array. Now, let's see the implementation of inserting an
element into the array.
#include <iostream>
using namespace std;
int main()
{
int arr[20] = { 18, 30, 15, 70, 12 };
int i, x, pos, n = 5;
printf("Array elements before insertion\n");
for (i = 0; i < n; i++)
cout<<arr[i]<<”\n”;
x = 50; // element to be inserted
pos = 4;
n++;
for (i = n-1; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
cout<<"Array elements after insertion<<”\n";
for (i = 0; i < n; i++)
6
cout<< arr[i]<<”\n”;
return 0;
}
Output
18
30
15
70
12
18
30
15
50
70
12
Deletion operation
As the name implies, this operation removes an element from the array and
then reorganizes all of the array elements.
#include <iostream>
using namespace std;
void main() {
int arr[] = {18, 30, 15, 70, 12};
7
int k = 30, n = 5;
int i, j;
cout<<"Given array elements are :<<”\n";
for(i = 0; i<n; i++)
{
cout<<arr[i]<<”\n”;
}
j = k;
while( j < n) {
arr[j-1] = arr[j];
j = j + 1;
}
n = n -1;
Output
1.3 Array applications–Searching method- Sequential search, Binary Search, Sorting, Method -
Insertion sort,
A linear search is also known as a sequential search that simply scans each
element at a time. Suppose we want to search an element in an array or list;
we simply calculate its length and do not jump at any item.
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
int result = search(arr, N, x);
if(result == -1)
9
In the above case, 'a' is the name of the array, mid is the index of the
element calculated recursively, data is the element that is to be
searched, left denotes the left element of the array and right denotes the
element that occur on the right side of the array.
Let's understand the working of binary search through an example.
In the above figure, we can observe that a[mid]>data, so again, the value of
mid will be calculated in the next step.
Now the value of mid is calculated again by using the formula which we have
already discussed. The values of left and right are 6 and 6 respectively, so
the value of mid becomes 6 as shown in the below figure:
We can observe in the above figure that a[mid]=data. Therefore, the search
is completed, and the element is found successfully.
#include<iostream>
using namespace std;
int binarySearch(int arr[], int low, int high, int x)
{
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
// Element to be searched
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
if(result == -1)
cout << "Element is not present in array"
else
cout << "Element is present at index " << result;
return 0;
}
Insertion Sort
Algorithm
The simple steps of achieving the insertion sort are listed as follows -
Step 1 - If the element is the first element, assume that it is already sorted.
Return 1.
Step3 - Now, compare the key with all elements in the sorted array.
Step 4 - If the element in the sorted array is smaller than the current
element, then move to the next element. Else, shift greater elements in the
array towards the right.
#include <iostream>
using namespace std;
void insert(int a[], int n) /* function to sort an aay with insertion sort */
{
int i, j, temp;
while(j>=0 && temp <= a[j]) /* Move the elements greater than temp
to one position ahead from their current position*/
17
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
int main()
{
int a[] = { 89, 45, 35, 8, 12, 2 };
int n = sizeof(a) / sizeof(a[0]);
cout<<"Before sorting array elements are - "<<endl;
printArr(a, n);
insert(a, n);
cout<<"\nAfter sorting array elements are - "<<endl;
printArr(a, n);
return 0;
}
Output:
1. Time Complexity
2. Space Complexity
Stable YES
o The space complexity of bubble sort is O(1). It is because, in bubble
sort, an extra variable is required for swapping.
o The space complexity of optimized bubble sort is O(2). It is because
two extra variables are required in optimized bubble sort.
22
#include<iostream>
using namespace std;
void print(int a[], int n) //function to print array elements
{
int i;
for(i = 0; i < n; i++)
{
cout<<a[i]<<" ";
}
}
void bubble(int a[], int n) // function to implement bubble sort
{
int i, j, temp;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
int main()
{
int i, j,temp;
int a[5] = {45, 1, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
cout<<"Before sorting array elements are - \n";
print(a, n);
bubble(a, n);
cout<<"\nAfter sorting array elements are - \n";
print(a, n);
return 0;
}
Output
Before sorting array elements are - 45 1 32 13 26
23
Merge Sort
Algorithm
In the following algorithm, arr is the given array, beg is the starting
element, and end is the last element of the array.
MERGE_SORT(arr, beg, end)
END MERGE_SORT
24
1. Time Complexity
2. Space Complexity
Stable YES
#include <iostream>
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
int main()
{
int a[] = { 11, 30, 24, 7, 31, 16, 39, 41 };
int n = sizeof(a) / sizeof(a[0]);
cout<<"Before sorting array elements are - \n";
printArray(a, n);
mergeSort(a, 0, n - 1);
27
Quick Sort
Algorithm:
1. Time Complexity
31
2. Space Complexity
Stable NO
#include <iostream>
return 0;
}
Output:
Before sorting array elements are –
23 8 28 13 18 26