0% found this document useful (0 votes)
14 views20 pages

W3-Structure - New

CG

Uploaded by

Phạm Hiếu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views20 pages

W3-Structure - New

CG

Uploaded by

Phạm Hiếu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CẤU TRÚC

DỮ LIỆU

1
Objectives

▪ What is Data structure?


▪ Array?

[email protected] 2/14
CONTENT

▪What is Data Structure?


▪How Data Structure varies from
Data Type?
▪Classification of Data Structure
▪Need Of Data structure
▪ARRAY

[email protected] 3/14
1. What is Data Structure?

▪A data structure is a storage that is


used to store and organize data.
▪It is a way of arranging data on a
computer so that it can be accessed
and updated efficiently.

[email protected] 4/14
1. What is Data Structure? (1)

▪A data structure is not only used for


organizing the data.
▪It is also used for processing,
retrieving, and storing data.
▪There are different basic and advanced
types of data structures that are used
in almost every program or software
system that has been developed.
▪ We must have good knowledge of
data structures.

[email protected] 5/14
2. Data Structure VS Data Type

Data Type Data Structure


The data type is the form of
Data structure is a collection
a variable to which a value
of different kinds of data. That
can be assigned. It defines
entire data can be represented
that the particular variable
using an object and can be
will assign the values of the
used throughout the program.
given data type only.
It can hold value but not
It can hold multiple types of
data. Therefore, it is
data within a single object.
dataless.

[email protected] 6/14
2. Data Type VS Data Structure (1)

The implementation of a data Data structure implementation


type is known as abstract is known as concrete
implementation. implementation.
In data structure objects, time
There is no time complexity in
complexity plays an important
the case of data types.
role.
While in the case of data
In the case of data types, the structures, the data and its
value of data is not stored value acquire the space in the
because it only represents the computer’s main memory.
type of data that can be Also, a data structure can hold
stored. different kinds and types of
data within one single object.
Data type examples are int, Data structure examples are
float, double, etc. stack, queue, tree, etc.
[email protected] 7/14
3. Classification of Data Structure
3. Classification of Data Structure

▪ 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.


3. Classification of Data Structure

▪ 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: Array.
▪ Dynamic data structure: In the 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.
▪ Queue, stack, etc.
4. Need Of Data structure

▪ Data structure modification is easy.


▪ It requires less time.
▪ Save storage memory space.
▪ Data representation is easy.
▪ Easy access to the large database.
What is array

▪ An array is a collection of items stored at contiguous


memory locations. The idea is to store multiple items
of the same type together.
Basic terminologies of array

▪ Array Index: In an array, elements are identified by


their indexes. Array index starts from 0.
▪ Array element: Elements are items stored in an array
and can be accessed by their index
▪ Array Length: The length of an array is determined
by the number of elements it can contain.
Array in C++


Array in C++


Array in Java

▪ All arrays are dynamically allocated.


▪ Arrays are stored in contiguous memory [consecutive
memory locations].
▪ Since arrays are objects in Java, we can find their length
using the object property length. This is different from
C/C++, where we find length using sizeof.
▪ A Java array variable can also be declared like other
variables with [] after the data type.
▪ The variables in the array are ordered, and each has an
index beginning with 0.
▪ Java array can also be used as a static field, a local variable,
or a method parameter.
▪ The size of an array must be specified by int or short value
and not long.
Basic operation in Array

▪ Searching in Array (later)


▪ Reverse an array
▪ Array Rotations
▪ Insert, Delete
▪ Sort (later)
▪ Generate all subarrays
Basic operation in Array: Reverse

▪ 1) Initialize start and end indexes as start = 0,


end = n-1
2) In a loop, swap arr[start] with arr[end] and
change start and end as follows :
start = start +1, end = end – 1
Basic operation in Array: Rotations

▪ After rotating d positions to the left, the first d elements become


the last d elements of the array
▪ First store the elements from index d to N-1 into the temp array.
▪ Then store the first d elements of the original array into the temp array.
▪ Copy back the elements of the temp array into the original array
Basic operation in Array: Generation all subarrays

▪ Stop if we have reached the end of the array


▪ Increment the end index if start has become greater than end
▪ Print the subarray from index start to end and increment the starting
index

You might also like