1. Trang chủ
  2. » Công Nghệ Thông Tin

ASM 1 Algorithm and Datastructure FPT GREENWICH BTECH Distinction(SUPER HOT SALE)

31 76 4

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 31
Dung lượng 1,62 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Điểm của bài asm còn tùy thuộc vào người chấm. Chỉ cần paraphase bài này là có thể đạt merit hoặc có thể đạt distinction tùy vào thầy dạy. 1 trong nhưng tool paraphase mình recommend là quillbot.ASSIGNMENT 1 FRONT SHEET Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 20 Advanced Programming Submission date Date Received 1st submission Re submission Date Date Rec.

Trang 1

ASSIGNMENT 1 FRONT SHEET Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19: Data Structures and Algorithms

Submission date 26/02/2022 Date Received 1st submission

Student declaration

I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism I understand that making a false declaration is a form of malpractice

Student’s signature Grading grid

Trang 2

Summative Feedback: Resubmission Feedback:

Internal Verifier’s Comments:

IV Signature:

Trang 3

Table of Contents

B CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS

THAT CAN BE CARRIED OUT ON THE STRUCTURES 6

1 Abstract Data Types 6

2 Stack Abstract Data Type 6

a) Definition: Stack ADT could be simply understood as a linear data structure utilized for storing data (quite similar to Linked Lists) In other words, the stack can also be viewed as an order list Hence, the order where the data is stored as well as retrieved very matters in the stack Basically, the final element added also is the first element which is removed from the stack LIFO(Last in first out) or FILO(first in last out) list are the other names of stack ADT because of the stack’s mechanism 6 b) Stack operations and working mechanism: 7

c) Applications/Examples of Stack 9

C DETERMINE THE OPERATIONS OF A MEMORY STACK AND HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER 10

1 Definition of Stack Memory 10

2 Operations of Stack Memory 10

3 How it is used to implement function calls in a computer 10

D Queue ADT (FIFO) 13

1) Definition: 13

2) Queue operations and working mechanism 13

a) Main operations: 13

b) Other operations: 14

3) Applications/Examples of Queue 15

E Compare the performance of two sorting algorithms 16

1 Introduction 16

a) Definition: 16

b) Some common sorting algorithms: 16

c) Chosen Sorting Algorithms: 16

2 Bubble Sort: 18

3 Quick sort: 21

Trang 4

F USING AN IMPERATIVE DEFINITION, SPECIFY THE ABSTRACT DATA TYPE FOR A SOFTWARE STACK 24

1 Explanation of how to specify an abstract data type using the example of the software stack 24

1.1 Introduction to the formal specification, types of formal specification languages 24

1.2 Describe what are Pre-condition, Post-condition and error-condition 25

2 Explanation Of The Advantages Of Encapsulation And Information Hiding When Using An ADT 28

a) What is encapsulation 28

b) Why do we need encapsulation? 28

c) Advantages of encapsulation and information hiding when using an ADT Encapsulation uses accessors and mutators Encapsulation uses properties 29

d) Read-only property and Write-only property 30

G CONCLUSION 30

References 30

List of figure Figure 1: Stack 6

Figure 2: Push operation illustration 7

Figure 3: stack overflow 7

Figure 4: Pop operation illustration 7

Figure 5: PeekTop Opeartion Illustration 8

Figure 6: isEmpty operation illustration 8

Figure 7: Example of Stack(1) 9

Figure 8: Example of Stack(2) 9

Figure 9: Example of Stack(3) 9

Figure 10: Example of Stack(4) 9

Figure 11: Code Example 11

Figure 12: Function call examples 13

Figure 13: EnQueue Illustration 13

Figure 14: DeQueue Illustration 14

Figure 15: PeekHead illustration 14

Figure 16: QueueSize operation 14

Figure 17: isEmptyQueue illustration 15

Figure 18: Sorting algorithm 16

Figure 19: Bubble sort flow chart 20

Figure 20: Bubble sort Source code 20

Figure 21: Bubble sort example 21

