introduction to data structures and algorithms · introduction to data structures and algorithms...

12
9/4/2018 Introduction to Data Structures and Algorithms | Studytonight https://www.studytonight.com/data-structures/introduction-to-data-structures 1/3 Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective way. Data Structures is about rendering data elements in terms of some relationship, for better organization and storage. For example, we have some data which has, player's name "Virat" and age 26. Here "Virat" is of String data type and 26 is of integer data type. We can organize this data as a record like Player record, which will have both player's name and age in it. Now we can collect and store player's records in a file or database as a data structure. For example: "Dhoni" 30, "Gambhir" 31, "Sehwag" 33 If you are aware of Object Oriented programming concepts, then a class also does the same thing, it collects different type of data under one single entity. The only difference being, data structures provides for techniques to access and manipulate data efficiently. In simple language, Data Structures are structures programmed to store ordered data, so that various operations can be performed on it easily. It represents the knowledge of data to be organized in memory. It should be designed and implemented in such a way that it reduces the complexity and increases the efficiency. Basic types of Data Structures As we have discussed above, anything that can store data can be called as a data structure, hence Integer, Float, Boolean, Char etc, all are data structures. They are known as Primitive Data Structures. Then we also have some complex Data Structures, which are used to store large and connected data. Some example of Abstract Data Structure are : Linked List Tree Graph Stack, Queue etc. All these data structures allow us to perform different operations on data. We select these data structures based on which type of operation is required. We will look into these data structures in more details in our later lessons. DATA STRUCTURES & ALGORITHMS lay.google.com/store/apps/details?id=com.studytonight.app) (https://www.studytonight.com/)

Upload: others

Post on 21-Jul-2020

25 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures and Algorithms | Studytonight

https://www.studytonight.com/data-structures/introduction-to-data-structures 1/3

Introduction to Data Structures and AlgorithmsData Structure is a way of collecting and organising data in such a way that we can perform operations on these data in an effective way.

Data Structures is about rendering data elements in terms of some relationship, for better organization and storage. For example, we have

some data which has, player's name "Virat" and age 26. Here "Virat" is of String data type and 26 is of integer data type.

We can organize this data as a record like Player record, which will have both player's name and age in it. Now we can collect and store

player's records in a file or database as a data structure. For example: "Dhoni" 30, "Gambhir" 31, "Sehwag" 33

If you are aware of Object Oriented programming concepts, then a  class  also does the same thing, it collects different type of data underone single entity. The only difference being, data structures provides for techniques to access and manipulate data efficiently.

In simple language, Data Structures are structures programmed to store ordered data, so that various operations can be performed on it

easily. It represents the knowledge of data to be organized in memory. It should be designed and implemented in such a way that it reduces

the complexity and increases the efficiency.

Basic types of Data StructuresAs we have discussed above, anything that can store data can be called as a data structure, hence Integer, Float, Boolean, Char etc, all are

data structures. They are known as Primitive Data Structures.

Then we also have some complex Data Structures, which are used to store large and connected data. Some example of Abstract Data

Structure are :

Linked List

Tree

Graph

Stack, Queue etc.

All these data structures allow us to perform different operations on data. We select these data structures based on which type of operation

is required. We will look into these data structures in more details in our later lessons.

DATA STRUCTURES & ALGORITHMS

