Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 47

Data Structures

Contents:

General aspects regarding data structure {part done by Sora David &
Potîrniche Andreea}

Data structures classification {part done by Goian Andrei & Andrei Igor}

Abstract data Types {part done by Pușcuță Răzvan}


General aspects regarding data structure

A data structure is a particular way of organizing data in a computer


so that it can be used effectively.

Data structures explain how data is organized. This structure dictates


how fast and efficient your code will run.
The Array Data  The array is a fundamental data structure. An array is basically
a list of data. An example of an array that says Hello is below:

Structure
Example of the
array:
 The numbers below the Hello is known
as an index. Think of an index of an
array as an address of a home. In that
Hello array, H lives in address 0, e
lives in address 1. The reason we start
the index at 0 is due to most
programming languages start an index
at 0 — like Python. To learn how data
structures like an array affect your
code, we have to look at operations.
The majority of data structures are applied
to these four operations:
Read: Checking out a data in a specific spot. For example, reading for “o” in the array above, the computer
knows to look in index 4.

Search: Finds a type of value within the data structure. For example, looking for numbers in the Hello array
would turn up with no findings.

Insert: Places another value in the data. For example, placing Hellow in the array above would add “w” in
index 5.

Delete: Removes a value in our data structure. For example, removing “H” from hello would be ello (side
note: deletion of a value h would actually involve another step of moving all the characters to left).
 Reading is checking what value is in a particular index
Reading inside an array. The speed of reading is really fast, just one
step fast. The reason is to the computer knows index in the
array and takes a peek inside. When a computer does reading
on an array, it goes to the correct index due to:
The computer knows the index of each value.
An array stored in memory is read in a “block.” Think of it
like houses in a street of finding addresses.
Arrays start at 0, so the computer knows it only goes up
from there.
 Searching in an array is looking for a particular type of
data and finding where it is in the index. Searching is done
in a pretty methodical way. The computer begins with
Searching
index 0 and looks onward until finding the value it is
looking for. In the worst case, if the computer wanted to
search within an array of 10 and the desired value is on
the 10th spot, then it would search all the way up to the
10th value. In other words, a 300 size array with the value
we want located in the 300th index, the computer would
take 300 steps to search for the value.
How fast insertion depends on where the value is being
Insertion

inserted. The fastest way for insertion in an array is
inserting in the end. Inserting in the beginning or
somewhere in the middle is a bit more complicated.
 In the worst case of insertion in an array, inserting, in
the beginning, takes the most steps. The reason is once
the value is inserted in the beginning, the computer has
to move all other values to the right.
 Any other value inserted in the middle just takes less
due to the values on the left not moved.
 Deletion removes a value within an array. Deletion
Deletion is kind of like insertion in an opposite kind of way.
Instead of placing a value, deletion removes the
value and shifts the values to cover the gap left by
the deletion. Just like insertion, the best case speed
of deletion is at the end — 1 step. It removes the end
value without having to shift any other values.
 The worst-case in deletion is at the beginning.
 Once it deletes the first value, all the other values to
the right have to be shifted one to the left to cover
the empty space in the array.
 The classification of data structure mainly consists of :
1. Primitive data structure
2. Non-primitive data structure

Classification of
data structure :
The primitive data structures are known as basic data
Primitive data

structures. These data structures are directly operated
upon by the machine instructions. Normally, primitive
structure : data structures have different representation on different
computers
Example of Integer Float

primitive data
structure :
Character Pointer
Integer : Float :
 The integers are signed or unsigned  Float refers floating point or real number.
whole numbers with the specified range It can hold a real number or a number
such as 5, 39, -1917, 0 etc. They have no having a fractional part like 3.112 or
fractional parts. Integers can be positive 588.001 etc. The decimal point signals
or negative but whether or not they can that it is a floating point number, not an
have negative values, it depends upon the integer. The number 15 is an integer but
integer types. 15.0 is a floating point number.
Character : Pointer :

 It can store any member of the basic  A pointer is but a variable-like name
character set. If a character from this set points or represents a storage location in
is stored in a character variable, its value memory (RAM). RAM contains many
is equivalent to the integer code of that cells to store values. Each cell in memory
character basically known as ASCII code. is 1 byte and has a unique address to
It can hold one letter/symbol like a, B, d identify it. The memory address is always
etc. Characters can also be of different an unsigned integer.
types.
 The non-primitive data structures are highly developed
Non-Primitive complex data structures. Basically, these are developed
from the primitive data structure. The non-primitive data
data structure : structure is responsible for organizing the group of
homogeneous and heterogeneous data elements.
Example of Non-primitive data structure :

ARRAYS LISTS FILES


Arrays are the set of homogeneous data elements stored in RAM. So, they can hold
only one type of data. The data may be all integers, all floating numbers or all
Arrays : characters. Values in an array are identified using array name with subscripts. Single
sub-scripted variables are known as a one-dimensional array or linear array; two sub-
scripted variables are referred as a two-dimensional array.
One Dimenstional Array Two Dimensional Array
 A list is a collection of a variable number of data items. Lists fall
in the non-primitive type of data structure in the classification of
data structure. Every element on a list contains at least two
fields, one is used to store data and the other one is used for
storing the address of next element.

Lists:
Files :

 Files contain data or information, stored


permanently in the secondary storage device
such as Hard Disk and Floppy Disk. It is
useful when we have to store and process a
large amount of data. A file stored in a
storage device is always identified using a
file name like HELLO.DAT or
TEXTNAME.TXT and so on. A file name
normally contains a primary and a secondary
name which is separated by a dot(.).
Like arrays, a stack is also defined as an ordered collection of
elements. A stack is a non-primitive linear data structure having a
special feature that we can delete and insert elements from only
one end, referred as TOP of the stack. The stack is also known as
Last In First Out (LIFO) type of data structure for this behaviour.