Figure 22: Quick Sort Flow chart 23

Trang 5

Figure 23: Quick Sort Code implementation 23

Figure 24: Quick sort example 24

Figure 25: Encapsulation Illustration 28

A INTRODUCTION

In this assignment, there are some tasks I need to complete such as creating a design specification for

a data structure that consists the valid operations Following that, I will demonstrate memory stack

operations and how they are applied in computers to handle function calls Additionally, I will describe

the abstract data type for a software stack using an imperative description To more specific, taking an

example to introduce the concept of data structure for a First In First Out (FIFO) queue The

performance of two sorting algorithms will next be compared and discussed: bubble sort and quick

sort In the end, discussing about the benefits of encapsulating and concealing data with ADT

Trang 6

B CREATE A DESIGN SPECIFICATION FOR DATA STRUCTURES EXPLAINING THE VALID OPERATIONS THAT CAN

BE CARRIED OUT ON THE STRUCTURES

1 Abstract Data Types

Definition: Basically, Abstract Data Type(ADT) is viewed as an abstraction of data structure, where only

operations are defined but do not show their actual implementation Specifically, It is not specified what algorithms will be utilized to implement the operations or how the data will be arranged, retrieved

or stored in memory (Karumanchi, 2017)

Because it provides an implementation-independent perspective, it is considered "abstract." Abstraction is the process of presenting only the necessary information while obscuring the subtleties Each abstract data type always consists of the data collection and some operations on the data or nested data

Typically, to make an ADT, this is essential to have a set of activity axioms that govern the interaction

of operations Here are some common types of axiomatic operations of a basic ADT: creators(constructor), producers(+,-,*,/ decrease size when deleting element), accessors and mutators Some ADTs which are commonly used are List, Stack and Queue and here are a summary of their accessors and mutators operations:

Mutators Push()

Pop()

EnQueue() DeQueue()

Insert() Remove() RemoveAt() Replace()

Accessors PeekTop()

isEmpty() Size()

PeekHead() isEmpty() Size()

Get() Size() isEmpty() isFull()

These operations provide for them(List, Stack, Queue) the ability to interact with the data of its instances The term in the operation’s name also shows the clear purpose and logical behaviour of each one

2 Stack Abstract Data Type

a) Definition: Stack ADT could be simply understood as a linear data

structure utilized for storing data (quite similar to Linked Lists) In

other words, the stack can also be viewed as an order list Hence, the

order where the data is stored as well as retrieved very matters in

the stack Basically, the final element added also is the first element

which is removed from the stack LIFO(Last in first out) or FILO(first

in last out) list are the other names of stack ADT because of the

Trang 7

b) Stack operations and working mechanism:

Some particular operation terms are given to the two changes that can be made to a stack For simplicity, assume the data parameter is the integer data type:

Push(int):

When an element is added to the top of a stack, this operation is called “push”

 The “push” operation inserts and stores the element parameter onto the

top of the stack

 Each stack also has a fixed capacity; hence, the operation “push” could be

performed if the stack is not full In case the stack is full and the push

operation is still called to insert an element, this causes stack overflow and

the mechanism of push operation will throw a “stack overflow error”

exception

 When pushing an element successfully, the push mechanism returns the

value of the inserted element

Int Pop():

 When an element on the top is deleted from the stack, this operation is called “pop”

Figure 2: Push operation illustration

Figure 3: stack overflow

Figure 4: Pop operation illustration

Trang 8

 The “pop” operation removes and returns the current last(top) element(or the element which is most recently added) in the stack This working mechanism implicitly describes the order of the element in/out(the last inserted element is also the first removed element)

 If the stack is empty but the “pop” method is still called, this will cause stack underflow When this case happens, the underflow condition of the “pop” method will immediately throw “nullpointerexception” or other “RuntimeException”

 “pop” operation returns the integer value of the removed element

“isEmpty” will be in charge of this task

Figure 5: PeekTop Opeartion Illustration

Figure 6: isEmpty operation illustration

