Tutorial Playlist

Data Structure Tutorial

Overview

Arrays in Data Structures: A Guide With Examples

Lesson - 1

All You Need to Know About Two-Dimensional Arrays

Lesson - 2

All You Need to Know About a Linked List in a Data Structure

Lesson - 3

The Complete Guide to Implement a Singly Linked List

Lesson - 4

The Ultimate Guide to Implement a Doubly Linked List

Lesson - 5

The Fundamentals for Understanding Circular Linked List

Lesson - 6

The Ultimate Guide To Understand The Differences Between Stack And Queue

Lesson - 7

Implementing Stacks in Data Structures

Lesson - 8

Your One-Stop Solution for Stack Implementation Using Array

Lesson - 9

Your One-Stop Solution for Queue Implementation Using Array

Lesson - 10

Your One-Stop Solution to Learn Depth-First Search(DFS) Algorithm From Scratch

Lesson - 11

Your One-Stop Solution for Stack Implementation Using Linked-List

Lesson - 12

The Definitive Guide to Understand Stack vs Heap Memory Allocation

Lesson - 13

All You Need to Know About Linear Search Algorithm

Lesson - 14

All You Need to Know About Breadth-First Search Algorithm

Lesson - 15

A One-Stop Solution for Using Binary Search Trees in Data Structure

Lesson - 16

The Best Tutorial to Understand Trees in Data Structure

Lesson - 17

A Complete Guide to Implement Binary Tree in Data Structure

Lesson - 18

A Holistic Look at Using AVL Trees in Data Structures

Lesson - 19

All You Need to Know About Tree Traversal in Data Structure

Lesson - 20

The Best Guide You’ll Ever Need to Understand B-Tree in Data Structure

Lesson - 21

The Best Guide You'll Ever Need to Understand Spanning Tree in Data Structure

Lesson - 22

The Best and Easiest Way to Understand an Algorithm

Lesson - 23

Your One-Stop Solution to Understand Shell Sort Algorithm

Lesson - 24

Your One-Stop Solution to Quick Sort Algorithm

Lesson - 25

The Most Useful Guide to Learn Selection Sort Algorithm

Lesson - 26

Everything You Need to Know About Radix Sort Algorithm

Lesson - 27

Everything You Need to Know About the Counting Sort Algorithm

Lesson - 28

Everything You Need to Know About the Merge Sort Algorithm

Lesson - 29

Insertion Sort Algorithm: One-Stop Solution That Will Help You Understand Insertion Sort

Lesson - 30

Everything You Need to Know About the Bubble Sort Algorithm

Lesson - 31

The Best Guide You’ll Ever Need to Understand Bucket Sort Algorithm

Lesson - 32

Your One-Stop Solution to Understand Recursive Algorithm in Programming

Lesson - 33

The Definitive Guide to Understanding Greedy Algorithm

Lesson - 34

Your One-Stop Solution to Understand Backtracking Algorithm

Lesson - 35

The Fundamentals of the Bellman-Ford Algorithm

Lesson - 36

Your One-Stop Solution for Graphs in Data Structures

Lesson - 37

The Best Guide to Understand and Implement Solutions for Tower of Hanoi Puzzle

Lesson - 38

A Simplified and Complete Guide to Learn Space and Time Complexity

Lesson - 39

All You Need to Know About the Knapsack Problem : Your Complete Guide

Lesson - 40

The Fibonacci Series: Mathematical and Programming Interpretation

Lesson - 41

The Holistic Look at Longest Common Subsequence Problem

Lesson - 42

The Best Article to Understand What Is Dynamic Programming

Lesson - 43

A Guide to Implement Longest Increasing Subsequence Using Dynamic Programming

Lesson - 44

A Holistic Guide to Learn Stop Solution Using Dynamic Programming

Lesson - 45

One Stop Solution to All the Dynamic Programming Problems

Lesson - 46