lay.google.com/store/apps/details?id=com.studytonight.app) (https://www.studytonight.com/)

Page 2: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures and Algorithms | Studytonight

https://www.studytonight.com/data-structures/introduction-to-data-structures 2/3

 

The data structures can also be classified on the basis of the following characteristics:

Characterstic Description

Linear In Linear data structures,the data items are arranged in a linear sequence. Example: Array

Non­Linear In Non­Linear data structures,the data items are not in sequence. Example: Tree, Graph

Homogeneous In homogeneous data structures,all the elements are of same type. Example: Array

Non­Homogeneous

In Non­Homogeneous data structure, the elements may or may not be of the same type. Example: Structures

Static Static data structures are those whose sizes and structures associated memory locations are fixed, at compile time.Example: Array

Dynamic Dynamic structures are those which expands or shrinks depending upon the program need and its execution. Also, theirassociated memory locations changes. Example: Linked List created using pointers

What is an Algorithm ?An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain predefined task. Algorithm is not the complete

code or program, it is just the core logic(solution) of a problem, which can be expressed either as an informal high level description as

pseudocode or using a flowchart.

Every Algorithm must satisfy the following properties:

1. Input­ There should be 0 or more inputs supplied externally to the algorithm.

2. Output­ There should be atleast 1 output obtained.

3. Definiteness­ Every step of the algorithm should be clear and well defined.

4. Finiteness­ The algorithm should have finite number of steps.

5. Correctness­ Every step of the algorithm must generate a correct output.

An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less memory space. The performance of an

algorithm is measured on the basis of following properties :

1. Time Complexity

2. Space Complexity

Page 3: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures and Algorithms | Studytonight

https://www.studytonight.com/data-structures/introduction-to-data-structures 3/3

Next → (aysmptotic­notations)

Space ComplexityIts the amount of memory space required by the algorithm, during the course of its execution. Space complexity must be taken seriously for

multi­user systems and in situations where limited memory is available.

An algorithm generally requires space for following components :

Instruction Space: Its the space required to store the executable version of the program. This space is fixed, but varies depending upon

the number of lines of code in the program.

Data Space: Its the space required to store all the constants and variables(including temporary variables) value.

Environment Space: Its the space required to store the environment information needed to resume the suspended function.

To learn about Space Complexity in detail, jump to the Space Complexity (space­complexity­of­algorithms) tutorial.

Time ComplexityTime Complexity is a way to represent the amount of time required by the program to run till its completion. It's generally a good practice to

try to keep the time required minimum, so that our algorithm completes it's execution in the minimum time possible. We will study about Time

Complexity (time­complexity­of­algorithms) in details in later sections.

NOTE: Before going deep into data structure, you should have a good knowledge of programming either in C or in C++ or Java or Python

etc.

Page 4: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 1/11

Aakash Varshney Follow

Jan 17 · 8 min read

Introduction to Data Structures

What is data structure?

Upcoming sophomores majoring in Computer Engineering, Software

Engineering or Computer Science who have signed up for Intro to Data

Structures class often ask the same question: What the heck is data

structure?

The simplest way to answer their question is by describing data

structures as a way of organizing and storing data so that operations

can be performed e�ciently. This leads us to the question, what kind of

operations are we talking about? Accessing, inserting, deleting, �nding,

and sorting the data are some of the basic operations that one can

perform using data structures. Not all data structures can perform

these operations e�ciently, that’s what led to the development of

di�erent data structures. Let’s say you are to �nd a speci�c book in an

unorganized library, that task would take an enormous amount of time.

Just like a library organizes their books, we need to organize our data

so that operations can be performed e�ciently.

If you have previous experience with programming, you might know

what a pre-de�ned data type is. A pre-de�ned data type such as Integer,

Strings, Boolean is used to create the structure of data. The use of data

type is based on the user requirement and the kind of data they want to

store. Now let’s dive into the data structures that are used in our day to

day programming, and we will also look into the support for the data

structures for di�erent programming languages.

One Dimensional Arrays

A basic data structure which one uses in a day to day programming is

an array. An array can hold a �xed number of containers to store data

and operations could be performed on that data according to the user’s

needs.

Page 5: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 2/11

In the picture above, an array is de�ned with the name arrayName and

has an integer data type. The number below in the bold shows the

memory address each container of an array is assigned to. The

important thing here to remember is an array index always starts at 0

and ends at (total array size -1). So, let’s just say you de�ned an array

of length 5, arrayName[5], then the indexes of this array would be

arrayName[0], arrayName[1], arrayName[2], arrayName[3],

arrayName[4].

Multidimensional Arrays

A multidimensional array is just an extension of the normal array,

where we de�ne an array with a row of containers. In case of a

multidimensional array, we have rows as well as columns. In order to

access an index, we need two numbers to get there.

Page 6: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 3/11

In the picture above, we de�ned an arrayName[3][4] with 3 rows and 4

columns and in total of Row * Column (3*4) indexes. Multidimensional

arrays are sometimes referred to as tables or matrix because of their

structure.

Dynamic Arrays

Most of the programming languages allow you to de�ne dynamic

arrays, which means the memory is allocated during program

execution. Consider it this way, you de�ned an array with 10 indexes

but, you only need 3 indexes, thus the remaining 7 indexes are laid to

waste, thereby consuming extra memory. Dynamic arrays give you the

�exibility to allocate memory according to the program requirement.

Support for Arrays for di�erent

programming languages:

Java :- There are a couple of classes for arrays, but the best known one

is ArrayList class.

C++ :- Vectors are used to represent the dynamic arrays in C++. It can

be implemented using std::vector.

Python :- Python has a new data type known as list just like Boolean

and Integers. The list data type can be used to dynamically de�ne

arrays.

Singly Linked List

A linked list is a collection of nodes that are connected by links. Linked

list contains node which store the data items and the address to the

next node. The �rst node is usually referred to as the head node and the

last node is referred to as the tail node. The pointer of the head node

points to the next node and the pointer of the tail node points to Null.

Page 7: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 4/11

The dynamics of this data structure makes it easier to add or remove

nodes from the end. In order to add/remove a node, you just need to

keep track of the previous node and the node after and adjust the

pointers accordingly.

Doubly Linked List

A doubly linked list is not much di�erent from a singly linked list, the

only thing that sets them apart is the pointer to the previous node. The

picture below can give you a brief idea of what the structure should

look like.

In the case of a doubly linked list, the previous pointer of the head node

points to Null and the next pointer of the tail points to Null. The

previous pointer makes it easier to traverse in either direction. So, node

addition and removal become super easy, all you need to do is keep

track of the previous node and the next node and adjust the pointer

accordingly.

Circular Linked List

A circular linked list is a linked list where all the nodes are connected to

form a circle. There is no head or tail node. The advantage of having

this type of linked list is any node can be used as a starting point.

Page 8: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 5/11

Comparison Between Arrays and

Linked List

A list is a di�erent type of data structure from an array. Instead of

storing data in chunks of memory the array requires a contiguous

memory space. If you add an element to an array, it might change the

memory location of the entire array, but the data remains intact. An

array gives you the index number, therefore, you can access it directly

or sequentially. Whereas to access any data member you must traverse

through the entire list.

Support for Linked Lists for di�erent

programming languages:

Java :- Java has an inbuilt class called LinkedList which can be used to

implement a linked list.

C++ :- Like Java, C++ has also a Standard Template Library called list

for linked list implementation.

Python :- Python also has an inbuilt module llist which implements

linked lists.

Stack

You have probably heard this word in your day to day life, whether it is

referring to a deck of cards or books lying on top of a desk. Well, the

data structure works in a similar manner as you would perform actions

on a real-world stack. For example, you can only add or remove books

from the top of the stack.

A stack is a LIFO data structure which means Last In First Out, the last

item to come is the �rst one to go out. A stack can either be

implemented using an array or a linked list. Consider a stack of books,

the last book would obviously be placed at the top and if you want to

remove a book you will remove one from the top. The three basic

operations a stack could perform are:

· Push — to input data into stack and place it on top.

· Pop — to remove the data from the top.

Page 9: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 6/11

· Peek — to look at the data at the top without removing it.

The picture below shows the e�ect of these operations on the structure

Support for Linked Lists for di�erent

programming languages:

Java :- Java has an inbuilt class called Stack which can be used to

implement stack data structures.

C++ :- Like Java, C++ has also a Standard Template Library called

stack for linked list implementation.

Python :- Python does not have an explicit stack class, but lists can be

implemented as stacks.

Queue

Just like the word stack, the word queue is also derived from day to day

activities. You have most likely seen a queue of people in a supermarket

at the billing counter where the last one to come stands at the end and

the �rst one to come is the �rst one to get their groceries check out. In

the similar manner, queue works where the operations are performed

at both ends.

Page 10: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 7/11

A stack is a FIFO data structure which elaborates to First In First Out. A

queue, like stack, can either be implemented using arrays or linked list.

The three most commonly performed operations on queue are:

· enqueue — to input data into stack and place them at the bottom.

· dequeue — to remove the data from the top.

· Peek — to look at the data at the top without removing it.

Support for Linked Lists for di�erent

programming languages:

Java :- Queue can be implemented using List class in Java

C++ :- Like Java, C++ has also a Standard Template Library called

Queue for queue implementation.

Python :- The queue module in python o�ers the functionality of the

queue class. Since queues can be implemented using linked list

therefore, llist can also be used.

Trees

The tree data structure resembles an upside tree, at the top we have a

root node which is divided into a left and a right node. They have links

between them, which connect all the nodes. Unlike linked list where a

Page 11: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 8/11

node can be connected to only one node, a tree could have a node

connected to two or more nodes.

Binary Trees

Binary trees are special kind of trees where the left node is always

smaller than the parent node and the right node is always bigger than

the parent node. In order to add/delete a node we have to compare the

value with the root node and then traverse to a speci�c point to

insert/delete.

Support for Linked Lists for di�erent

programming languages

Page 12: Introduction to Data Structures and Algorithms · Introduction to Data Structures and Algorithms Data Structure is a way of collecting and organising data in such a way that we can

9/4/2018 Introduction to Data Structures – The Startup – Medium

https://medium.com/swlh/introduction-to-data-structures-9134b7d064a6 9/11

Java :- Java doesn’t have any built-in class to implement tree data

structure.

C++ :- Just like Java, C++ also doesn’t have any container in standard

template library to implement trees.

Python :- Python doesn’t have built-in data structures so in order to

implement a tree you have to create the structure manually.

Conclusion

There are some points you need to consider while deciding which data

structure satis�es your need. Unless you have a really big set of data, it

doesn’t matter which data structure you use. The questions you need to

ask yourself before implementing a data structure is:

How much data you need to store?

How often do you access data?

What is the purpose of the data? Do you need to access, insert,

delete, or sort the data?

The algorithms work di�erently with di�erent data structures and

across di�erent programming languages, so you need to understand

the respective syntax of the language before implementing a data

structure.

This story is published in The Startup, Medium’s

largest entrepreneurship publication followed by

295,232+ people.

Subscribe to receive our top stories here.