Introduction
In simple terms, Data Structures are ways to arrange data in the computer memory so that it can be used efficiently.
Whereas, Algorithms can be defined as a sequence of steps performed on data or data structures to efficiently solve a problem.
Classification of Data Structures
Based on Storage
Linear: Elements are arranged sequentially. Accessed sequentially and stored in a linear or non-linear manner in memory. For example Arrays, Linked Lists, Stacks & Queues.
Non-Linear: Elements are arranged in a non-linear fashion. Accessed in non-linear ways and stored in a linear or non-linear manner in memory. For example Trees, Graphs, Heaps.
Based on Purpose
Container: Particularly used for storage of data. For example Arrays & Linked Lists.
Priority: Elements are inserted or deleted based on priority. For example Queues, Stacks & Heaps.
Indexing: Particularly used for searching purpose. For example Tree and Hash Table.
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. There can be two types of arrays:
Static array - Size cannot be changed.
Dynamic array - Size can be changed.
Elements in the array can be accessed using the base address in constant time. Although changing the size of the array is not possible, one can always relocate the elements to a bigger memory location by creating a new array of a bigger size. Therefore resizing an array is a costly option. In Java array is dynamically allocated. The array can be multi-dimensional.
Types of Arrays
One-Dimensional Array: A variable which represents the list of items using only one index is called a 1D array.
// Declaration of array
int[5] arr;
// Initialization of array
int[] arr = {1,2,3,4,5};
Two-Dimensional Array: A variable which represents the list of items using two indices is called a 2D array. In a 2D array, data is stored in rows and columns. Number of rows is necessary to be specified while initializing, number of columns can be ignored.
// Declaration of array
int[3][3] arr; // int[row][column]
// Initialization of array
int[3][3] arr = {{1,2,3},{4,5,6},{7,8,9}};
Advantages of Array
Random access to elements.
Easy sorting and iteration.
Replacement of multiple variables.
Disadvantages of Array
The size is fixed.
Difficult to insert and delete elements.
Needs contiguous memory.
If the size is more & occupancy is less, most of the array gets wasted.
Operations possible on an Array
Traversal.
Insertion.
Deletion.
Searching.
Merging.
Sorting.
In this series of blogs, we will understand DSA in depth along with 450 problems of Love Babbar Sheet: Love Babbar DSA Sheet and problems from Kunal Kushwaha Assignments: Kunal Kushwaha Assignment Sheet.