Understanding the Fundamentals of Binomial Distribution

Lesson - 47
Arrays in Data Structures: A Guide With Examples

Arrays in data structures help solve some high-level problems like the "longest consecutive subsequence" program or some easy tasks like arranging the same things in ascending order. The concept is to collect many objects of the same kind. 

What Are Arrays in Data Structures?

Arrays_in_ds-what-is-array-img1

An array is a linear data structure that collects elements of the same data type and stores them in contiguous and adjacent memory locations. Arrays work on an index system starting from 0 to (n-1), where n is the size of the array.

It is an array, but there is a reason that arrays came into the picture.

Post Graduate Program In Data Science

The Ultimate Ticket To Top Data Science Job RolesExplore Course
Post Graduate Program In Data Science

Why Do You Need an Array in Data Structures?

Arrays_in_ds-why-array-img1

Let's suppose a class consists of ten students, and the class has to publish their results. If you had declared all ten variables individually, it would be challenging to manipulate and maintain the data. 

If more students were to join, it would become more difficult to declare all the variables and keep track of it. To overcome this problem, arrays came into the picture.

What Are the Types of Arrays?

There are majorly two types of arrays, they are:

One-Dimensional Arrays: 

Arrays_in_ds-array-type-1d-img1

You can imagine a 1d array as a row, where elements are stored one after another.

Multi-Dimensional Arrays:

These multi-dimensional arrays are again of two types. They are:

Two-Dimensional Arrays: 

Arrays_in_ds-array-type-2d-img1

You can imagine it like a table where each cell contains elements.

Three-Dimensional Arrays: 

Arrays_in_ds-array-type-3d-img1.

You can imagine it like a cuboid made up of smaller cuboids where each cuboid can contain an element.

In this "arrays in data structures" tutorial, you will work around one-dimensional arrays.

How Do You Declare an Array?

Arrays_in_ds-array-declare-img1.

Arrays are typically defined with square brackets with the size of the arrays as its argument.

Here is the syntax for arrays:

1D Arrays: int arr[n];

2D Arrays: int arr[m][n];

3D Arrays: int arr[m][n][o];

How Do You Initialize an Array?

You can initialize an array in four different ways:

  • Method 1:

int a[6] = {2, 3, 5, 7, 11, 13};

  • Method 2: 

int arr[]= {2, 3, 5, 7, 11};

  • Method 3: 

int n;

scanf(“%d”,&n);

int arr[n];

for(int i=0;i<5;i++)

{

scanf(“%d”,&arr[i]);

}

  • Method 4:

int arr[5];

arr[0]=1;

arr[1]=2;

arr[2]=3;

arr[3]=4;

arr[4]=5;

How Can You Access Elements of Arrays in Data Structures?

Arrays_in_ds-array-access-img1

You can access elements with the help of the index at which you stored them. Let's discuss it with a code:

#include<stdio.h>

int main()

{

int a[5] = {2, 3, 5, 7, 11};

printf(“%d\n”,a[0]); // we are accessing 

printf(“%d\n”,a[1]);

printf(“%d\n”,a[2]);

printf(“%d\n”,a[3]);

printf(“%d”,a[4]);

return 0;

}

Arrays_in_ds-array-access-img2

In this “Arrays in Data structures” tutorial, you have seen the basics of array implementation, now you will perform operations on arrays.

What Operations Can You Perform on an Array?

  • Traversal
  • Insertion
  • Deletion
  • Searching
  • Sorting

Traversing the Array:

Arrays_in_ds-op-traverse-img1

Traversal in an array is a process of visiting each element once.

Code:

#include<stdio.h>

int main()

{

int a[5] = {2, 3, 5, 7, 11};

for(int i=0;i<5;i++)

{

//traversing ith element in the array

printf(“%d\n”,a[i]); 

}

return 0;

}

Arrays_in_ds-op-traverse-img2

Insertion:

Arrays_in_ds-op-insertion-img1

Insertion in an array is the process of including one or more elements in an array.

Insertion of an element can be done:

  • At the beginning
  • At the end and
  • At any given index of an array. 

At the Beginning:

Code:

#include<stdio.h>

int main()

{

    int array[10], n,i, item;

    printf("Enter the size of array: ");

     scanf("%d", &n);

    printf("\nEnter Elements in array: ");

        for(i=0;i<n;i++)

        {

         scanf("%d", &array[i]);

        }

    printf("\n enter the element at the beginning");

    scanf("%d", &item);

    n++;

    for(i=n; i>1; i--)

    {

      array[i-1]=array[i-2];

    }

    array[0]=item;

    printf("resultant array element");

    for(i=0;i<n;i++)

    {

     printf("\n%d", array[i]);

    }

    getch();

    return 0;

}

Arrays_in_ds-op-insertionbeg-img1

At the End:

Code:

#include<stdio.h>

#include<conio.h>

int main()

{

int array[10], i, values;

printf("Enter 5 Array Elements: ");

for(i=0; i<5; i++)

     scanf("%d", &array[i]);

printf("\nEnter Element to Insert: ");

scanf("%d", &values);

array[i] = values;

printf("\nThe New Array is:\n");

for(i=0; i<6; i++)

     printf("%d ", array[i]);

getch();

return 0;

}

Arrays_in_ds-op-insertionend-img1

At a Specific Position:

Code:

#include <stdio.h>

int main()

{

 int array[100], pos, size, val;

 printf("Enter size of the array:");

 scanf("%d", &size);

 printf("\nEnter %d elements\n", size);

 for (int i = 0; i < size; i++)

  scanf("%d", &array[i]);

 printf("Enter the insertion location\n");

 scanf("%d", &pos);

 printf("Enter the value to insert\n");

 scanf("%d", &val);

 for (int i = size - 1; i >= pos - 1; i--)

  array[i+1] = array[i];

 array[pos-1] = val;

 printf("Resultant array is\n");

 for (int i = 0; i <= size; i++)

  printf("%d\n", array[i]);

 return 0;

}

Arrays_in_ds-op-insertionpos-img1

Deletion:

Arrays_in_ds-op-deletion-img1

Deletion of an element is the process of removing the desired element and re-organizing it.

You can also do deletion in different ways:

  • At the beginning
  • At the end

At the Beginning:

#include<stdio.h>

int main()

{

    int n,array[10];

    printf("enter the size of an array");

    scanf("%d" ,&n);

    printf("enter elements in an array");

    for(int i=0;i<n;i++)

        scanf("%d", &array[i]);

    n--;

    for(int i=0;i<n;i++)

     array[i]=array[i+1];

    printf("\nafter deletion ");

     for(int i=0;i<n;i++)

     printf("\n%d" , array[i]);

}

Arrays_in_ds-op-deletionbeg-img1

At the End:

#include<stdio.h>

int main()

{

int n,array[10];

printf("enter the size of an array");

scanf("%d" ,&n);

printf("enter elements in an array");

for(int i=0;i<n;i++)

scanf("%d", &array[i]);

printf("\nafter deletion array elements are");

for(int i=0;i<n-1;i++)

printf("\n%d" , array[i]);

}

Arrays_in_ds-op-deletionend-img1

Searching for an Element

Arrays_in_ds-op-search-img1.

The method of searching for a specific value in an array is known as searching.

There are two ways we can search in an array, they are:

  • Linear search
  • Binary search

Linear Search:

Code:

#include <stdio.h>

int linear(int a[], int n, int x)

{

for (int i = 0; i < n; i++)

if (a[i] == x)

return i;

return -1;

}

int main(void)

{

int a[] = { 2, 3, 4, 10, 40 };

int x = 10;

int n = sizeof(a) / sizeof(a[0]);

// Function call

int result = linear(a, n, x);

if(result == -1)

{

printf("Element is not present in array");

}

else

{

printf("Element found at index: %d", result);

}

return 0;

}