Stack :
When we perform insertion or deletion operation on a stack, its
base remains unchanged but the top of the stack changes.
Insertion in a stack is called Push and deletion of elements from
the stack is known as Pop.
We can implement a
stack using 2 ways:

 Static implementation (using arrays)


 Dynamic implementation (using
pointers)
Insertion Sort Algorithm

 In this tutorial, you will learn how insertion sort works. Also, you will find
working examples of insertion sort in C, C++, Java and Python.
 Insertion sort works in the similar way as we sort cards in our hand in a card
game.
 We assume that the first card is already sorted then, we select an unsorted card. If
the unsorted card is greater than the card in hand, it is placed on the right
otherwise, to the left. In the same way, other unsorted cards are taken and put at
their right place.
 A similar approach is used by insertion sort.
 Insertion sort is a sorting algorithm that places an unsorted element at its suitable
place in each iteration
 Suppose we need to sort the following array.

How
Insertion
Sort
Works?
Compare key  Compare key with the first element. If
the first element is greater than key, then key is placed
in front of the first element. key, then key is placed in
front of the first

The first element in the array


is assumed to be sorted. Take
the second element and store it
separately in key.
Now, the first two elements are sorted.
Take the third element and compare it with the elements on the left of it. Placed it just behind
the element smaller than it. If there is no element smaller than it, then place it at the begining
of the array
 Now, the first two elements are
sorted.
Take the third element and
compare it with the elements
on the left of it. Placed it just
behind the element smaller
than it. If there is no element
smaller than it, then place it at
the begining of the array
In a similar
way, place
every unsorted
element at its
correct position
Insertion sort in Java

import java.util.Arrays; array[j + 1] = key;


class InsertionSort { }}
void insertionSort(int array[]) { public static void main(String args[]) {
int size = array.length;
int[] data = { 9, 5, 1, 4, 3 };
for (int step = 1; step < size; step++) {
InsertionSort is = new InsertionSort();
int key = array[step];
is.insertionSort(data);
int j = step - 1;
while (j >= 0 && key < array[j]) { System.out.println("Sorted Array in
Ascending Order: ");
// For descending order, change key<array[j] to
key>array[j]. System.out.println(Arrays.toString(data));
array[j + 1] = array[j]; }}
--j; }
Selection Sort
Algorithm
 In this tutorial, you will learn how
selection sort works. Also, you will
find working examples of selection sort
in C, C++, Java and Python.
 Selection sort is an algorithm that
selects the smallest element from an
unsorted list in each iteration and
places that element at the beginning of
the unsorted list.
 How Selection Sort Works?
 Set the first element as minimum.
 Compare minimum with the second
element. If the second element is
smaller than minimum, assign second
element as minimum.
 Compare minimum with the third
element. Again, if the third element is
smaller, then assign minimum to the
third element otherwise do nothing.
The process goes on until the last
element.
After each iteration, minimum is placed in the front of the unsorted list.
For each iteration, indexing starts from the first unsorted element. Step 1 to 3 are repeated until all the elements
are placed at their correct positions.
Abstract Data type (ADT) is a type (or class) for objects
Abstract Data whose behaviour is defined by a set of value and a set of
operations.It is called “abstract” because it gives an
Types implementation-independent view. The process of
providing only the essentials and hiding the details is known
as abstraction.
EXAMPLE

// Selection sort in Java int temp = array[step];

import java.util.Arrays; array[step] = array[min_idx];

class SelectionSort { array[min_idx] = temp;


}
void selectionSort(int array[]) {
}
int size = array.length;
public static void main(String args[]) {
for (int step = 0; step < size - 1; step++) {
int[] data = { 20, 12, 10, 15, 2 };
int min_idx = step; SelectionSort ss = new SelectionSort();
for (int i = step + 1; i < size; i++) { ss.selectionSort(data);
if (array[i] < array[min_idx]) { System.out.println("Sorted Array in Ascending Order: ");
min_idx = i; System.out.println(Arrays.toString(data));
} }

} }
Abstract Data
Types
The user of data type does not need to
know how that data type is implemented,
for example, we have been using Primitive
values like int, float, char data types only
with the knowledge that these data type
can operate and be performed on without
any idea of how they are implemented.
List ADT

The data is generally stored in key sequence in


a list which has a head structure consisting of
count, pointers and address of compare
function needed to compare the data in the list.
List ADT

The data node contains the


pointer to a data structure and
a self-referential pointer which
points to the next node in the
list.
The List ADT
Functions is
given below:
In Stack ADT Implementation instead of data being stored in
each node, the pointer to data is stored.
The program allocates memory for the data and address is
passed to the stack ADT.

Stack ADT
The head node and the data
nodes are encapsulated in the
ADT. The calling function can
only see the pointer to the
stack.
The stack head structure also
contains a pointer to top and
count of number of entries
currently in stack.
Queue ADT

The queue abstract data type


(ADT) follows the basic design
of the stack abstract data type.
Each node contains a void
pointer to the data and the link
pointer to the next element in
the queue. The program’s
responsibility is to allocate
memory for storing the data.
 A Queue contains elements of the same type arranged in sequential order. Operations take place
at both ends, insertion is done at the end and deletion is done at the front. Following operations
can be performed:
 enqueue() – Insert an element at the end of the queue.
 dequeue() – Remove and return the first element of the queue, if the queue is not empty.
 peek() – Return the element of the queue without removing it, if the queue is not empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise return false.
 isFull() – Return true if the queue is full, otherwise return false.
The end
THANK YOU FOR YOUR ATTENTION!

You might also like