Trang 9

 “isEmpty” will check the existence of the top(last) element in the stack If this element does not exist, “isEmpty” returns true and reverse

c) Applications/Examples of Stack

 One of the applications widely known is Undo

sequence in a text editor(word, excel, notebook, etc…)

Based on the stack mechanism, the last text or

sequence users delete or change is also the first one

which will be “undo”(or restored) in the text editor

 The other significant example of the stack that anyone who

knows the internet sees or uses at least one time is “Page-visited

history in a Web browser [Back Buttons]” Simply, the last

page(website, site) users access is the first one which will be

redirected to when users click the “Back button” on the

browser Additionally, the way the history of the web browser

stores the list of website users’ access also is one example of the

stack The last website user’s access is always pushed to the top

of the history list sequentially

 Different types of parenthesis are used in programming, such as - (,),

and, to open and close blocks of code (Jaswal, 2020) This parenthesis

then influences how our program runs by being stored in the stack

 Stack memory – the base concept of computer science is also one of the

examples Different from Heap memory, stack memory stores the local

variables of each method when it is executed in the order of the Stack

ADT (last in first out) in temporary memory(RAM) The last function data

store also is the first one which will be removed

Figure 7: Example of Stack(1)

Figure 8: Example of Stack(2)

Figure 10: Example of Stack(4) Figure 9: Example of Stack(3)

Trang 10

C DETERMINE THE OPERATIONS OF A MEMORY STACK AND HOW IT IS USED TO IMPLEMENT FUNCTION CALLS IN A COMPUTER

1 Definition of Stack Memory

The execution of a thread using Stack memory Stack memory mainly stores the function calls, primitive local variables and references to other objects in the heap that the method is referring to as well as temporary method-specific variables Stack is one of the significant stack ADT applications, it always refers to storing data in LIFO(last in first out) order (Kamil, 2019)

To store local primitive values and references to other objects(such as an array, String,…) in the function calls, a new stack frame is always created for each method call in the stack memory When the function call is complete, the stack frame is popped out and terminated The size of each stack is limited based on OS

2 Operations of Stack Memory

Because the data is inserted and deleted in a last-in-first-out manner based on function call, It has two principal operations:

 Push: Storing the new data onto the top of stack memory

 Pop: Restoring the last data on the top of stack memory

 Peek: Access and retrieve the last data on the top of stack memory

3 How it is used to implement function calls in a computer

Before talking about the implementation of function calls, there is a concept needing to be defined and this is the activation record An activation record, which contains the space for all of the function's parameters and local variables, temporary objects, the return address, and other things the function needs, is where most implementations keep the data for a function call as a whole (Kamil, 2019)

When a function is called, the activation record is created immediately and the stack memory mechanism will push it to the top of the stack in this thread(Pushing) In this case, the activation record now could be called the stack frame(or block; in other words, the stack frame is an implementation of the activation record) The first activation record(stack frame) to be terminated is the most recent one that has been created

For more specific, there is a particular example of the function call:

Trang 11

I have a code screen like this one:

Here are the illustration of stack memory and their explanations

When running the program, the main() function is called first, hence an activation record(stack frame) is created and pushed to the top of the stack

The variable reqCapital(primitive type) is stored in this stack frame space with the value 1000

In the main() function, it calls the getCapital() function; so, the new unique stack frame is created and pushed on top of the previous stack frame

The parameter c is stored temporarily in this stack frame space

Figure 11: Code Example

Trang 12

In the getCapital(), when the bribeBoss() is called, new stack frame immediately is created and pushed on top of stack

This stack frame store the argument (int m = 1000) passed from getCapital() function and the variable bribeMoney(int) = 300 after calculating

When the bribeBoss() function finishes executing, this stack frame will

be popped out from the stack and all data stored in this one also are terminated from memory This function return the value to the getCapital() function and that value replaces the function call in the the stack frame of getCapital() function

The stack frame of getCapital() is popped out when this function is completed All data stored in getCapital() stack frame also are terminated Only the return value is returned to main() function and the main() function stores it temporarily