Arrays_in_ds-op-lsearch-img1

Binary Search:

#include <stdio.h>

int binary(int a[], int lt, int rt, int k)

{

if (rt >= lt) {

int mid = lt + (rt - l) / 2;

// check If element is at the middle

if (a[mid] == k)

return mid;

//check if element is at the left side of mid

if (a[mid] > x)

return binary(a, lt, mid - 1, k);

// Else element is at the right side of mid

return binary(a, mid + 1, rt, k);

}

// if element not found

return -1;

}

int main(void)

{

int a[] = { 2, 3, 5, 7, 11 };

int n = sizeof(a) / sizeof(a[0]);

int k = 11;

int res = binary(arr, 0, n - 1, k);

if(res == -1) 

{

printf("Element is not found")

}

else

{

printf("Element found at index %d",res);

}

return 0;

}

Arrays_in_ds-op-bsearch-img1

FREE Data Science With Python Course

Start Learning Data Science with Python for FREEStart Learning
FREE Data Science With Python Course

Sorting:

Arrays_in_ds-op-sort-img1

Sorting in an array is the process in which it sorts elements in a user-defined order.

Code:

#include <stdio.h>

void main()

{

        int temp, size, array[100];

        printf("Enter the size \n");

        scanf("%d", &size);

        printf("Enter the numbers \n");

        for (int i = 0; i < size; ++i)

         scanf("%d", &array[i]);

        for (int i = 0; i < size; ++i)

        {

          for (int j = i + 1; j < size; ++j)

         {

             if (array[i] > array[j])

         {

                 temp = array[i];

                 array[i] = array[j];

                 array[j] = temp;

          }

         }

        }

      printf("sorted array:\n");

       for (int i = 0; i < size; ++i)

         printf("%d\n", array[i]);

 }

Arrays_in_ds-op-sort-img2

What Are the Advantages of Arrays in Data Structures?

Arrays_in_ds-advantages-img1

  • Arrays store multiple elements of the same type with the same name.
  • You can randomly access elements in the array using an index number.
  • Array memory is predefined, so there is no extra memory loss.
  • Arrays avoid memory overflow.
  • 2D arrays can efficiently represent the tabular data.

What Are the Disadvantages of Arrays in Data Structures?

Arrays_in_ds-disadvantages-img1

  • The number of elements in an array should be predefined
  • An array is static. It cannot alter its size after declaration.
  • Insertion and deletion operation in an array is quite tricky as the array stores elements in continuous form.
  • Allocating excess memory than required may lead to memory wastage.
Are you considering a profession in the field of Data Science? Then get certified with the Data Science Bootcamp Program today!

Next Steps

"Linked list Data structures" can be your next stop. Linked lists are made up of nodes that point to the next node. Linked lists are dynamic and have faster insertion/deletion time complexities.

Simplilearn is the world's #1 online bootcamp for digital economy skills training focused on helping people acquire the skills they need to thrive in the digital economy. We provide rigorous online training in disciplines such as Cyber Security, Cloud Computing, Project Management, Digital Marketing, and Data Science, among others. 

If you have any questions about this ‘Arrays in Data structures’ tutorial, please feel free to leave them in the comments section below. Our 24/7 expert team will answer all your queries for you at the earliest.

If you are perhaps looking to explore and become a data professional, Simplilearn’s Post Graduate Program in Data Engineering, offered in partnership with Purdue University and in collaboration with IBM would be the ideal starting point. Within just 8 months, this globally-recognized, completely online program will transform you into a fully-equipped, work-ready big data and analytics professional. Aligned with top AWS and Azure certifications, and endorsed by Purdue and other top names in the industry, you will have all you need to fast track your career in the field, anywhere in the world.

Happy learning!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.