When the program finishes, all stack frames are popped out

Trang 13

Summary of the working process of how the stack memory implements function calls:

D Queue ADT (FIFO)

1) Definition:

The queue is a common abstract data type used to store data in an order list in which the data is inserted at the end of the queue and removed in the first place of this one In other words, the first data inserted is the first one which will be removed from the queue Hence, its mechanism is called FIFO(first in first out) or LILO(last in last out)

2) Queue operations and working mechanism

Based on queue mechanism(FIFO), here are some specific operation terms making a queue ADT For simplicity, assume the data parameter is the integer data type:

a) Main operations:

EnQueue: when one element is added to the queue, this operation is called “EnQueue”

EnQueue gets the parameter’s value and inserts it in the end sequently with each inserted element

Each Queue also has a fixed capacity; hence, the operation “EnQueue” could be performed if the queue is not full yet If the queue does not have any available place and the enqueue operation is still called to insert an element, this causes queue overflow and the program of enqueue operation will throw a “queue overflow error” exception

Figure 12: Function call examples

Figure 13: EnQueue Illustration

Trang 14

DeQueue: When the element is removed from the queue, this operation is called “DeQueue”

DeQueue removes the first element(which has been first inserted into the queue In other words, the located at the first place in the queue) sequently with each inserted element This operation only is used when the Queue has at least one element; In other words, when the queue is not empty, DeQueue could be executed If the queue does not have any element (empty queue) and the dequeue operation is still called to delete the first element, this causes queue underflow and the program of the dequeue operation will throw a “queue underflow error” exception

When the Dequeue completes deleting the first element, it will return this value of this element

b) Other operations:

PeekHead: This operation also returns the first element without deleting it

As the “DeQUeue” operation, If the queue is empty but the “PeekHead” method is still called, this will cause queue underflow When this case happens, the underflow condition of the

“PeekHead” method will immediately throw “nullpointerexception” or other

“RuntimeException”

QueueSize: QueueSize returns the existed number in queue

Figure 14: DeQueue Illustration

Figure 15: PeekHead illustration

Figure 16: QueueSize operation

Trang 15

isEmptyQueue: When other operations (“Dequeue”, “peek head”,…) always need to check the

number of existing elements in the queue(empty or not) to decide to perform algorithms or throw an error “isEmptyQueue” will be in charge of this task

This operation will return true if the queue is empty and reverse

3) Applications/Examples of Queue

a) Task Queue in javascript: the concept “event loop” in javascript contains two more sub-concepts: Microtask Queue and Macrotask Queue which are the significant application of Queue ADT These names of this two-term (microtask “Queue” and macrotask “queue”), explicitly show that they are based on the Queue mechanism (FiFo or first in first out) Specifically, The event loop takes and deletes the first task at the first place in the micro-macro task queue, and when another asyncrinous task is performed, this will be added at the end of these task queues after some handling process

b) Another application of Queue is Access to Shared Resources In a networked system, several workstations or computers request a service from a single resource at the same time The queries are processed in fractions of a second, which is extremely quick (Anon., 2021)

Despite the fact that there is usually some time gap in the requests generated This state is kept by implementing a queue in the server's device

c) Job Scheduling: CPU scheduling or I/O scheduling or Job scheduling is the most essential application

of a queue A queue ADT is used to organize and schedule the jobs that computers do in the processor/CPU That is the way the computer does these jobs in the sequence stipulated (Pandey, 2022)

d) Message/mail queue: A queue is a line of messages that are waiting to be treated, commencing at the top and working down the line A message queue is a collection of messages that are transferred from one application to the next It holds a collection of work objects that are awaiting processing

A message is data that is sent between the sender and recipient programs; it is just a byte array with some headers on top One form of the message is an event One program directs another application to begin processing a given job by using the queue

Figure 17: isEmptyQueue illustration

Ngày đăng: 30/10/2022, 21:28

TỪ KHÓA LIÊN QUAN